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. }  
Stkcd [股票代码] ShortName [股票简称] Accper [统计截止日期] Typrep [报表型编码] Indcd [行业代码] Indnme [行业名称] Source [公告来源] F060101B [净利润现金净含量] F060101C [净利润现金净含量TTM] F060201B [营业收入现金含量] F060201C [营业收入现金含量TTM] F060301B [营业收入现金净含量] F060301C [营业收入现金净含量TTM] F060401B [营业利润现金净含量] F060401C [营业利润现金净含量TTM] F060901B [筹资活动债权人现金净流量] F060901C [筹资活动债权人现金净流量TTM] F061001B [筹资活动股东现金净流量] F061001C [筹资活动股东现金净流量TTM] F061201B [折旧摊销] F061201C [折旧摊销TTM] F061301B [公司现金流1] F061302B [公司现金流2] F061301C [公司现金流TTM1] F061302C [公司现金流TTM2] F061401B [股权现金流1] F061402B [股权现金流2] F061401C [股权现金流TTM1] F061402C [股权现金流TTM2] F061501B [公司自由现金流(原有)] F061601B [股权自由现金流(原有)] F061701B [全部现金回收率] F061801B [营运指数] F061901B [资本支出与折旧摊销比] F062001B [现金适合比率] F062101B [现金再投资比率] F062201B [现金满足投资比率] F062301B [股权自由现金流] F062401B [企业自由现金流] Indcd1 [行业代码1] Indnme1 [行业名称1] 季度数据,所有沪深北上市公司的 分别包含excel、dta数据文件格式及其说明,便于不同软件工具对数据的分析应用 数据来源:基于上市公司年报及公告数据整理,或相关证券交易所、各部委、省、市数据 数据范围:基于沪深北证上市公司 A股(主板、小企业板、创业板、科创板等)数据整理计算
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值