因为项目需要做一个图片的微服务,用到了mongoDB,springboot,springCloud,等。其中因为需要对图片做处理。查看资料看到Thumbnails,于是,对Thumbnails的功能做了一些简单的使用。
这是两张原图,及路径。
public static void test2() throws IOException{
File file = new File("E:/图像/juan_/test_s/juan_yts1.jpg");
// 指定坐标
Thumbnails.of(file).sourceRegion(3850, 1500, 600, 2000).size(
600, 2000).keepAspectRatio(false).toFile(
"E:/图像/juan_/test_c/juan_backup20.jpg");
}
这是裁剪过后的图片
private static void test1() throws FileNotFoundException, IOException{
File file = new File("E:/图像/juan_/test_s/xifu_66.jpg");
//scale0.3是一个double类型的数字,缩放比例,大于1就是变大,小于1就是缩小
//Thumbnails.of(new FileInputStream(file)).scale(0.3).toFile(new File("E:/图像/juan_/juan_buck.jpg"));
//按照比例进行缩放
Thumbnails.of(file).scale(0.25f).toFile("E:/图像/juan_/test_c/juan_backup1.jpg");
Thumbnails.of(file).scale(1.10f).toFile("E:/图像/juan_/test_c/juan_backup2.jpg");
//指定大小进行缩放
Thumbnails.of(file).size(200, 300).toFile("E:/图像/juan_/test_c/juan_backup3.jpg");
Thumbnails.of(file).size(2560, 2048).toFile("E:/图像/juan_/test_c/juan_backup4.jpg");
//旋转
Thumbnails.of(file).size(1280, 1024).rotate(90).toFile("E:/图像/juan_/test_c/juan_backup5.jpg");
Thumbnails.of(file).size(1280, 1024).rotate(-90).toFile("E:/图像/juan_/test_c/juan_backup6.jpg");
/**
* 不按照比例,指定大小进行缩放
* keepAspectRatio(false) 默认是按照比例缩放的
*/
Thumbnails.of(file).size(120, 120).keepAspectRatio(false).toFile("E:/图像/juan_/test_c/juan_backup7.jpg");
// 水印 watermark(位置,水印图,透明度)
Thumbnails.of(file).size(1280, 1024).watermark(Positions.BOTTOM_RIGHT,
ImageIO.read(new File("E:/图像/juan_/test_c/juan_backup20.jpg")), 0.5f)
.outputQuality(0.8f).toFile("E:/图像/juan_/test_c/juan_backup8.jpg");
Thumbnails.of(file).size(1280, 1024).watermark(
Positions.CENTER,
ImageIO.read(new File("E:/图像/juan_/test_c/juan_backup20.jpg")), 0.5f)
.outputQuality(0.8f).toFile("E:/图像/juan_/test_c/juan_backup9.jpg");
//裁剪
// 图片中心400*400的区域
Thumbnails.of(file).sourceRegion(Positions.CENTER, 400,
400).size(200, 200).keepAspectRatio(false).toFile(
"E:/图像/juan_/test_c/juan_backup10.jpg");
// 图片右下400*400的区域
Thumbnails.of(file).sourceRegion(Positions.BOTTOM_RIGHT,
400, 400).size(200, 200).keepAspectRatio(false).toFile(
"E:/图像/juan_/test_c/juan_backup11.jpg");
// 指定坐标
Thumbnails.of(file).sourceRegion(600, 500, 400, 400).size(
200, 200).keepAspectRatio(false).toFile(
"E:/图像/juan_/test_c/juan_backup12.jpg");
//转化图像格式
/**
* outputFormat(图像格式)
*/
Thumbnails.of(file).size(1280, 1024).outputFormat("jpeg")
.toFile("E:/图像/juan_/test_c/juan_backup13.jpeg");
Thumbnails.of(file).size(1280, 1024).outputFormat("gif")
.toFile("E:/图像/juan_/test_c/juan_backup14.gif");
Thumbnails.of(file).size(1280, 1024).outputFormat("jpg")
.toFile("E:/图像/juan_/test_c/juan_backup15.jpg");
Thumbnails.of(file).size(1280, 1024).outputFormat("bmp")
.toFile("E:/图像/juan_/test_c/juan_backup16.bmp");
//输出到OutputStream
//toOutputStream(流对象)
OutputStream os = new FileOutputStream(
"E:/图像/juan_/test_c/juan_backup17.jpg");
Thumbnails.of(file).size(1280, 1024).toOutputStream(os);
// 输出到BufferedImage
/**
* asBufferedImage() 返回BufferedImage
*/
BufferedImage thumbnail = Thumbnails.of(file).size(1280,
1024).asBufferedImage();
ImageIO.write(thumbnail, "jpg", new File(
"E:/图像/juan_/test_c/juan_backup18.jpg"));
}
水印
package test;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
import net.coobird.thumbnailator.geometry.Positions;
public class LiunnTest {
private static final float PIC_SIZE = 500 * 1024;
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO Auto-generated method stub
test1();
}