如何在Java中调整图像大小?

2022-09-02 19:12:15

你好,我有一个QR码图像,我想调整它的大小,当我尝试使用此代码将其调整为小图像时,我总是得到一个模糊的图像,并且当我扫描它时QR码不再有效,但是当我用相同的代码调整为大尺寸图像时,它可以正常工作:

public BufferedImage getScaledInstance(BufferedImage img,
                                   int targetWidth,
                                   int targetHeight,
                                   Object hint,
                                   boolean higherQuality)
{
int type = (img.getTransparency() == Transparency.OPAQUE) ?
    BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage ret = (BufferedImage)img;
int w, h;
if (higherQuality) {
    // Use multi-step technique: start with original size, then
    // scale down in multiple passes with drawImage()
    // until the target size is reached
    w = img.getWidth();
    h = img.getHeight();
} else {
    // Use one-step technique: scale directly from original
    // size to target size with a single drawImage() call
    w = targetWidth;
    h = targetHeight;
}

do {
    if (higherQuality && w > targetWidth) {
        w /= 2;
        if (w < targetWidth) {
            w = targetWidth;
        }
    }

    if (higherQuality && h > targetHeight) {
        h /= 2;
        if (h < targetHeight) {
            h = targetHeight;
        }
    }

    BufferedImage tmp = new BufferedImage(w, h, type);
    Graphics2D g2 = tmp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
    //        g2.setRenderingHint(RenderingHints.KEY_DITHERING, hint);
    g2.drawImage(ret, 0, 0, w, h, null);
    g2.dispose();

    ret = tmp;
} while (w != targetWidth || h != targetHeight);

return ret;
}

有什么问题,我不完全明白,请至少给我一个提示,谢谢


答案 1

我使用仿射变换来完成这个任务,这是我的代码,希望它有帮助

/**
 * scale image
 * 
 * @param sbi image to scale
 * @param imageType type of image
 * @param dWidth width of destination image
 * @param dHeight height of destination image
 * @param fWidth x-factor for transformation / scaling
 * @param fHeight y-factor for transformation / scaling
 * @return scaled image
 */
public static BufferedImage scale(BufferedImage sbi, int imageType, int dWidth, int dHeight, double fWidth, double fHeight) {
    BufferedImage dbi = null;
    if(sbi != null) {
        dbi = new BufferedImage(dWidth, dHeight, imageType);
        Graphics2D g = dbi.createGraphics();
        AffineTransform at = AffineTransform.getScaleInstance(fWidth, fHeight);
        g.drawRenderedImage(sbi, at);
    }
    return dbi;
}

答案 2

根据@A4L的回答:

一个更直接向前的版本。此外,他的解决方案只缩放了画布,而不是图像本身。

public static BufferedImage scale(BufferedImage imageToScale, int dWidth, int dHeight) {
        BufferedImage scaledImage = null;
        if (imageToScale != null) {
            scaledImage = new BufferedImage(dWidth, dHeight, imageToScale.getType());
            Graphics2D graphics2D = scaledImage.createGraphics();
            graphics2D.drawImage(imageToScale, 0, 0, dWidth, dHeight, null);
            graphics2D.dispose();
        }
        return scaledImage;
    }

以提高您可以添加的质量

graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

推荐