当前位置: 移动技术网 > IT编程>开发语言>Java > 详解Java中使用ImageIO类对图片进行压缩的方法

详解Java中使用ImageIO类对图片进行压缩的方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

最近做项目需要图片压缩处理,网上找的方法大都使用了 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类来进行处理的替代方案,代码如下:
可以压缩为任意大小,压缩后高清,不变形(留白),可以改后缀名,可以修改压缩分辨率。
可能有朋友也有这个需要,参考一下吧,有问题还请指证!

package cn.com.images; 
 
import java.awt.graphics; 
import java.awt.image; 
import java.awt.image.bufferedimage; 
import java.io.file; 
import java.io.ioexception; 
import java.math.bigdecimal; 
import java.math.mathcontext; 
import java.util.arraylist; 
 
import javax.imageio.imageio; 
 
/*** 
 * 对图片进行操作 
 * 
 * @author chenzheng_java 
 * @since 2011/7/29 
 * 
 */ 
public class imagehelper { 
 
  private static imagehelper imagehelper = null; 
 
  public static imagehelper getimagehelper() { 
    if (imagehelper == null) { 
      imagehelper = new imagehelper(); 
    } 
    return imagehelper; 
  } 
 
  /*** 
   * 按指定的比例缩放图片 
   * 
   * @param sourceimagepath 
   *      源地址 
   * @param destinationpath 
   *      改变大小后图片的地址 
   * @param scale 
   *      缩放比例,如1.2 
   */ 
  public static void scaleimage(string sourceimagepath, 
      string destinationpath, double scale,string format) { 
 
    file file = new file(sourceimagepath); 
    bufferedimage bufferedimage; 
    try { 
      bufferedimage = imageio.read(file); 
      int width = bufferedimage.getwidth(); 
      int height = bufferedimage.getheight(); 
 
      width = parsedoubletoint(width * scale); 
      height = parsedoubletoint(height * scale); 
 
      image image = bufferedimage.getscaledinstance(width, height, 
          image.scale_smooth); 
      bufferedimage outputimage = new bufferedimage(width, height, 
          bufferedimage.type_int_rgb); 
      graphics graphics = outputimage.getgraphics(); 
      graphics.drawimage(image, 0, 0, null); 
      graphics.dispose(); 
 
      imageio.write(outputimage, format, new file(destinationpath)); 
    } catch (ioexception e) { 
      system.out.println("scaleimage方法压缩图片时出错了"); 
      e.printstacktrace(); 
    } 
 
  } 
 
  /*** 
   * 将图片缩放到指定的高度或者宽度 
   * @param sourceimagepath 图片源地址 
   * @param destinationpath 压缩完图片的地址 
   * @param width 缩放后的宽度 
   * @param height 缩放后的高度 
   * @param auto 是否自动保持图片的原高宽比例 
   * @param format 图图片格式 例如 jpg 
   */ 
  public static void scaleimagewithparams(string sourceimagepath, 
      string destinationpath, int width, int height, boolean auto,string format) { 
     
    try { 
    file file = new file(sourceimagepath); 
    bufferedimage bufferedimage = 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); 
      } 
     
    image image = bufferedimage.getscaledinstance(width, height, 
        image.scale_default); 
    bufferedimage outputimage = new bufferedimage(width, height, 
        bufferedimage.type_int_rgb); 
    graphics graphics = outputimage.getgraphics(); 
    graphics.drawimage(image, 0, 0, null); 
    graphics.dispose(); 
    imageio.write(outputimage, format, new file(destinationpath)); 
    } catch (exception e) { 
      system.out.println("scaleimagewithparams方法压缩图片时出错了"); 
      e.printstacktrace(); 
    } 
     
     
  } 
 
  /** 
   * 将double类型的数据转换为int,四舍五入原则 
   * 
   * @param sourcedouble 
   * @return 
   */ 
  private static int parsedoubletoint(double sourcedouble) { 
    int result = 0; 
    result = (int) sourcedouble; 
    return result; 
  } 
   
  /*** 
   * 
   * @param bufferedimage 要缩放的图片对象 
   * @param width_scale 要缩放到的宽度 
   * @param height_scale 要缩放到的高度 
   * @return 一个集合,第一个元素为宽度,第二个元素为高度 
   */ 
  private static arraylist<integer> getautowidthandheight(bufferedimage bufferedimage,int width_scale,int height_scale){ 
    arraylist<integer> arraylist = new arraylist<integer>(); 
    int width = bufferedimage.getwidth(); 
    int height = bufferedimage.getheight(); 
    double scale_w =getdot2decimal( width_scale,width); 
     
    system.out.println("getautowidthandheight width="+width + "scale_w="+scale_w); 
    double scale_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)); 
    } 
    return arraylist; 
     
  } 
   
   
  /*** 
   * 返回两个数a/b的小数点后三位的表示 
   * @param a 
   * @param b 
   * @return 
   */ 
  public static double getdot2decimal(int a,int b){ 
     
    bigdecimal bigdecimal_1 = new bigdecimal(a); 
    bigdecimal bigdecimal_2 = new bigdecimal(b); 
    bigdecimal bigdecimal_result = bigdecimal_1.divide(bigdecimal_2,new mathcontext(4)); 
    double double1 = new double(bigdecimal_result.tostring()); 
    system.out.println("相除后的double为:"+double1); 
    return double1; 
  } 
 
} 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网