详解Java中使用ImageIO类对图片进行压缩的方法
最近做项目需要图片压缩处理,网上找的方法大都使用了com.sun.image.codec.jpeg.*这个包中的JPEGImageEncoder类,引入这个包后一直报错,各种google百度,尝试了各种方法,包括手动引jre中的rt.jar,以及在eclipse中把受访问限制的API提示从ERROR改为WARNING,等等,然而这些都是不好使的,因为后来我发现我的java-7-openjdk-amd64中的rt.jar里边根本就没有com.sun.image.*,貌似这个类在java7中已经被彻底remove了,至少我这个版本是没有了。然后搜了个使用ImageIO类来进行处理的替代方案,代码如下:
可以压缩为任意大小,压缩后高清,不变形(留白),可以改后缀名,可以修改压缩分辨率。
可能有朋友也有这个需要,参考一下吧,有问题还请指证!
packagecn.com.images;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjava.math.BigDecimal;
importjava.math.MathContext;
importjava.util.ArrayList;
importjavax.imageio.ImageIO;
/***
*对图片进行操作
*
*@authorchenzheng_java
*@since2011/7/29
*
*/
publicclassImageHelper{
privatestaticImageHelperimageHelper=null;
publicstaticImageHelpergetImageHelper(){
if(imageHelper==null){
imageHelper=newImageHelper();
}
returnimageHelper;
}
/***
*按指定的比例缩放图片
*
*@paramsourceImagePath
*源地址
*@paramdestinationPath
*改变大小后图片的地址
*@paramscale
*缩放比例,如1.2
*/
publicstaticvoidscaleImage(StringsourceImagePath,
StringdestinationPath,doublescale,Stringformat){
Filefile=newFile(sourceImagePath);
BufferedImagebufferedImage;
try{
bufferedImage=ImageIO.read(file);
intwidth=bufferedImage.getWidth();
intheight=bufferedImage.getHeight();
width=parseDoubleToInt(width*scale);
height=parseDoubleToInt(height*scale);
Imageimage=bufferedImage.getScaledInstance(width,height,
Image.SCALE_SMOOTH);
BufferedImageoutputImage=newBufferedImage(width,height,
BufferedImage.TYPE_INT_RGB);
Graphicsgraphics=outputImage.getGraphics();
graphics.drawImage(image,0,0,null);
graphics.dispose();
ImageIO.write(outputImage,format,newFile(destinationPath));
}catch(IOExceptione){
System.out.println("scaleImage方法压缩图片时出错了");
e.printStackTrace();
}
}
/***
*将图片缩放到指定的高度或者宽度
*@paramsourceImagePath图片源地址
*@paramdestinationPath压缩完图片的地址
*@paramwidth缩放后的宽度
*@paramheight缩放后的高度
*@paramauto是否自动保持图片的原高宽比例
*@paramformat图图片格式例如jpg
*/
publicstaticvoidscaleImageWithParams(StringsourceImagePath,
StringdestinationPath,intwidth,intheight,booleanauto,Stringformat){
try{
Filefile=newFile(sourceImagePath);
BufferedImagebufferedImage=null;
bufferedImage=ImageIO.read(file);
if(auto){
ArrayList<Integer>paramsArrayList=getAutoWidthAndHeight(bufferedImage,width,height);
width=paramsArrayList.get(0);
height=paramsArrayList.get(1);
System.out.println("自动调整比例,width="+width+"height="+height);
}
Imageimage=bufferedImage.getScaledInstance(width,height,
Image.SCALE_DEFAULT);
BufferedImageoutputImage=newBufferedImage(width,height,
BufferedImage.TYPE_INT_RGB);
Graphicsgraphics=outputImage.getGraphics();
graphics.drawImage(image,0,0,null);
graphics.dispose();
ImageIO.write(outputImage,format,newFile(destinationPath));
}catch(Exceptione){
System.out.println("scaleImageWithParams方法压缩图片时出错了");
e.printStackTrace();
}
}
/**
*将double类型的数据转换为int,四舍五入原则
*
*@paramsourceDouble
*@return
*/
privatestaticintparseDoubleToInt(doublesourceDouble){
intresult=0;
result=(int)sourceDouble;
returnresult;
}
/***
*
*@parambufferedImage要缩放的图片对象
*@paramwidth_scale要缩放到的宽度
*@paramheight_scale要缩放到的高度
*@return一个集合,第一个元素为宽度,第二个元素为高度
*/
privatestaticArrayList<Integer>getAutoWidthAndHeight(BufferedImagebufferedImage,intwidth_scale,intheight_scale){
ArrayList<Integer>arrayList=newArrayList<Integer>();
intwidth=bufferedImage.getWidth();
intheight=bufferedImage.getHeight();
doublescale_w=getDot2Decimal(width_scale,width);
System.out.println("getAutoWidthAndHeightwidth="+width+"scale_w="+scale_w);
doublescale_h=getDot2Decimal(height_scale,height);
if(scale_w<scale_h){
arrayList.add(parseDoubleToInt(scale_w*width));
arrayList.add(parseDoubleToInt(scale_w*height));
}
else{
arrayList.add(parseDoubleToInt(scale_h*width));
arrayList.add(parseDoubleToInt(scale_h*height));
}
returnarrayList;
}
/***
*返回两个数a/b的小数点后三位的表示
*@parama
*@paramb
*@return
*/
publicstaticdoublegetDot2Decimal(inta,intb){
BigDecimalbigDecimal_1=newBigDecimal(a);
BigDecimalbigDecimal_2=newBigDecimal(b);
BigDecimalbigDecimal_result=bigDecimal_1.divide(bigDecimal_2,newMathContext(4));
Doubledouble1=newDouble(bigDecimal_result.toString());
System.out.println("相除后的double为:"+double1);
returndouble1;
}
}