I. Java uses google's thumbnailator tool to process image compression watermarks,
Thumbnailator is a good open-source image tool: Add the following jar package to pom
<! -- Start -->
<Dependency>
<GroupId> net. coobird </groupId>
<ArtifactId> thumbnailator </artifactId>
<Version> 0.4.8 </version>
</Dependency> <! -- Image thumbnail image compression watermark end -->
Then, only one line of code is required for compression and watermarking.
Package com. shallowmemory. test; import net. coobird. thumbnailator. thumbnails; import net. coobird. thumbnailator. geometry. positions; import javax. imageio. imageIO; import java. awt. image. bufferedImage; import java. io. file; import java. io. IOException;/*** Created by HONGLINCHEN on * Image Compression * @ author HONGLINCHEN * @ since JDK 1.8 */public class ImgCompress {public static void main (String [] args) throw S IOException {// The first parameter of the compressed image is the path after the source image path which is the compressed output path Thumbnails. of ("C: \ Users \ HONGLINCHEN \ Desktop \ 23.jpg "). size (600,600 ). outputQuality (0.8f ). toFile ("C: \ Users \ HONGLINCHEN \ Desktop \ 2.jpg"); // Add a watermark BufferedImage watermarkImage = ImageIO to the image. read (new File ("C: \ Users \ HONGLINCHEN \ Desktop \ 1.jpg"); // The first parameter is the watermark position, and the second parameter is the cached data of the watermark image; the third parameter is transparency. Thumbnails. of ("C: \ Users \ HONGLINCHEN \ Desktop \ 23.jpg "). scaling (0.8 ). watermark (Positions. BOTTOM_RIGHT, watermarkImage, 0.5f ). toFile ("C: \ Users \ HONGLINCHEN \ Desktop \ 3.jpg ");}}
Github source code:Https://github.com/moyiblog/thumbnailator