一、前言
基于com.sun.image.codec.jpeg.JPEGCodec、com.sun.image.codec.jpeg.JPEGImageEncoder压缩JPG/JPEG类,对定义的图片压缩处理工具类ImageTools,详情参见代码示例说明部分。
二、代码示例
import java.awt.Color;@b@import java.awt.Graphics2D;@b@import java.awt.Image;@b@import java.awt.image.BufferedImage;@b@import java.awt.image.ConvolveOp;@b@import java.awt.image.Kernel;@b@import java.io.File;@b@import java.io.FileNotFoundException;@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@import java.util.Map;@b@@b@import com.sun.image.codec.jpeg.JPEGCodec;@b@import com.sun.image.codec.jpeg.JPEGEncodeParam;@b@import com.sun.image.codec.jpeg.JPEGImageEncoder;@b@@b@public class ImageTools {@b@ /**@b@ * 描述:@b@ * @param path 需要压缩的图片路径@b@ * @param fileName 要压缩的图片名称@b@ * @param toFileName 压缩后的图片名称@b@ * @param scale 压缩比例 不能大于1,默认0.5@b@ * @param quality 压缩品质介于0.1~1.0之间 @b@ * @param width 压缩后的图片的宽度@b@ * @param height 压缩后的图片的高度@b@ * 返回值:void@b@ */@b@ private static void imageCompress(String path,String newPath, String fileName,String toFileName,float scale,float quality,int width,int height){@b@ FileOutputStream out = null;@b@ try {@b@ Image image = javax.imageio.ImageIO.read(new File(path +"/"+ fileName));@b@ int imageWidth = image.getWidth(null);@b@ int imageHeight = image.getHeight(null);@b@ if(scale >=1.0) scale = 0.99f;//默认压缩比为0.5,压缩比越大,对内存要去越高,可能导致内存溢出@b@ //按比例计算出来的压缩比@b@ float realscale = getRatio(imageWidth,imageHeight,width,height);@b@ float finalScale = Math.min(scale, realscale);//取压缩比最小的进行压缩@b@ imageWidth = (int)(finalScale*imageWidth);@b@ imageHeight = (int)(finalScale*imageHeight);@b@ @b@ image = image.getScaledInstance(imageWidth, imageHeight, Image.SCALE_AREA_AVERAGING);@b@ BufferedImage mBufferedImage = new BufferedImage(imageWidth, imageHeight,BufferedImage.TYPE_INT_RGB);@b@ Graphics2D g2 = mBufferedImage.createGraphics();@b@ @b@ g2.drawImage(image, 0, 0,imageWidth, imageHeight, Color.white,null);@b@ g2.dispose();@b@ @b@ float[] kernelData2 = {-0.125f, -0.125f, -0.125f,-0.125f,2, -0.125f,-0.125f,-0.125f, -0.125f };@b@ Kernel kernel = new Kernel(3, 3, kernelData2);@b@ ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);@b@ mBufferedImage = cOp.filter(mBufferedImage, null);@b@@b@ out = new FileOutputStream(newPath+"/" + toFileName);@b@ JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);@b@ JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(mBufferedImage);@b@ param.setQuality(quality, true);//默认0.75@b@ encoder.setJPEGEncodeParam(param);@b@ encoder.encode(mBufferedImage);@b@ }catch (FileNotFoundException fnf){@b@ fnf.printStackTrace();@b@ }catch (IOException ioe){@b@ ioe.printStackTrace();@b@ }catch(Exception ex){@b@ ex.printStackTrace();@b@ }finally{@b@ try {@b@ if(out !=null) out.close();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ }@b@ }@b@ public static void imageCompress(String path,String newPath,String fileName,String toFileName,float scale,int width,int height){@b@ imageCompress(path,newPath, fileName, toFileName, scale, 0.75f, width, height);@b@ }@b@ private static float getRatio(int width,int height,int maxWidth,int maxHeight){@b@ float Ratio = 1.0f;@b@ float widthRatio ;@b@ float heightRatio ;@b@ widthRatio = (float)maxWidth/width;@b@ heightRatio = (float)maxHeight/height;@b@ if(widthRatio<1.0 || heightRatio<1.0){@b@ Ratio = widthRatio<=heightRatio?widthRatio:heightRatio;@b@ }@b@ return Ratio;@b@ }@b@ @b@ /**@b@ * 动态获取压缩比列@b@ * @param map@b@ * @return@b@ */@b@ public static float getCompressSize(float width){@b@ float result = 0.5f;@b@ result = 595/width;@b@ result = Float.parseFloat(StringUtil.formatNumber("#.##", result));@b@ return result;@b@ }@b@ @b@ public static void main(String[] args) {@b@ //ImageTools.imageCompress("D:\\images\\","D:\\images\\temp\\", "460.gif", "460.gif",0.75f, 280,280);@b@ System.out.println(ImageTools.getCompressSize(600f));@b@ }@b@}