Swing中调整图像大小

5
我有一段代码,用于调整图像大小以达到特定的尺寸(我想将分辨率更改为200 dpi之类的)。基本上我需要这个代码是因为我想显示用户选择的图像(相对较大),如果用户同意,我想在另一个地方以较低的分辨率显示相同的图像。不幸的是,如果我给它一个大图像,屏幕上什么也没有显示。同时,如果我更改了

标签,那么代码就无法正常工作。
imageLabel.setIcon(newIcon); 

为了

imageLabel.setIcon(icon); 

我能看到图片的显示,但分辨率不对,这表明问题出现在代码片段中而不是其他地方。请参考以下翻译:

我能够显示图片,但分辨率不正确,因此我知道问题出现在此代码片段中而不是其他地方。

Image img = icon.getImage();

BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
boolean myBool = g.drawImage(img, 0, 0, 100, 100, null);
System.out.println(myBool);
ImageIcon newIcon = new ImageIcon(bi);
imageLabel.setIcon(newIcon);
submitText.setText(currentImagePath);
imageThirdPanel.add(imageLabel);

  1. 你有问题吗?
  2. 注意,从ImageIcon newIcon = new ImageIcon(bi);之前的所有内容都是AWT,而不是Swing。
  3. 它是'Swing',而不是'swing'
  4. 为了更快地获得更好的帮助,请发布一个SSCCE
- Andrew Thompson
好的。我没有展示整个代码,因为它太长了。另外,我不确定为什么你说它是Swing而不是swing(这是我在输入标签时从自动完成中得到的 - 我不认为这是我的错)。我认为这是SSCCE。就像我说的,其余的代码都可以工作,我肯定这段代码有问题。可能是在绘制图像时,但我不确定是什么问题。 - Paul Kar.
"我认为这是SSCCE。" 你的想法是错误的,请阅读文档。 - Andrew Thompson
为什么不使用 Image.getScaledInstance() - Jomoos
4个回答

9
您不必太关注缩放图像的细节。Image类已经有一个名为getScaledInstance(int width, int height, int hints) 的方法专门用于此目的。 Java文档中说:

创建此图像的缩放版本。默认情况下,将返回一个新的Image对象,该对象将以指定的宽度和高度呈现图像。即使原始源图像已完全加载,也可以异步加载新的Image对象。如果宽度或高度为负数,则会替换一个值以保持原始图像尺寸的纵横比。

您可以这样使用它:
// Scale Down the original image fast
Image scaledImage = imageToScale.getScaledInstance(newWidth, newHighth, Image.SCALE_FAST);
// Repaint this component
repaint();

点击此处查看完整示例。


如果需要按比例缩放,请将参数设置为 -1,以便测量值跟随另一个测量值。例如,如果您只想根据宽度进行缩放,则可以调用类似 anImage.getScaledInstance(150, -1, java.awt.Image.SCALE_FAST) 的方法。 - Jaime Hablutzel

6
这是我的解决方案:
    private BufferedImage resizeImage(BufferedImage originalImage, int width, int height, int type) throws IOException {  
        BufferedImage resizedImage = new BufferedImage(width, height, type);  
        Graphics2D g = resizedImage.createGraphics();  
        g.drawImage(originalImage, 0, 0, width, height, null);  
        g.dispose();  
        return resizedImage;  
    }  

1

尝试使用以下代码调整图像大小:

public static Image scaleImage(Image original, int newWidth, int newHeight) {
    //do nothing if new and old resolutions are same
    if (original.getWidth() == newWidth && original.getHeight() == newHeight) {
        return original;
    }

    int[] rawInput = new int[original.getHeight() * original.getWidth()];
    original.getRGB(rawInput, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());
    int[] rawOutput = new int[newWidth * newHeight];
    // YD compensates for the x loop by subtracting the width back out
    int YD = (original.getHeight() / newHeight) * original.getWidth() - original.getWidth();
    int YR = original.getHeight() % newHeight;
    int XD = original.getWidth() / newWidth;
    int XR = original.getWidth() % newWidth;
    int outOffset = 0;
    int inOffset = 0;
    for (int y = newHeight, YE = 0; y > 0; y--) {
        for (int x = newWidth, XE = 0; x > 0; x--) {
            rawOutput[outOffset++] = rawInput[inOffset];
            inOffset += XD;
            XE += XR;
            if (XE >= newWidth) {
                XE -= newWidth;
                inOffset++;
            }
        }
        inOffset += YD;
        YE += YR;
        if (YE >= newHeight) {
            YE -= newHeight;
            inOffset += original.getWidth();
        }
    }
    return Image.createRGBImage(rawOutput, newWidth, newHeight, false);
}

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接