Thumbnailator图片处理工具的使用

Thumbnailator 是一个优秀的图片处理的Google开源Java类库。处理效果远比Java API的好。它支持的操作有如下:图片缩放,区域裁剪,水印,旋转,保持比例

- 图片缩放

按指定大小缩放(遵循原图高宽比例)

Thumbnails.of("images/a.jpg")  
    .size(200, 300)  //size(width, height)
    .toFile("c:/a_new.jpg");  //输出新文件

按比例缩放

Thumbnails.of("images/a.jpg")  
    .scale(0.25f)  //0~1.00f
    .toFile("c:/a_new.jpg");  

按指定大小缩放(不遵循原图高宽比例)

Thumbnails.of("images/a.jpg")  
    .size(300, 300)  //size(width, height)
    .keepAspectRatio(false)
    .toFile("c:/a_new.jpg");  //输出新文件

- 旋转
rotate

Thumbnails.of("images/a.jpg")  
       .size(1280,1024)  
       .rotate(90)  //rotate(角度),正数:顺时针负数:逆时针  
       .toFile("c:/a_rotate_90.jpg");  

- 水印
.watermark(位置,水印图,透明度)

 Thumbnails.of("images/a.jpg")  
    .size(1280,1024)  
    .watermark(Positions.BOTTOM_RIGHT,ImageIO.read(newFile("images/watermark.png")),0.5f)  
    .outputQuality(0.8f)  //压缩图片文件大小,参数1为最高质量
    .toFile("c:/a_watermark_bottom_right.jpg");  

- 裁剪
指定图片区域

 //图片中心300*300的区域  
Thumbnails.of("images/a.jpg")  
    .sourceRegion(Positions.CENTER,300,300)  
    .size(200,200)  
    .keepAspectRatio(false)  
    .toFile("c:/a_region_center.jpg"); 



//图片右下300*300的区域  
Thumbnails.of("images/a.jpg")  
    .sourceRegion(Positions.BOTTOM_RIGHT,300,300)  
    .size(200,200)  
    .keepAspectRatio(false)  
    .toFile("c:/a_region_bootom_right.jpg");  

指定坐标

Thumbnails.of("images/a.jpg")  
    .sourceRegion(400,400,200,150)  
    .size(200,200)  
    .keepAspectRatio(false)  
    .toFile("c:/a_region_coord.jpg"); 

- 转换图片格式
outputFormat(图像格式)

 Thumbnails.of("images/a.jpg")  
    .size(1280,1024)  
    .outputFormat("png")  
    .toFile("c:/a_jpgtopng.png");

- 输出文件流OutputStream
toOutputStream(流对象)

OutputStreamos=newFileOutputStream("c:/a.png");  
Thumbnails.of("images/a_1080x1024.jpg")  
    .size(1080,1024)  
    .toOutputStream(os);  

- 输出文件流BufferedImage
asBufferedImage()返回BufferedImage

BufferedImagethumbnail=Thumbnails.of("images/a_1080x1024.jpg")  
    .size(1080,1024)  
    .asBufferedImage();  
ImageIO.write(thumbnail,"jpg",newFile("c:/a_1080x1024_BufferedImage.jpg"));
  • **

实战使用

**
到https://mvnrepository.com/artifact/net.coobird/thumbnailator/0.4.8复制依赖到项目pom.xml

  public class ImageUtil {
        private static String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath(); //获取路径

    public static void main(String[] args) throws IOException {

        //获取到的路径的“空格”处理
        basePath= URLDecoder.decode(basePath,"utf-8");
        
        Thumbnails.of(new File("E:/photo/psb.jpg"))
        .size(200, 200)//按大小进行缩放
        .watermark(Positions.BOTTOM_RIGHT,
                ImageIO.read(new File(basePath+"/watermark.jpg")), 0.25f)
        .outputQuality(0.8f)
        .toFile("E:/photo/psb_new.jpg");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值