图片裁剪缩略处理小工具类

最近在看有关于Java图片处理方面的东西,因为项目中要用到,所以之前就各种参考,其中引用了thumbnailator工具以及Java原生的图片处理工具类,效率上还是原生的较好,不过thumbnailator提供的功能较全面也较简单,需要注意的是thumbnailator处理png格式的图片时,如果遇到透明背景的话,会处理成黑色的背景,处理方法就是在使用thumbnailator处理png图像之前将其背景图设置为白色,这是最简单有效的办法,各位看官如果有其他行之有效的方法,欢迎随时留言
package com.*.util;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import net.coobird.thumbnailator.Thumbnailator;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

public class ImageUtils
{

	
	/**
	 * jpg文件格式
	 */
	public static String JPG = "jpg";
	
	/**
	 * png文件格式
	 */
	public static String PNG = "png";
	
	/**
	 * 图片裁剪工具
	 * 
	 * @param in
	 *            ******************输入流
	 * @param readImageFormat
	 *            *****图片格式
	 * @param x
	 *            *******************起始点x坐标
	 * @param y
	 *            *******************起始点y坐标
	 * @param w
	 *            *******************裁剪宽度
	 * @param h
	 *            *******************裁剪高度
	 * @return is*****************输出流
	 * @author Jieve
	 * @throws IOException
	 */
	public static InputStream cutImage(InputStream in, String readImageFormat, int x, int y, int w, int h) throws IOException
	{

		Iterator<ImageReader> iterator = ImageIO.getImageReadersByFormatName(readImageFormat);
		ImageReader reader = (ImageReader) iterator.next();
		ImageInputStream iis = ImageIO.createImageInputStream(in);
		reader.setInput(iis, true);
		ImageReadParam param = reader.getDefaultReadParam();
		Rectangle rect = new Rectangle(x, y, w, h);
		param.setSourceRegion(rect);
		BufferedImage bi = reader.read(0, param);
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		ImageIO.write(bi, readImageFormat, os);
		InputStream is = new ByteArrayInputStream(os.toByteArray());
		in.close();
		os.close();
		return is;
	}

	/**
	 * 根据图片对象获取对应InputStream
	 * 
	 * @param image
	 * @param readImageFormat
	 * @return
	 * @throws IOException
	 */
	public static InputStream getInputStream(BufferedImage image, String readImageFormat) throws IOException
	{

		ByteArrayOutputStream os = new ByteArrayOutputStream();
		ImageIO.write(image, readImageFormat, os);
		InputStream is = new ByteArrayInputStream(os.toByteArray());
		os.close();
		return is;
	}
	
	/**
	 * 生成缩略图
	 * 
	 * @param in
	 * @param size
	 * @param imageFormat
	 * @return
	 * @throws IOException
	 */
	public static InputStream createThumbnails(InputStream in, int size, String imageFormat) throws IOException
	{
		if(imageFormat.equalsIgnoreCase("jpg"))
		{
			
			return createThumbnails(in, size);
		}
		else if(imageFormat.equalsIgnoreCase("png"))
		{
		BufferedImage image = Thumbnails.of(in).scale(1f).outputFormat(JPG).asBufferedImage();
		InputStream is = getInputStream(image, JPG);
		return createThumbnails(is, size);
		}
		return null;
	}
	
	/**
	 * 转化为jpg格式
	 * 
	 * @param in
	 * @param imageFormat
	 * @return
	 * @throws IOException
	 */
	public static BufferedImage toJPG(BufferedImage image) throws IOException{
		
		int width = image.getWidth();
	    int height = image.getHeight();
	    BufferedImage image_ = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	    Graphics2D graphic = image_.createGraphics();
	    graphic.setColor(Color.WHITE);
	    graphic.fillRect(0,0,width,height);
	    graphic.drawRenderedImage(image, null);
	    graphic.dispose();
	    return image_;
	}

	/**
	 * 生成缩略图
	 * 
	 * @param in
	 * @param width
	 * @param height
	 * @return
	 * @throws IOException
	 */
	public static InputStream createThumbnails(InputStream in, int width, int height) throws IOException
	{

		ByteArrayOutputStream os = new ByteArrayOutputStream();
		Thumbnailator.createThumbnail(in, os, width, height);
		InputStream is = new ByteArrayInputStream(os.toByteArray());
		in.close();
		os.close();
		return is;
	}

	/**
	 * 生成缩略图
	 * 
	 * @param in
	 * @param size
	 * @return
	 * @throws IOException
	 */
	public static InputStream createThumbnails(InputStream in, int size) throws IOException
	{

		ByteArrayOutputStream os = new ByteArrayOutputStream();
		Thumbnailator.createThumbnail(in, os, size, size);
		InputStream is = new ByteArrayInputStream(os.toByteArray());
		in.close();
		os.close();
		return is;
	}

	/**
	 * 添加水印
	 * 
	 * @param in
	 * @param width
	 * @param height
	 * @return
	 * @throws IOException
	 */
	public static InputStream addWaterMark(InputStream in, InputStream waterMark, int width, int height) throws IOException
	{

		ByteArrayOutputStream os = new ByteArrayOutputStream();
		Thumbnails.of(in).size(width, height).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(waterMark), 0.4f).outputQuality(0.8f).toOutputStream(os);
		InputStream is = new ByteArrayInputStream(os.toByteArray());
		in.close();
		waterMark.close();
		os.close();
		return is;
	}

	/**
	 * 居中裁剪
	 * 
	 * @param image
	 * @return
	 */
	public static BufferedImage clipCenter(BufferedImage image)
	{

		int height = image.getHeight();
		int width = image.getWidth();
		int size = height >= width ? width : height;
		int temp = 0;
		if(height >= width)
		{
			temp = (height - width) / 2;
			image = image.getSubimage(0, temp, size, size);
		}
		else
		{
			temp = (width - height) / 2;
			image = image.getSubimage(temp, 0, size, size);
		}

		return image;
	}

	/**
	 * 裁剪图片
	 * 
	 * @param image
	 * @param x
	 * @param y
	 * @param size
	 * @return
	 */
	public static BufferedImage cutImage(BufferedImage image, int x, int y, int size)
	{

		int height = image.getHeight();
		int width = image.getWidth();
		if((width >= (x + size)) && (height >= (y + size)))
		{
			image = image.getSubimage(x, y, size, size);
		}
		else
		{
			int temp = ((height - y) >= (width - x)) ? (width - x) : (height - y);
			image = image.getSubimage(x, y, temp, temp);
		}

		return image;
	}

	/**
	 * 检查格式是否合法
	 * 
	 * @param imageType
	 * @return
	 */
	public static boolean checkType(String imageType)
	{

		boolean flag = false;
		if(JPG.equalsIgnoreCase(imageType) || PNG.equalsIgnoreCase(imageType))
		{
			flag = true;
		}
		return flag;
	}

	/**
	 * 压缩图片
	 * 默认输出50%质量图片
	 * 
	 * @param image
	 * @return
	 * @throws IOException
	 */
	public static InputStream compress(BufferedImage image, String readImageFormat) throws IOException
	{

		InputStream in = ImageUtils.getInputStream(image, readImageFormat);
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		Thumbnails.of(in).size(image.getWidth(), image.getHeight()).outputQuality(0.5f).toOutputStream(os);
		InputStream is = new ByteArrayInputStream(os.toByteArray());
		in.close();
		os.close();
		return is;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值