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

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();
				}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜鸟驿站ㅤ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值