lee 1213 0 2017-06-01 19:06:06

文章位置:左搜> 编程技术> 正文
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();
		}
	}
}