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;
}


//不要使用avaliable()方法  JDK说的很清楚这个方法没有始终返回0 ,但是DOTNET里面是可以获取到的 
public static byte[] inStream2Bytes(InputStream in)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
   int b=0;
   while((b = in.read())!=-1)
       baos.write(b);
byte[] buf=baos.toByteArray();
return buf;
}
//图片格式转换, formatType  "JPG", PNG etc...
public static byte[] imgFormatConvert(byte[] imgData,String formatType)
{
BufferedImage  bufImage=ImageIO.read(new ByteArrayInputStream(imgData));

//格式转换
ByteArrayOutputStream outStreamTemp=new ByteArrayOutputStream();
ImageIO.write(bufImage, formatType, outStreamTemp);

imgData=outStreamTemp.toByteArray();
return imgData;
}
/**
* 构建一个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 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;
}
}
}

你可能感兴趣的:(java,java,byte,string,null,bi,image)