java中压缩图片的代码辅助类

来源:互联网 发布:linux卸载firefox 编辑:程序博客网 时间:2024/06/05 17:16
  1. package cn.com.images;  
  2.   
  3. import java.awt.Graphics;  
  4. import java.awt.Image;  
  5. import java.awt.image.BufferedImage;  
  6. import java.io.File;  
  7. import java.io.IOException;  
  8. import java.math.BigDecimal;  
  9. import java.math.MathContext;  
  10. import java.util.ArrayList;  
  11.   
  12. import javax.imageio.ImageIO;  
  13.   
  14. /*** 
  15.  * 对图片进行操作 
  16.  *  
  17.  * @author chenzheng_java 
  18.  * @since 2011/7/29 
  19.  *  
  20.  */  
  21. public class ImageHelper {  
  22.   
  23.     private static ImageHelper imageHelper = null;  
  24.   
  25.     public static ImageHelper getImageHelper() {  
  26.         if (imageHelper == null) {  
  27.             imageHelper = new ImageHelper();  
  28.         }  
  29.         return imageHelper;  
  30.     }  
  31.   
  32.     /*** 
  33.      * 按指定的比例缩放图片 
  34.      *  
  35.      * @param sourceImagePath 
  36.      *            源地址 
  37.      * @param destinationPath 
  38.      *            改变大小后图片的地址 
  39.      * @param scale 
  40.      *            缩放比例,如1.2 
  41.      */  
  42.     public static void scaleImage(String sourceImagePath,  
  43.             String destinationPath, double scale,String format) {  
  44.   
  45.         File file = new File(sourceImagePath);  
  46.         BufferedImage bufferedImage;  
  47.         try {  
  48.             bufferedImage = ImageIO.read(file);  
  49.             int width = bufferedImage.getWidth();  
  50.             int height = bufferedImage.getHeight();  
  51.   
  52.             width = parseDoubleToInt(width * scale);  
  53.             height = parseDoubleToInt(height * scale);  
  54.   
  55.             Image image = bufferedImage.getScaledInstance(width, height,  
  56.                     Image.SCALE_SMOOTH);  
  57.             BufferedImage outputImage = new BufferedImage(width, height,  
  58.                     BufferedImage.TYPE_INT_RGB);  
  59.             Graphics graphics = outputImage.getGraphics();  
  60.             graphics.drawImage(image, 00null);  
  61.             graphics.dispose();  
  62.   
  63.             ImageIO.write(outputImage, format, new File(destinationPath));  
  64.         } catch (IOException e) {  
  65.             System.out.println("scaleImage方法压缩图片时出错了");  
  66.             e.printStackTrace();  
  67.         }  
  68.   
  69.     }  
  70.   
  71.     /*** 
  72.      * 将图片缩放到指定的高度或者宽度 
  73.      * @param sourceImagePath 图片源地址 
  74.      * @param destinationPath 压缩完图片的地址 
  75.      * @param width 缩放后的宽度 
  76.      * @param height 缩放后的高度 
  77.      * @param auto 是否自动保持图片的原高宽比例 
  78.      * @param format 图图片格式 例如 jpg 
  79.      */  
  80.     public static void scaleImageWithParams(String sourceImagePath,  
  81.             String destinationPath, int width, int height, boolean auto,String format) {  
  82.           
  83.         try {  
  84.         File file = new File(sourceImagePath);  
  85.         BufferedImage bufferedImage = null;  
  86.         bufferedImage = ImageIO.read(file);  
  87.             if (auto) {  
  88.                 ArrayList<Integer> paramsArrayList = getAutoWidthAndHeight(bufferedImage,width,height);  
  89.                 width = paramsArrayList.get(0);  
  90.                 height = paramsArrayList.get(1);  
  91.                 System.out.println("自动调整比例,width="+width+" height="+height);  
  92.             }  
  93.           
  94.         Image image = bufferedImage.getScaledInstance(width, height,  
  95.                 Image.SCALE_DEFAULT);  
  96.         BufferedImage outputImage = new BufferedImage(width, height,  
  97.                 BufferedImage.TYPE_INT_RGB);  
  98.         Graphics graphics = outputImage.getGraphics();  
  99.         graphics.drawImage(image, 00null);  
  100.         graphics.dispose();  
  101.         ImageIO.write(outputImage, format, new File(destinationPath));  
  102.         } catch (Exception e) {  
  103.             System.out.println("scaleImageWithParams方法压缩图片时出错了");  
  104.             e.printStackTrace();  
  105.         }  
  106.           
  107.           
  108.     }  
  109.   
  110.     /** 
  111.      * 将double类型的数据转换为int,四舍五入原则 
  112.      *  
  113.      * @param sourceDouble 
  114.      * @return 
  115.      */  
  116.     private static int parseDoubleToInt(double sourceDouble) {  
  117.         int result = 0;  
  118.         result = (int) sourceDouble;  
  119.         return result;  
  120.     }  
  121.       
  122.     /*** 
  123.      *  
  124.      * @param bufferedImage 要缩放的图片对象 
  125.      * @param width_scale 要缩放到的宽度 
  126.      * @param height_scale 要缩放到的高度 
  127.      * @return 一个集合,第一个元素为宽度,第二个元素为高度 
  128.      */  
  129.     private static ArrayList<Integer> getAutoWidthAndHeight(BufferedImage bufferedImage,int width_scale,int height_scale){  
  130.         ArrayList<Integer> arrayList = new ArrayList<Integer>();  
  131.         int width = bufferedImage.getWidth();  
  132.         int height = bufferedImage.getHeight();  
  133.         double scale_w =getDot2Decimal( width_scale,width);  
  134.           
  135.         System.out.println("getAutoWidthAndHeight width="+width + "scale_w="+scale_w);  
  136.         double scale_h = getDot2Decimal(height_scale,height);  
  137.         if (scale_w<scale_h) {  
  138.             arrayList.add(parseDoubleToInt(scale_w*width));  
  139.             arrayList.add(parseDoubleToInt(scale_w*height));  
  140.         }  
  141.         else {  
  142.             arrayList.add(parseDoubleToInt(scale_h*width));  
  143.             arrayList.add(parseDoubleToInt(scale_h*height));  
  144.         }  
  145.         return arrayList;  
  146.           
  147.     }  
  148.       
  149.       
  150.     /*** 
  151.      * 返回两个数a/b的小数点后三位的表示 
  152.      * @param a 
  153.      * @param b 
  154.      * @return 
  155.      */  
  156.     public static double getDot2Decimal(int a,int b){  
  157.           
  158.         BigDecimal bigDecimal_1 = new BigDecimal(a);  
  159.         BigDecimal bigDecimal_2 = new BigDecimal(b);  
  160.         BigDecimal bigDecimal_result = bigDecimal_1.divide(bigDecimal_2,new MathContext(4));  
  161.         Double double1 = new Double(bigDecimal_result.toString());  
  162.         System.out.println("相除后的double为:"+double1);  
  163.         return double1;  
  164.     }  
  165.   
  166. }  
原创粉丝点击