java图片添加水印logo/大小缩放工具类

位置:首页>文章>详情   分类: 教程分享 > Java教程   阅读(1121)   2023-03-28 11:29:14
Java中图片常用处理工具类

功能说明:
1.指定图片大小缩放图片(reSize);
2.指定图片的宽度,高度根据比例缩放(reSizeByWith);
3.指定图片的高度,宽度根据比例缩放(reSizeByHeight);
4.判断是否为图片文件(isImageFile);
5.判断图片格式是否支持(isSupportFormat);
6.给图片添加水印(markLogo);

支持的图片格式: JPG,BMP,GIF,WBMP,PNG,JPEG
package net.xqlee.project.demo.fastdfs.clients;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;

import javax.imageio.ImageIO;

import org.springframework.util.StringUtils;

/**
 * <pre>
 * Java中图片常用处理工具类
 * 功能说明:
 * 1.指定图片大小缩放图片(reSize);
 * 2.指定图片的宽度,高度根据比例缩放(reSizeByWith);
 * 3.指定图片的高度,宽度根据比例缩放(reSizeByHeight);
 * 4.判断是否为图片文件(isImageFile);
 * 5.判断图片格式是否支持(isSupportFormat);
 * 6.给图片添加水印(markLogo);
 * 支持的图片格式:
 * JPG,BMP,GIF,WBMP,PNG,JPEG
 * </pre>
 * 
 * @author xq
 *
 */
public class ImageUtils {
	/****
	 * 判断是否为图片文件,如果是图片文件则返回BufferedImage对象,反之则返回null
	 * 
	 * @param imageFile
	 *            图片文件
	 * @return BufferedImage
	 * @throws IOException
	 *             IO异常
	 */
	public static BufferedImage isImageFile(File imageFile) throws IOException {
		BufferedImage image = isImageFile(new FileInputStream(imageFile));
		return image;
	}

	/****
	 * 判断是否为图片文件,如果是图片文件则返回BufferedImage对象,反之则返回null
	 * 
	 * @param imageFileInputStream
	 *            图片文件的输入流
	 * @return BufferedImage
	 * @throws IOException
	 *             IO异常
	 */
	public static BufferedImage isImageFile(InputStream imageFileInputStream) throws IOException {
		BufferedImage image = ImageIO.read(imageFileInputStream);
		return image;
	}

	/**
	 * 图片格式是否支持
	 * 
	 * @param formatName
	 *            图片格式名称
	 * @return 是否支持
	 */
	public static boolean isSupportFormat(String formatName) {
		if (StringUtils.isEmpty(formatName)) {
			return false;
		}
		String supportTypes = Arrays.toString(ImageIO.getReaderFormatNames());
		if (supportTypes.indexOf(formatName) == -1) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 重置图片到指定的宽和高
	 * 
	 * @param imageFile
	 * @param imageFileOutputStream
	 * @param formatName
	 * @param newWith
	 * @param newHeight
	 * @throws IOException
	 */
	public static void reSize(InputStream imageFile, OutputStream imageFileOutputStream, String formatName, int newWith,
			int newHeight) throws IOException {
		// 1.判断文件格式是否支持
		if (!isSupportFormat(formatName)) {
			throw new IOException(
					"Format Not Support:" + formatName + ",Support" + Arrays.toString(ImageIO.getReaderFormatNames()));
		}
		// 1.判断是否为图片文件
		BufferedImage bufferedImage = isImageFile(imageFile);
		if (bufferedImage == null) {
			throw new IOException("Input File Is Not Image File.");
		}
		// 2.调整到指定宽高
		BufferedImage newImage = new BufferedImage(newWith, newHeight, bufferedImage.getType());

		// 3.执行重构画操作
		Graphics2D graphics = newImage.createGraphics();
		graphics.drawImage(bufferedImage, 0, 0, newWith, newHeight, Color.LIGHT_GRAY, null);
		graphics.dispose();

		// 4.输出缩略图片
		ImageIO.write(newImage, formatName, imageFileOutputStream);

	}

	/**
	 * 指定图片的宽,高根据比例压缩
	 * 
	 * @param imageFile
	 *            远图片
	 * @param imageFileOutputStream
	 *            新图片输出
	 * @param formatName
	 *            图片格式
	 * @param newWith
	 *            新的宽度
	 * @throws IOException
	 */
	public static void reSizeByWith(InputStream imageFile, OutputStream imageFileOutputStream, String formatName,
			int newWith) throws IOException {
		// 1.判断文件格式是否支持
		if (!isSupportFormat(formatName)) {
			throw new IOException(
					"Format Not Support:" + formatName + ",Support" + Arrays.toString(ImageIO.getReaderFormatNames()));
		}
		// 1.判断是否为图片文件
		BufferedImage bufferedImage = isImageFile(imageFile);
		if (bufferedImage == null) {
			throw new IOException("Input File Is Not Image File.");
		}
		// 2.调整到指定宽
		int oldWith = bufferedImage.getWidth();
		int oldHeigth = bufferedImage.getHeight();
		// 计算比例
		BigDecimal newWithBig = new BigDecimal(newWith);
		float ratio = newWithBig.divide(new BigDecimal(oldWith), 2, RoundingMode.HALF_UP).floatValue();
		int newHeight = (int) (oldHeigth * ratio);
		if (newHeight == 0) {// 太小的时候默认原始高度
			newHeight = oldHeigth;
		}
		BufferedImage newImage = new BufferedImage(newWith, newHeight, bufferedImage.getType());

		// 3.执行重构画操作
		Graphics2D graphics = newImage.createGraphics();
		graphics.drawImage(bufferedImage, 0, 0, newWith, newHeight, null);
		graphics.dispose();

		// 4.输出缩略图片
		ImageIO.write(newImage, formatName, imageFileOutputStream);
	}

	public static void reSizeByHeight(InputStream imageFile, OutputStream imageFileOutputStream, String formatName,
			int newHeight) throws IOException {
		// 1.判断文件格式是否支持
		if (!isSupportFormat(formatName)) {
			throw new IOException(
					"Format Not Support:" + formatName + ",Support" + Arrays.toString(ImageIO.getReaderFormatNames()));
		}
		// 1.判断是否为图片文件
		BufferedImage bufferedImage = isImageFile(imageFile);
		if (bufferedImage == null) {
			throw new IOException("Input File Is Not Image File.");
		}
		// 2.调整到指定宽
		int oldWith = bufferedImage.getWidth();
		int oldHeigth = bufferedImage.getHeight();
		// 计算比例
		BigDecimal newHeightBig = new BigDecimal(newHeight);
		float ratio = newHeightBig.divide(new BigDecimal(oldHeigth), 2, RoundingMode.HALF_UP).floatValue();
		int newWith = (int) (oldWith * ratio);
		if (newWith == 0) {// 太小的时候默认原始高度
			newWith = oldWith;
		}
		BufferedImage newImage = new BufferedImage(newWith, newHeight, bufferedImage.getType());

		// 3.执行重构画操作
		Graphics2D graphics = newImage.createGraphics();
		graphics.drawImage(bufferedImage, 0, 0, newWith, newHeight, null);
		graphics.dispose();

		// 4.输出缩略图片
		ImageIO.write(newImage, formatName, imageFileOutputStream);
	}

	/**
	 * 添加水印LOGO
	 * 
	 * @param targetImage
	 *            需要添加水印的原图
	 * @param logoImage
	 *            水印图片/水印Logo
	 * @param x
	 *            Logo存放的x轴(原图的左上角为0,0)
	 * @param y
	 *            Logo存放的y轴(原图的左上角为0,0)
	 * @throws IOException
	 *             IO异常
	 */
	public static void markLogo(BufferedImage targetImage, BufferedImage logoImage, int x, int y) {
		Graphics2D g = targetImage.createGraphics();
		g.drawImage(targetImage, 0, 0, targetImage.getWidth(), targetImage.getHeight(), null);
		g.drawImage(logoImage, x, y, logoImage.getWidth(), logoImage.getHeight(), null);
		g.dispose();
	}

	/**
	 * 添加水印LOGO,执行后新的图片将存放于targetImage里面
	 * 
	 * @param targetImage
	 *            需要添加水印的图片文件流
	 * @param targetImageFormat
	 *            图片格式
	 * @param logoImage
	 *            水印图片流/logo图片流
	 * @param x
	 *            Logo存放的x轴(原图的左上角为0,0)
	 * @param y
	 *            Logo存放的y轴(原图的左上角为0,0)
	 * @param imageFileOutputStream
	 *            最终图片文件输出流
	 * @throws IOException
	 *             IO异常需要捕获
	 */
	public static void markLogo(InputStream targetImage, String targetImageFormat, InputStream logoImage, int x, int y,
			OutputStream imageFileOutputStream) throws IOException {
		if (!isSupportFormat(targetImageFormat)) {
			throw new IOException("Image Format Not Support." + targetImageFormat + ",Support"
					+ Arrays.toString(ImageIO.getReaderFormatNames()));
		}
		BufferedImage targetImageBuffere = ImageIO.read(targetImage);
		BufferedImage logoImageBuffere = ImageIO.read(logoImage);
		markLogo(targetImageBuffere, logoImageBuffere, x, y);
		ImageIO.write(targetImageBuffere, targetImageFormat, imageFileOutputStream);
	}

	/**
	 * 添加水印LOGO
	 * 
	 * @param targetImage
	 *            需要添加logo的图片
	 * @param logoImage
	 *            logo图片
	 * @param x
	 *            Logo存放的x轴(原图的左上角为0,0)
	 * @param y
	 *            Logo存放的y轴(原图的左上角为0,0)
	 * @param imageFileOutputStream
	 *            最终图片文件输出流
	 * @throws IOException
	 *             IO异常需要捕获
	 */
	public static void markLogo(File targetImage, File logoImage, int x, int y, OutputStream imageFileOutputStream)
			throws IOException {
		String filename = targetImage.getName();
		String format = filename.substring(filename.indexOf(".") + 1).toUpperCase();
		markLogo(new FileInputStream(targetImage), format, new FileInputStream(logoImage), x, y, imageFileOutputStream);
	}

	public static void main(String[] args) {
		try {
			//@formatter:off
			// 重置大小测试
			//指定宽高 
//			reSize(
//					 new FileInputStream(new File("C:\\Users\\xq\\Pictures\\1.jpg")),
//					 new FileOutputStream(new File("C:\\Users\\xq\\Pictures\\1-t.jpg")), "jpg", 400, 400);
			 //指定宽
//			reSizeByWith(
//					new FileInputStream(new File("C:\\Users\\xq\\Pictures\\1.jpg")),
//					new FileOutputStream(new File("C:\\Users\\xq\\Pictures\\1-t.jpg")),
//					 "jpg", 400);
			
			//指定高度
//			reSizeByHeight(
//					new FileInputStream(new File("C:\\Users\\xq\\Pictures\\0.png")),
//					new FileOutputStream(new File("C:\\Users\\xq\\Pictures\\1-t-h.png")),
//					 "png", 400);
			 

			// logo水印测试
//			 markLogo(
//					 new FileInputStream(new File("C:\\Users\\xq\\Pictures\\0.jpg")), "JPG",
//					 new FileInputStream( new File("C:\\Users\\xq\\Pictures\\tx.jpg")), 0,0,
//					 new FileOutputStream(new File("C:\\Users\\xq\\Pictures\\1-tx.jpg")));
			markLogo(
					new File("C:\\Users\\xq\\Pictures\\0.png"), 
					new File("C:\\Users\\xq\\Pictures\\tx.jpg"), 0, 0,
					new FileOutputStream(new File("C:\\Users\\xq\\Pictures\\1-tx.png")));

			 System.out.println(Arrays.toString(ImageIO.getReaderFormatNames()));
			//@formatter:off
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
地址:https://www.leftso.com/article/176.html

相关阅读

功能说明:1.指定图片大小缩放图片(reSize);2.指定图片的宽度,高度根据比例缩放(reSizeByWith);3.指定图片的高度,宽度根据比例缩放(reSizeByHeight);4.判...
Java编程中处理图片文件与base64串的图片相互转化工具类,方便在编程中处理前端传递过来的base64图片。
java 图片 无损压缩。随着科技进步,大家手机拍的照片也从之前的几百KB变成几MB或者10MB了,有些情况我们需要压缩一下图片节省网络资源。这里将会讲解如何采用Java语音进行图片的无损压缩
Java编程中,很多报表系统需要导出Excel文件,并且某些时候需要导出一定的图形统计,其实就是将图片放在Excel中,下面讲解Java如何实现Excel图片编辑插入
java实现生成彩色背景图片验证码,在登陆和注册可以使用,工具类将图片的IO流通过response输出到浏览器,在浏览器中可以使用 img标签的src属性路径写上该工具类的访问路径即可。
java编程中通过easypoi导出excel文件并处理导出数据
在Java编程中,ORM框架mybaties处理@Select注解中的动态sql处理方法
SpringBoot使用@ResponseBody返回图片的实现以前使用HttpServletResponse可以通过输出流的方式来向前台输出图片
Java EE 8 JSON Pointer讲解,Java EE 8包含JSON处理API的更新,并为最新的JSON标准提供最新的IEFT标准。
CKEditor4.4整合SprngMVC实现上传图片