按质量、图片比例压缩
import java.io.File;
import java.io.FileOutputStream;
import net.coobird.thumbnailator.Thumbnails;
public class Client2 {
public static void main(String[] args) throws Exception {
Thumbnails.of(new File("D:/showqrcode.jpg"))
.scale(0.5f) //图片大小(长宽)压缩 从0按照
.outputQuality(0.5f) //图片质量压缩比例 从0-1,越接近1质量越好
.toOutputStream(new FileOutputStream("D:/showqrcode_50%.jpg"));
}
}
按图片大小压缩
import java.io.File;
import java.io.FileOutputStream;
import net.coobird.thumbnailator.Thumbnails;
public class Client22 {
public static void main(String[] args) throws Exception {
Thumbnails.of(new File("D:/showqrcode.jpg"))
.forceSize(500, 300) //不保持图片比例
.toOutputStream(new FileOutputStream("D:/showqrcode_500_300.jpg"));
}
}
旋转
import java.io.File;
import java.io.FileOutputStream;
import net.coobird.thumbnailator.Thumbnails;
public class Client3 {
public static void main(String[] args) throws Exception {
Thumbnails.of(new File("D:/showqrcode.jpg"))
.forceSize(500, 300)
.rotate(90f) //向右旋转
.toOutputStream(new FileOutputStream("D:/showqrcode+90.jpg"));
}
}
裁剪
import java.io.IOException;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
public class Client5 {
public static void main(String[] args) throws IOException {
Thumbnails.of("D:/showqrcode.jpg")
.sourceRegion(Positions.CENTER, 800, 600) //位置,宽,高
.size(800, 600)
.keepAspectRatio(false)
.toFile("D:/showqrcode_region_center.jpg");
}
}
加水印
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
public class Client4 {
public static void main(String[] args) throws IOException {
Thumbnails.of("D:/showqrcode.jpg")
.size(1280, 1024)
.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("D:/watermark.jpg")),
0.5f) //位置,水印来源,透明度
.outputQuality(0.8f)
.toFile("D:/showqrcode_watermark_bottom_right.jpg");
}
}