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);
4

4 回答 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();

检查以获取完整示例。

于 2011-11-27T08:39:15.097 回答
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;  
    }  
于 2011-11-27T08:32:17.060 回答
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);
}
于 2011-11-27T07:17:42.973 回答
0

这里给出了另一个例子:

2D-Graphics/LoadImageandscaleit.htm">http://www.java2s.com/Tutorial/Java/0261_2D-Graphics/LoadImageandscaleit.htm

http://www.java2s.com/Code/JavaAPI/java.awt/ImagegetScaledInstanceintwidthintheightinthints.htm

于 2011-11-27T10:18:58.360 回答