how to resize Image in java?

2019-01-14 08:04发布

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

7条回答
Bombasti
2楼-- · 2019-01-14 08:08

Please check this out Image.getScaledInstance() details can be found in this answer: How to improve the performance of g.drawImage() method for resizing images

Hope it helps

查看更多
倾城 Initia
3楼-- · 2019-01-14 08:10

Based on @A4L's answer:

A more straigt forward version. Also his solution did only scale the canvas not the image itself.

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

to increase the quality you could add

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);
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-14 08:11

I recommend Thumbnailnator because it gave me better quality images than the Java multi-step approach. The speed however might be better with your Graphics2D drawImage code.

See also Java - resize image without losing quality

查看更多
爷、活的狠高调
5楼-- · 2019-01-14 08:12
    public static BufferedImage resizeImg(BufferedImage img, int newW, int newH)
    {
    int w = img.getWidth();
    int h = img.getHeight();
    BufferedImage dimg = new BufferedImage(newW, newH, img.getType());
    Graphics2D g = dimg.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
    g.dispose();
    return dimg;      
   }
查看更多
成全新的幸福
6楼-- · 2019-01-14 08:15

In fact, the solution is even more simple. You don't have to create a new BufferedImage. You can apply the method mentioned by for3st directly to the original BufferedImage image ('image') and set the width and height you wish for it. Thus a single statement is only needed (included in stadard Java documentation):

drawImage(BufferedImage image, int x, int y, int width, int height, ImageObserver observer) 
查看更多
家丑人穷心不美
7楼-- · 2019-01-14 08:20

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;
}
查看更多
登录 后发表回答