简体   繁体   English

使用spring-boot应用程序进行自动图像压缩/优化

[英]Automatic image compression/optimization with spring-boot application

is there a way to automatically compress/optimize images in a spring-boot-application? 在spring-boot-application中,有没有办法自动压缩/优化图像? As in my application the user can put any images in a folder themselves, I cannot make sure, they are compressed the best way. 就像在我的应用程序中一样,用户可以自己将任何图像放在文件夹中,我无法确定,它们是最佳压缩方式。 And as they are not uploaded through the application, I can also not create an optimized version. 而且由于它们不是通过应用程序上传的,因此我也无法创建优化版本。

So what I would like to do is to compress/optimize the images once they are requested and maybe save them in a kind of "image-cache" for a while. 因此,我想做的就是在请求图像后对其进行压缩/优化,也许将它们保存在一种“图像缓存”中一段时间​​。

Or is there a tomcat/apache-module, which already does this kind of things out-of-the box? 还是有一个tomcat / apache-module,这种东西已经开箱即用了?

Thanks for your help 谢谢你的帮助

You can use javax.imageio 's classes and interface to compress a given image. 您可以使用javax.imageio的类和接口来压缩给定的图像。 Below is an example of image compression of JPG image. 以下是JPG图像的图像压缩示例。 You can add the below main method code to your service in spring boot application. 您可以将以下主要方法代码添加到Spring Boot应用程序中的服务中。

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

public class ImageCompression {
    public static void main(String[] args) throws FileNotFoundException, IOException{
        File imageFile = new File("YOUR_IMAGE.jpg");
        File compressedImageFile = new File("YOUR_COMPRESSED_IMAGE.jpg");

        InputStream inputStream = new FileInputStream(imageFile);
        OutputStream outputStream = new FileOutputStream(compressedImageFile);

        float imageQuality = 0.3f;

        //Create the buffered image
        BufferedImage bufferedImage = ImageIO.read(inputStream);

        //Get image writers
        Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByFormatName("jpg");

        if (!imageWriters.hasNext())
            throw new IllegalStateException("Writers Not Found!!");

        ImageWriter imageWriter = (ImageWriter) imageWriters.next();
        ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputStream);
        imageWriter.setOutput(imageOutputStream);

        ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();

        //Set the compress quality metrics
        imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        imageWriteParam.setCompressionQuality(imageQuality);

        //Created image
        imageWriter.write(null, new IIOImage(bufferedImage, null, null), imageWriteParam);

        // close all streams
        inputStream.close();
        outputStream.close();
        imageOutputStream.close();
        imageWriter.dispose();
    }
}

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

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