Thumbnailator图片处理(图片缩放,区域裁剪,水印,旋转,保持比例) 您所在的位置:网站首页 thumbnailator处理速度 Thumbnailator图片处理(图片缩放,区域裁剪,水印,旋转,保持比例)

Thumbnailator图片处理(图片缩放,区域裁剪,水印,旋转,保持比例)

2023-08-07 07:40| 来源: 网络整理| 查看: 265

Thumbnailator 是一个优秀的图片处理的Google开源Java类库。处理效果远比Java API的好。从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片生成处理后的图片,且允许微调图片的生成方式,同时保持了需要写入的最低限度的代码量。还支持对一个目录的所有图片进行批量处理操作。 jar包下载:https://pan.baidu.com/s/1P29mvsCqeL_R05DBM0EkkQ 提取码:hfgi 1.按照比例进行缩放:

//原始图片的路径 String imgPath = "F:/file/1.png"; //压缩后图片的路径 String smallImgPath = imgPath.replace(".", "_small."); try { Thumbnails.of(imgPath) //scale是比例,值在0到1之间,1f就是原图大小 //0.5f就是原图的一半大小,这里的大小是指图片的长宽。 .scale(0.5f) //outputQuality是图片的质量,值也是在0到1,越接近于1质量越好,越接近于0质量越差。 .outputQuality(0.1) //.toFile图片处理后路径 .toFile(smallImgPath); } catch (IOException e) { e.printStackTrace(); }

2.指定大小进行缩放:

//原始图片的路径 String imgPath = "F:/file/1.png"; //压缩后图片的路径 String smallImgPath = imgPath.replace(".", "_small."); try { Thumbnails.of(imgPath) //.size是宽度、高度 .size(200, 300) .toFile(smallImgPath); } catch (IOException e) { e.printStackTrace(); }

3.不按照比例,指定大小进行缩放:

//原始图片路径 String imgPath = "F:/file/1.png"; //压缩图片路径 String smallImgPath = imgPath.replace(".", "_small."); try { //keepAspectRatio(false)默认是按照比例缩放的 Thumbnails.of(imgPath) .size(200,200) .keepAspectRatio(false) .toFile(smallImgPath); } catch (IOException e) { e.printStackTrace(); }

4.旋转:

//原始图片路径 String imgPath = "F:/file/1.png"; //压缩图片路径 String smallImgPath = imgPath.replace(".", "_small."); try { //rotate(角度),正数:顺时针,负数:逆时针 Thumbnails.of(imgPath) .size(1280,1024) .rotate(90) .toFile(smallImgPath); } catch (IOException e) { e.printStackTrace(); }

5.水印:

//原始图片路径 String imgPath = "F:/file/1.png"; //压缩图片路径 String smallImgPath = imgPath.replace(".", "_small."); //水印图位置 File file= new File("e:/demo/demoNext" + File.separator + "yin.png") ; try { //watermark(位置,水印图,透明度) Thumbnails.of(imgPath) .size(1280,1024) .watermark(Positions.BOTTOM_RIGHT,ImageIO.read(file),0.5f) .outputQuality(0.5f) .toFile(smallImgPath); } catch (IOException e) { e.printStackTrace(); }

6.剪裁:

//原始图片路径 String imgPath = "F:/file/1.png"; //压缩图片路径 String smallImgPath = imgPath.replace(".", "_small."); try { //sourceRegion()指定位置 //图片中心400*400的区域 Thumbnails.of(imgPath) .sourceRegion(Positions.CENTER,400,400) .size(200,200) .keepAspectRatio(false) .toFile(smallImgPath); //图片右下400*400的区域 Thumbnails.of(imgPath) .sourceRegion(Positions.BOTTOM_RIGHT,400,400) .size(200,200) .keepAspectRatio(false) .toFile(smallImgPath); //指定坐标 Thumbnails.of(imgPath) .sourceRegion(600,500,400,400) .size(200,200) .keepAspectRatio(false) .toFile(smallImgPath); } catch (IOException e) { e.printStackTrace(); }

7.转换图像格式:

//原始图片路径 String imgPath = "F:/file/1.png"; //压缩图片路径 String smallImgPath = imgPath.replace(".", "_small."); File file= new File("e:/demo/demoNext" + File.separator + "yin.png") ; try { Thumbnails.of(imgPath) .size(1280,1024) //outputFormat(图像格式) .outputFormat("gif") .toFile(smallImgPath); } catch (IOException e) { e.printStackTrace(); }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有