图片压缩
因为做APP ,APP 上面传来的图片很大 有好几M 的图片,如果不进行压缩处理,后面再回传到APP里面,那么久非常的耗流量。所以百度查了一个图片压缩的算法来,自己整理了一下。话不多说上代码
package com.qx.client.common.utils;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.*;
/**
* 图片压缩处理
* @author 崔素强
*/
public class ImageCompressionUtil{
public static byte[] compress(File file,
String tempPath,
int w,
int h) throws FileNotFoundException{
return compress(new FileInputStream(file), tempPath, w, h);
}
/**
*
* @param file
* @param tempPath
* @param w
* @param h
* @return
*/
public static byte[] compress(InputStream in,
String tempPath,
int w,
int h){
try{
Image img = ImageIO.read(in);
int width = img.getWidth(null); // 得到源图宽
int height = img.getHeight(null); // 得到源图长
if(width / height > w / h){
h = (int) (height * w / width);
}else{
w = (int) (width * h / height);
}
return resize(img, tempPath, w, h);
}catch(IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 强制压缩/放大图片到固定的大小
* @param tempPath 临时目录
* @param w int 新宽度
* @param h int 新高度
*/
private static byte[] resize(Image img,
String tempPath,
int w,
int h)
throws IOException{
// SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图
File destFile = new File(tempPath);
FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流
// 可以正常实现bmp、png、gif转jpg
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image); // JPEG编码
out.close();
byte[] b = fileToByte(destFile);
destFile.deleteOnExit();
return b;
}
public static byte[] fileToByte(File file){
byte[] buffer = null;
try{
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while((n = fis.read(b)) != -1){
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
}catch(IOException e){
e.printStackTrace();
}
return buffer;
}
public static File byteToFile(byte[] buf,
String filePath,
String fileName){
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try{
File dir = new File(filePath);
if(!dir.exists() && dir.isDirectory()){
dir.mkdirs();
}
file = new File(filePath + File.separator + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(buf);
}catch(Exception e){
e.printStackTrace();
}finally{
if(bos != null){
try{
bos.close();
}catch(IOException e){
e.printStackTrace();
}
}
if(fos != null){
try{
fos.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
return file;
}
/*public static void main(String[] args){
File file = new File("G:\\图片\\73.jpg");
byte[] b = compress(file, "G:\\1.jpg",400, 400);
byteToFile(b, "G:\\", "my1.png");
System.out.println(b);
}*/
}
网上有很多压缩的算法,自己找了好多个,最后还是觉得这个是简单易懂的一个。