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

  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. }  
CCF大数据与计算智能大赛-面向电信行业存量用户的智能套餐个性化匹配模型联通赛-复赛第二名-【多分,embedding】.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计,皆可应用在项目、毕业设计、课程设计、期末/期/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值