Java 图片剪切、水印、旋转、格式转换

本文详细介绍了如何使用Java进行图片处理,包括图片的剪切、自由角度旋转、添加文字水印以及转换图片格式等操作,适合Java开发者学习和参考。
摘要由CSDN通过智能技术生成

Java 图片剪切、水印、旋转、格式转换


首先对于刚刚接触Java的朋友们,图片的处理可能是一个难点,因为可能大家在之前的培训或者工作经历中没有接触过相似的需求,这里我为大家总结了一些关于图片处理的一些代码。

<!-- 首先我们使用了net.coobird.thumbnailator的jar包 -->
<dependency>
	<groupId>net.coobird</groupId>
	<artifactId>thumbnailator</artifactId>
	<version>0.4.8</version>
</dependency>

/**
 * TODO:在这里我是将这个图片处理写成工具类的形式,方便以后使用的时候方便调用
 * ImgUtils
 * @author Mr.Zhang  
 * @date 2019年10月8日  
 * @version 1.0
 */
public class ImgUtils {
	/**
	 * @ClassName SpecifiedSize 指定图片大小进行剪切修改
	 * @param imgPath 需要修改的图片路径&nbsp;(此处需要加上图片名称。例: C:/img/SpecifiedSize.png)
	 * @param outPath 输出到指定位置&nbsp;(此处需要加上图片名称。例: C:/img/SpecifiedSize.png)
	 * @param height 指定图片高度
	 * @param width 指定图片宽度
	 * @throws IOException
	 */
	private void SpecifiedSize(String imgPath,String outPath,int height,int width) throws IOException {
		/*
		 * size(width,height) 若图片横比width小,高比height小,不变 若图片横比width小,高比height大,高缩小到height,图片比例不变
		 * 若图片横比width大,高比height小,横缩小到width,图片比例不变 若图片横比width大,高比height大,图片按比例缩小,横为width或高为height
		 */
		Thumbnails.of(imgPath).size(width, height).toFile(outPath);
	}

	/**
	 * @ClassName proportion 按照比例进行缩放
	 * @param imgPath 需要修改的图片路径&nbsp;(此处需要加上图片名称。例: C:/img/SpecifiedSize.png)
	 * @param outPath 输出到指定位置&nbsp;(此处需要加上图片名称。例: C:/img/SpecifiedSize.png)
	 * @param size double类型,等比例缩小的百分比
	 * @throws IOException
	 */
	private void Proportion(String imgPath,String outPath,double size) throws IOException {
		Thumbnails.of(imgPath).scale(size).toFile(outPath);
	}

	/**
	 * @ClassName SpecifiedNoProportion 不按照比例,指定大小进行缩放
	 * @param imgPath 需要修改的图片路径&nbsp;(此处需要加上图片名称。例: C:/img/SpecifiedSize.png)
	 * @param outPath 输出到指定位置&nbsp;(此处需要加上图片名称。例: C:/img/SpecifiedSize.png)
	 * @param height 指定图片高度
	 * @param width 指定图片宽度
	 * @throws IOException
	 */
	private void SpecifiedNoProportion(String imgPath,String outPath,int height,int width) throws IOException {
		/**
		 * keepAspectRatio(false) 默认是按照比例缩放的
		 */
		Thumbnails.of(imgPath).size(width, height).keepAspectRatio(false).toFile(outPath);
	}

	/**
	 * @ClassName RotationScaling 旋转等比例缩放
	 * @param imgPath 需要修改的图片路径&nbsp;(此处需要加上图片名称。例: C:/img/SpecifiedSize.png)
	 * @param outPath 输出到指定位置&nbsp;(此处需要加上图片名称。例: C:/img/SpecifiedSize.png)
	 * @param size double类型,等比例缩小的百分比(1.00是不缩放)
	 * @param rotate(角度),正数:顺时针 负数:逆时针
	 * @throws IOException
	 */
	private void RotationScaling(String imgPath,String outPath,double size,int rotate) throws IOException {
		/**
		 * rotate(角度),正数:顺时针 负数:逆时针
		 */
		Thumbnails.of(imgPath).scale(size).rotate(rotate).toFile(outPath);
	}

	/**
	 * @ClassName WaterMark 给图片加水印
	 * @param imgPath 需要修改的图片路径&nbsp;(此处需要加上图片名称。例: C:/img/SpecifiedSize.png)
	 * @param outPath 输出到指定位置&nbsp;(此处需要加上图片名称。例: C:/img/SpecifiedSize.png)
	 * @param waterMarkPath 水印图片的位置&nbsp;(此处需要加上图片名称。例: C:/img/SpecifiedSize.png)
	 * @param size double类型,等比例缩小的百分比(1.00是不缩放)
	 * @param Transparency 透明度
	 * @throws IOException
	 */
	private void WaterMark(String imgPath,String outPath,String waterMarkPath,double size,float Transparency,Positions Positions) throws IOException {
		/**
		 * watermark(位置,水印图,透明度)
		 */
		Thumbnails.of(imgPath).scale(size)
				.watermark(Positions, ImageIO.read(new File(waterMarkPath)), Transparency).outputQuality(0.8f)
				.toFile(outPath);
	}

//	/**
//	 * @ClassName imgTailoring 裁剪
//	 * @throws IOException
//	 */
//	private void imgTailoring() throws IOException {
//		/**
//		 * 图片中心400*400的区域
//		 */
//		Thumbnails.of("images/test.jpg").sourceRegion(Positions.CENTER, 400, 400).size(200, 200).keepAspectRatio(false)
//				.toFile("F:/img/image_region_center.jpg");
//		/**
//		 * 图片右下400*400的区域
//		 */
//		Thumbnails.of("images/test.jpg").sourceRegion(Positions.BOTTOM_RIGHT, 400, 400).size(200, 200)
//				.keepAspectRatio(false).toFile("F:/img/image_region_bootom_right.jpg");
//		/**
//		 * 指定坐标
//		 */
//		Thumbnails.of("images/test.jpg").sourceRegion(600, 500, 400, 400).size(200, 200).keepAspectRatio(false)
//				.toFile("F:/img/image_region_coord.jpg");
//	}
	
	/**
	 * @ClassName imgFormart 转换图片格式
	 * @param imgPath 需要转换图片的路径
	 * @param outPath 输出已转换的路径
	 * @param Suffix 需要转换的后缀名
	 * @throws IOException
	 */
	private void imgFormart(String imgPath,String outPath,String Suffix) throws IOException {
		/**
		 * outputFormat(图像格式)
		 */
		Thumbnails.of(imgPath).scale(1.00).outputFormat(Suffix).toFile(outPath);
	}

	/**
	 * @ClassName outputStream 输出到OutputStream
	 * @param imgPath 需要转换图片的路径
	 * @param outPath 输出已转换的路径
	 * @param Suffix 需要转换的后缀名
	 * @throws IOException
	 */
	private void outputStream(String imgPath,String outPath) throws IOException {
		/**
		 * toOutputStream(流对象)
		 */
		OutputStream os = new FileOutputStream(outPath);
		Thumbnails.of(imgPath).scale(1.00).toOutputStream(os);
	}

	/**
	 * @ClassName bufferedImage 输出到BufferedImage
	 * @param imgPath 需要转换图片的路径
	 * @param outPath 输出已转换的路径
	 * @param Suffix 需要转换的后缀名
	 * @throws IOException
	 */
	private void bufferedImage(String imgPath,String outPath,String Suffix) throws IOException {
		/**
		 * asBufferedImage() 返回BufferedImage
		 */
		BufferedImage thumbnail = Thumbnails.of(imgPath).scale(1.00).asBufferedImage();
		ImageIO.write(thumbnail, Suffix, new File(outPath));
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值