java 图片缩放,获取图片类型,和其他信息




import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.MemoryCacheImageInputStream;

/**
 * 图片工具类,使用iio,需要优化!
 * @author zuoge85
 *
 */
public final class ImageUtils {
	public static final String PNG="png";
	public static final String JPG="jpeg";
	public static final String BMP="bmp";
	public static final String GIF="gif";
	public static byte[] readFromFile(String path) throws IOException {
		InputStream is = new FileInputStream(new File(path));
		byte[] buf = new byte[is.available()];
		is.read(buf);
		is.close();
		return buf;
	}

	/**
	 * 构建一个image对象
	 * 
	 * @param img
	 * @return
	 * @throws IOException
	 */
	public static ImageInfo getImageInfo(byte[] img) throws IOException {
		ByteArrayInputStream bais = new ByteArrayInputStream(img);
		MemoryCacheImageInputStream is=new MemoryCacheImageInputStream(bais);
		Iterator<ImageReader> it=ImageIO.getImageReaders(is);
		ImageReader r=null;
		while(it.hasNext()){
			r=it.next();
			break;
		}
		if(r==null){
			return null;
		}
		ImageInfo i=new ImageInfo();
		i.setType(r.getFormatName().toLowerCase());
		int index=r.getMinIndex();
		/**
		 * 对于ImageReader的线程安全是不确定的
		 */
		synchronized (r) {
			r.setInput(is);
			i.setHeight(r.getHeight(index));
			i.setWidth(r.getWidth(index));
		}
		return i;
	}
	public static BufferedImage getImage(byte[] img) throws IOException {
		ByteArrayInputStream bais = new ByteArrayInputStream(img);
		BufferedImage src = ImageIO.read(bais);
		return src;
	}
	/**
	 * 等比例缩放
	 * @param img
	 * @param width
	 * @return
	 * @throws IOException
	 */
	public static BufferedImage getScaleImage(byte[] img,String type,int width) throws IOException {
		ByteArrayInputStream bais = new ByteArrayInputStream(img);
		BufferedImage src = ImageIO.read(bais);
		int w=src.getWidth();
		int h=src.getHeight();
		int height=(int) (((float)width/w)*h);
		Image im=src.getScaledInstance(width, height,Image.SCALE_SMOOTH);
		BufferedImage bi=new BufferedImage(width, height, src.getType());
		bi.getGraphics().drawImage(im, 0, 0,null);
		return bi;
	}
	public static byte[] getScaleImageBytes(byte[] img,String type,int width) throws IOException {
		BufferedImage bi=getScaleImage(img, type, width);
		ByteArrayOutputStream out=new ByteArrayOutputStream();
		ImageIO.write(bi, type, out);
		return out.toByteArray();
	}
	/**
	 * 获取文件类型,没找到返回null,这方法太高效了,可能不准确,
	 * 这个是我看的网上的,有bug不准确
	 * @param byte1
	 * @return
	 */
	public static String fastParseFileType(byte[] byte1) {
		if ((byte1[0] == 71) && (byte1[1] == 73) && (byte1[2] == 70)
				&& (byte1[3] == 56) && ((byte1[4] == 55) || (byte1[4] == 57))
				&& (byte1[5] == 97)) {
			return GIF;
		}
		if ((byte1[6] == 74) && (byte1[7] == 70) && (byte1[8] == 73)
				&& (byte1[9] == 70)) {
			return JPG;
		}
		if ((byte1[0] == 66) && (byte1[1] == 77)) {
			return BMP;
		}
		if ((byte1[1] == 80) && (byte1[2] == 78) && (byte1[3] == 71)) {
			return PNG;
		}
		return null;
	};
	public static class ImageInfo{
		private String type;
		private int width;
		private int height;
		public String getType() {
			return type;
		}
		public void setType(String type) {
			this.type = type;
		}
		public int getWidth() {
			return width;
		}
		public void setWidth(int width) {
			this.width = width;
		}
		public int getHeight() {
			return height;
		}
		public void setHeight(int height) {
			this.height = height;
		}
	}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值