Java 在swing中调整图像大小

Java 在swing中调整图像大小,java,swing,graphics,bufferedimage,imageicon,Java,Swing,Graphics,Bufferedimage,Imageicon,我有一段代码,用于将图像大小调整为窗帘大小(我想将分辨率更改为200 dpi)。基本上,我需要它的原因是因为我想显示用户拾取的图像(有点大),然后如果用户同意,我想在不同的位置显示相同的图像,但使用较小的分辨率。不幸的是,如果我给它一个大的图像,屏幕上什么也不会出现。还有,如果我改变 imageLabel.setIcon(newIcon); 到 我得到了要显示的图像,但分辨率不正确,这就是为什么我知道我在这段代码中遇到了问题,而不是在其他地方 Image img = icon.getImag

我有一段代码,用于将图像大小调整为窗帘大小(我想将分辨率更改为200 dpi)。基本上,我需要它的原因是因为我想显示用户拾取的图像(有点大),然后如果用户同意,我想在不同的位置显示相同的图像,但使用较小的分辨率。不幸的是,如果我给它一个大的图像,屏幕上什么也不会出现。还有,如果我改变

imageLabel.setIcon(newIcon); 

我得到了要显示的图像,但分辨率不正确,这就是为什么我知道我在这段代码中遇到了问题,而不是在其他地方

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

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

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);
}
以下是我的解决方案:

    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;  
    }  

你真的不必关心缩放图像的细节。Image类已经有一个方法
getScaledInstance(int-width、int-height、int-hints)
专门为此设计。 Java文档说明:

创建此图像的缩放版本。返回一个新的图像对象 将按指定的宽度和高度渲染图像 违约即使 原始源图像已完全加载。如果有的话 宽度或高度为负数,然后替换一个值 以保持原始图像尺寸的纵横比

您可以这样使用它:

// Scale Down the original image fast
Image scaledImage = imageToScale.getScaledInstance(newWidth, newHighth, Image.SCALE_FAST);
// Repaint this component
repaint();

查看完整的示例。

此处给出了另一个示例:

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


1)你有问题吗?2)注意到
ImageIcon newIcon=newImageIcon(bi);
之前的所有内容都是AWT,而不是Swing。3)是“Swing”,而不是“Swing”。4)为了更快地获得更好的帮助,请发布一条。好的。我不会显示整个代码,因为它太长。另外,我也不确定你为什么说它是Swing而不是Swing(这是我在输入标记时从自动完成中得到的-我不知道这是我的错)。我认为这是SSCCE。正如我所说的,其余的代码工作正常,我确信这段代码中存在问题。可能是在我绘制图像时,但我不确定是什么。”我认为这是SSCCE。“您想错了。请阅读文档。为什么不使用
Image.getScaledInstance()
?如果需要比例缩放,请将
-1
作为度量值的参数,该度量值应紧跟在另一个度量值之后。例如,如果您只想基于宽度进行缩放,您可以调用类似
anImage.getScaledInstance的内容。”(150,-1,java.awt.Image.SCALE\u FAST)
// Scale Down the original image fast
Image scaledImage = imageToScale.getScaledInstance(newWidth, newHighth, Image.SCALE_FAST);
// Repaint this component
repaint();