简体   繁体   English

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

[英]how to resize Image in java?

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 : 您好我有QR码图像,我想调整大小,当我尝试使用此代码将其调整为小图像时,我总是得到一个blury图像,当我扫描它时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;
}

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

Based on @A4L's answer: 根据@ A4L的回答:

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

I wrote this class which i personally also use. 我写了这个我个人也使用的课程。 I hope the code is straight forward. 我希望代码是直截了当的。

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;


public class ImageScaler {

        private ImageIcon originalImage;
        private ImageIcon scaledImage;

        public ImageScaler(Image image) {
                this.originalImage = new ImageIcon(image);
        }

        public ImageScaler(String fileName) {
                originalImage = new ImageIcon(fileName);
        }

        public void createScaledImage(int size, ScalingDirection scalingDirection) {
                if (scalingDirection == ScalingDirection.HORIZONTAL) {
                        scaledImage = new ImageIcon(originalImage.getImage().getScaledInstance(size, -1, Image.SCALE_SMOOTH));
                } else {
                        scaledImage = new ImageIcon(originalImage.getImage().getScaledInstance(-1, size, Image.SCALE_SMOOTH));
                }       
        }

        public void createScaledImage(int size, ScalingDirection scalingDirection, int scale) {
                if (scalingDirection == ScalingDirection.HORIZONTAL) {
                        scaledImage = new ImageIcon(originalImage.getImage().getScaledInstance(size, -1, scale));
                } else {
                        scaledImage = new ImageIcon(originalImage.getImage().getScaledInstance(-1, size, scale));
                }
        }

        public void createScaledImage(int width, int height, ScaleType scaleType) {
                int imageWidth = originalImage.getImage().getWidth(null);
                int imageHeight = originalImage.getImage().getHeight(null);
                double originalImageRatio = imageWidth / (double) imageHeight;
                double scaledImageRatio = width / (double) height;

                if(scaleType == ScaleType.FIT) {
                        if(imageHeight - (Math.abs(imageWidth - width) / originalImageRatio) <= height) {
                                scaledImage = new ImageIcon(originalImage.getImage().getScaledInstance(width, -1, Image.SCALE_SMOOTH));
                        } else if(imageWidth - (Math.abs(imageHeight - height) * originalImageRatio) <= width) {
                                scaledImage = new ImageIcon(originalImage.getImage().getScaledInstance(-1, height, Image.SCALE_SMOOTH));
                        }
                } else if(scaleType == ScaleType.FILL) {
                        if(imageHeight - (Math.abs(imageWidth - width) / originalImageRatio) >= height) {
                                scaledImage = new ImageIcon(originalImage.getImage().getScaledInstance(width, -1, Image.SCALE_SMOOTH));
                                int thumbHeight = scaledImage.getImage().getHeight(null);

                                // Crop the image
                                scaledImage = new ImageIcon(Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(scaledImage.getImage().getSource(), new CropImageFilter(0, (thumbHeight-height)/2, width, height))));
                        } else if(imageWidth - (Math.abs(imageHeight - height) * originalImageRatio) >= width) {
                                scaledImage = new ImageIcon(originalImage.getImage().getScaledInstance(-1, height, Image.SCALE_SMOOTH));
                                int thumbWidth = scaledImage.getImage().getWidth(null);

                                // Crop the image
                                scaledImage = new ImageIcon(Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(scaledImage.getImage().getSource(), new CropImageFilter((thumbWidth-width)/2, 0, width, height))));
                        }               
                }
        }

        public void saveScaledImage(File file, ImageType imageType) {
                if (scaledImage != null) {
                        BufferedImage bi = new BufferedImage(scaledImage.getIconWidth(), scaledImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
                        Graphics g = bi.getGraphics();
                        g.drawImage(scaledImage.getImage(), 0, 0, null);
                        try {
                                ImageIO.write(bi, imageType.value(), file);
                        } catch (IOException ioe) {
                                System.out.println("Error occured saving scaled image");
                        }
                } else {
                        System.out.println("Scaled image has not yet been created");
                }
        }

        public void saveOriginalImage(File file, ImageType imageType) {
                if (originalImage != null) {
                        BufferedImage bi = new BufferedImage(originalImage.getIconWidth(), originalImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
                        Graphics g = bi.getGraphics();
                        g.drawImage(originalImage.getImage(), 0, 0, null);
                        try {
                                ImageIO.write(bi, imageType.value(), file);
                        } catch (IOException ioe) {
                                System.out.println("Error occured saving original image");
                        }
                } else {
                        System.out.println("Original image has not yet been created");
                }
        }

        // ENUMS
        public enum ScalingDirection {VERTICAL, HORIZONTAL};
        public enum ScaleType {FIT, FILL};
        public enum ImageType {
                IMAGE_JPEG ("jpeg"),
                IMAGE_JPG ("jpg"),
                IMAGE_PNG ("png");

                private String value = null;

                ImageType(String value) {
                        this.value = value;
                }

                String value() {
                        return value;
                }
        };
}   

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 请检查一下Image.getScaledInstance()详细信息可以在这个答案中找到: 如何提高g.drawImage()方法的性能来调整图像大小

Hope it helps 希望能帮助到你

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

I recommend Thumbnailnator because it gave me better quality images than the Java multi-step approach. 我推荐Thumbnailnator,因为它给了我比Java多步骤方法更高质量的图像。 The speed however might be better with your Graphics2D drawImage code. 然而,使用Graphics2D drawImage代码的速度可能会更好。

See also Java - resize image without losing quality 另请参阅Java - 调整图像大小而不会降低质量

In fact, the solution is even more simple. 事实上,解决方案更加简单。 You don't have to create a new BufferedImage. 您不必创建新的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. 您可以将for3st提到的方法直接应用于原始BufferedImage图像('image')并设置您希望的宽度和高度。 Thus a single statement is only needed (included in stadard Java documentation): 因此,只需要一个语句(包含在stadard Java文档中):

drawImage(BufferedImage image, int x, int y, int width, int height, ImageObserver observer) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM