Hello everyone, in this chapter we add the image compression function. If you have any questions, please contact me at [email protected]. Ask for directions of various gods, thank you

Now on many websites, there are uploading pictures this function, and pictures for many mobile phones now, are taken out of the hd pictures, the resolution is also very high, of course, the storage space is large. So the question is, what if every one of your users uploads a 3M picture?

If people load your web page for a long time because the image is too big, it’s not a problem that money can solve.

Because the user’s network environment is out of your control. So in this chapter we add image compression, add watermark, format conversion and other functions.

One: Add thumbnailator dependencies

< the dependency > < groupId > net. Coobird < / groupId > < artifactId > thumbnailator < / artifactId > < version > 0.4.8 < / version > </dependency>Copy the code

Two: create a picture processing class

To create the core – utils – ImageUtils. Java

package com.example.demo.core.utils; import net.coobird.thumbnailator.Thumbnails; import net.coobird.thumbnailator.geometry.Positions; import net.coobird.thumbnailator.name.Rename; import net.coobird.thumbnailator.resizers.configurations.ScalingMode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.imageio.ImageIO; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; /** ** @author */ public class ImageUtils {public static Logger Logger = LoggerFactory.getLogger(ImageUtils.class); Public static void main(String[] args) {generateFixedSizeImage(); / / to the original image watermarking, then rotate it 90 degrees clockwise and finally 80% compression save / / generateRotationWatermark (); GenerateOutputstream (); generateOutputstream(); //generateScale(); / / generated thumbnail to the specified directory / / generateThumbnail2Directory (); / / will specify all generated thumbnail images/directory/generateDirectoryThumbnail (); } /** ** use the given image to generate the specified size image */ public static voidgenerateFixedSizeImage(){
        try {
            Thumbnails.of("C:\ Users\ Administrator\ Desktop\ wechat image_20180129100019.jpg"). The size (80). ToFile ("C:\\Users\\Administrator\\Desktop\\newmeinv.jpg"); } catch (IOException e) { logger.error(e.getMessage()); } /** * Watermarks the original image, then rotates it 90 degrees clockwise, and finally compresses it to 80% */ public static voidgenerateRotationWatermark(){
        try {
            Thumbnails.of("C:\ Users\ Administrator\ Desktop\ wechat image_20180129100019.jpg"Rotate (90). // Rotate (90) clockwise. // The watermark is located at the bottom right, translucent watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("C:\ Users\ Administrator\ Desktop\ wechat image_20180329153521.png"// Image compression 80% outputQuality(0.8).tofile ("C:\\Users\\Administrator\\Desktop\\2016010208_new.jpg"); } catch (IOException e) { logger.error(e.getMessage()); } /** * convert the image format to the output stream */ public static voidgenerateOutputstream(){
        try(OutputStream outputStream = new FileOutputStream("data/2016010208_outputstream.png")) {
            Thumbnails.of("data/2016010208.jpg").size (500,500). // Convert format outputFormat("png"// write the outputStream toOutputStream(outputStream); } catch (IOException e) { logger.error(e.getMessage()); }} /** ** scale the image */ public static voidgenerateScale(){
        try {
            Thumbnails.of("data/2016010208.jpg"ScalingMode (scalingmode.bicubic). // Image scale 80%, can not be used with size() scale(0.8). // Image quality compression 80% outputQuality(0.8).tofile ("data/2016010208_scale.jpg"); } catch (IOException e) { logger.error(e.getMessage()); }} /** * Generate thumbnail to the specified directory */ public static voidgenerateThumbnail2Directory(){
        try {
            Thumbnails.of("data/2016010208.jpg"."data/meinv.jpg"ToFiles (new File(); toFiles(new File();"data/new/"), Rename.NO_CHANGE); } catch (IOException e) { logger.error(e.getMessage()); }} /** * generate thumbnails for all images in the specified directory */ public static voidgenerateDirectoryThumbnail(){
        try {
            Thumbnails.of(
                    new File("data/new"). ListFiles ()). Scale (0.8). The toFiles (new File ("data/new/"), Rename.SUFFIX_HYPHEN_THUMBNAIL); } catch (IOException e) { logger.error(e.getMessage()); }}}Copy the code

Three: Basic usage method introduction

Thumbnails.of("Path to original file"Size (100,100) // scale between 0 and 1. Scale (1f) // picture quality between 0 and 1. OutputQuality (0.5f) // clockwise rotation degree Watermark (Positions.BOTTOM_RIGHT, imageio.read (new File(position.BOTTOM_RIGHT, imageio.read)"Watermark path")),1f).
        .toFile("Compressed file path");Copy the code

Four: document address

Specific examples can be viewed by ourselves

Help document


The project address

Code cloud address: gitee.com/beany/mySpr…

GitHub address: github.com/MyBeany/myS…

Writing articles is not easy, if it is helpful to you, please help click star

At the end

The image compression processing function has been added, and the subsequent functions will be updated in succession. If you have any questions, please contact me at [email protected]. Ask for directions from various gods, thank you.