使用方法:

在pom中加入以下jar包

<!-- 图片缩略图 图片压缩 水印 start-->

<dependency>
<
groupId>net.coobird</groupId>
<
artifactId>thumbnailator</artifactId>
<
version>0.4.8</version>
</
dependency><!-- 图片缩略图 图片压缩 水印 end-->

 

然后压缩和水印 只需要一行代码搞定

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 
2017/10/31 11:00* 图片压缩* @author HONGLINCHEN* @since 
JDK 1.8*/
public class ImgCompress {
public static void main(String[] args) throws IOException {
//压缩图片 第一个参数是原图路径 后面那个路径是压缩以后的输出路径Thumbnails.of("C:\Users\HONGLINCHEN\Desktop\23.jpg").size(6
00,600).outputQuality(0.8f).toFile("C:\Users\HONGLINCHEN\Desk
top\2.jpg");
//给图片加水印
BufferedImage watermarkImage = ImageIO.read(new File("C:\Users\HONGLINCHEN\Desktop\1.jpg"));
//第一个参数是水印的位置;第二个参数是水印图片的缓存数据;第三个参数是透明度。
Thumbnails.of("C:\Users\HONGLINCHEN\Desktop\23.jpg").scale(
0.8).watermark(Positions.BOTTOM_RIGHT, watermarkImage, 
0.5f).toFile("C:\Users\HONGLINCHEN\Desktop\3.jpg");}}

github源码:https://github.com/moyiblog/thumbnailator

相关文章:

  • 2022-01-26
  • 2022-02-09
  • 2022-12-23
  • 2022-01-01
  • 2022-01-29
猜你喜欢
  • 2022-12-23
  • 2021-06-02
  • 2021-12-10
  • 2022-02-15
  • 2021-11-30
相关资源
相似解决方案