All Downloads are FREE. Search and download functionalities are using the official Maven repository.

matrix.boot.common.utils.ImageUtil Maven / Gradle / Ivy

There is a newer version: 2.1.11
Show newest version
package matrix.boot.common.utils;

import matrix.boot.common.encrypt.Base64;
import matrix.boot.common.exception.ServiceException;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;

/**
 * 图片工具
 * @author wangcheng
 * 2021/8/10
 */
public class ImageUtil {

	/**
	 * 文件路径
	 */
	private String filePath;

	private ImageUtil() {}

	private ImageUtil(String filePath) {
		this.filePath = filePath;
	}

	/**
	 * 实例化
	 * @param filePath 文件路径
	 * @return 实例
	 */
	public static ImageUtil getInstance(String filePath) {
		return new ImageUtil(filePath);
	}

	/**
	 * 获取图片
	 *
	 * @param fileName 文件名
	 * @return 图片缓冲区
	 */
	public BufferedImage getBufferedImage(String fileName) {
		AssertUtil.notNullTip(fileName, "fileName");
		try {
			return ImageIO.read(new File(filePath, fileName));
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	/**
	 * 裁剪图片
	 *
	 * @param fileName 文件名
	 * @param x 开始x坐标
	 * @param y 开始y坐标
	 * @param width 宽度
	 * @param height 高度
	 * @return 新文件名
	 */
	public String cutImage(String fileName, int x, int y, int width, int height) {
		FileInputStream fis = null;
		ImageInputStream iis = null;
		try {
			String suffix = FileUtil.getSuffix(fileName);
			String type = suffix.replace(".", "");
			String result = RandomUtil.getUUID() + suffix;
			fis = new FileInputStream(new File(filePath, fileName));
			Iterator iterator = ImageIO.getImageReadersByFormatName(type);
			ImageReader reader = iterator.next();
			iis = ImageIO.createImageInputStream(fis);
			reader.setInput(iis, true);
			ImageReadParam imageReadParam = reader.getDefaultReadParam();
			imageReadParam.setSourceRegion(new Rectangle(x, y, width, height));
			BufferedImage bi = reader.read(0, imageReadParam);
			ImageIO.write(bi, type, new File(filePath, result));
			return result;
		} catch (Exception e) {
			throw new ServiceException(e);
		} finally {
			BIOStreamUtil.closeStream(iis);
			BIOStreamUtil.closeStream(fis);
		}
	}

	/**
	 * 缩放图片
	 *
	 * @param fileName 文件名
	 * @param width 宽度
	 * @param height 高度
	 * @return 新文件名
	 */
	public String zoomImage(String fileName, Integer width, Integer height) {
		AssertUtil.notNullTip(fileName, "fileName");
		AssertUtil.state(width != null || height != null, "width or height don't null");
		BufferedImage bufferedImage = this.getBufferedImage(fileName);
		if (width == null) {
			width = getAdaptiveWidth(bufferedImage, height);
		}
		if (height == null) {
			height = getAdaptiveHeight(bufferedImage, width);
		}
		FileOutputStream fos = null;
		try {
			String suffix = FileUtil.getSuffix(fileName);
			String type = suffix.replace(".", "");
			String result = RandomUtil.getUUID() + suffix;
			fos = new FileOutputStream(new File(filePath, result));
			double widthRate = 0, heightRate = 0;
			widthRate = width * 1.0 / bufferedImage.getWidth();
			heightRate = height * 1.0 / bufferedImage.getHeight();
			AffineTransformOp transformOp = new AffineTransformOp(AffineTransform.getScaleInstance(widthRate, heightRate), null);
			BufferedImage image = transformOp.filter(bufferedImage, null);
			ImageIO.write(image, type, fos);
			return result;
		} catch (Exception e) {
			throw new ServiceException(e);
		} finally {
			BIOStreamUtil.closeStream(fos);
		}
	}

	/**
	 * 获取自适应宽度
	 * @param image 图片缓存区
	 * @param height 高度
	 * @return 宽度
	 */
	private Integer getAdaptiveWidth(BufferedImage image, Integer height) {
		return Double.valueOf(height * 1.0 / image.getHeight() * image.getWidth()).intValue();
	}

	/**
	 * 获取自适应高度
	 * @param image 图片缓存区
	 * @param width 宽度
	 * @return 高度
	 */
	private Integer getAdaptiveHeight(BufferedImage image, Integer width) {
		return Double.valueOf(width * 1.0 / image.getWidth() * image.getHeight()).intValue();
	}

	/**
	 * 获取图片的base64编码
	 * @param image 图片缓冲区
	 * @return base64图片
	 */
	public static String getImageBase64(BufferedImage image) {
		byte[] bytes;
		ByteArrayOutputStream byteArrayOutputStream = null;
		try {
			byteArrayOutputStream = new ByteArrayOutputStream();
			ImageIO.write(image, "png", byteArrayOutputStream);
			bytes = byteArrayOutputStream.toByteArray();
		} catch (Exception e) {
			throw new ServiceException(e);
		} finally {
			BIOStreamUtil.closeStream(byteArrayOutputStream);
		}
		return "data:image/png;base64," + Base64.encrypt(bytes);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy