如何在java中调整图像大小? [英] how to resize Image in java?

查看:164
本文介绍了如何在java中调整图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hello I have a QR code Image , and I want to resize it , when I try to resize it to a small image using this code , I always get a blury image , and the QR code is no longer valid when I scan it , but it works fine when I resize to a big sized images with the same code :

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

有什么问题,我不太明白,请给我说至少提示一下,谢谢

what is the problem , I don't exactly understand , please give me at least a hint , thank you

推荐答案

我用仿射变换来实现这个任务,这里是我的代码,希望有所帮助

I use affine transformation to achieve this task, here is my code, hope it helps

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

这篇关于如何在java中调整图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆