java-image-scaling图片缩放

洪子晋
2023-12-01

文档地址:https://code.google.com/archive/p/java-image-scaling/wikis/Getting_started.wiki

项目地址:https://github.com/mortennobel/java-image-scaling

Introduction

This will guide you through how to rescale images using this library.

A simple rescale

Lets say that you have a BufferedImage instance called tomato that should be rescaled to 100x200. If you want a high quality rescale, you should choose the ResampleOp class:

ResampleOp resampleOp = new ResampleOp (100,200); BufferedImage rescaledTomato = resampleOp.filter(tomato, null);

Tip: You might need to increase your maximum heap. This can be done using the -Xmx parameter, such as: java -Xmx512m MyJavaClass

Using Unsharpen Filter

Let's improve the quality a bit more adding a unsharpen filter to the final image. This will remove some of blur, that a rescale operation creates. ResampleOp resampleOp = new ResampleOp (100,200); resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Normal); BufferedImage rescaledTomato = resampleOp.filter(tomato, null);

Adding a listener

If your tomato image was very large, this might take some time. The following code add a change listener so the user can see that the computer is actually is working. ResampleOp resampleOp = new ResampleOp (100,200); resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Normal); resampleOp.addProgressListener(new ProgressListener() { public void notifyProgress(float fraction) { System.out.printf("Still working - %f percent %n",fraction*100); } }); BufferedImage rescaledTomato = resampleOp.filter(tomato, null);

 

 

依赖

<dependency>
      <groupId>com.mortennobel</groupId>
      <artifactId>java-image-scaling</artifactId>
      <version>0.8.6</version>
    </dependency>

代码

public static void resize(File originalFile, File resizedFile, int newWidth, int newHeight, String formatName) {
        try {
            BufferedImage inputBufImage = ImageIO.read(originalFile);
            ResampleOp resampleOp = new ResampleOp(newWidth, newHeight);// 转换
            resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Oversharpened);
            BufferedImage rescaledTomato = resampleOp.filter(inputBufImage,null);
            ImageIO.write(rescaledTomato, formatName, resizedFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

 类似资料: