图片压缩(很简单的 压缩 方法,可自定义图片的质量,宽,高)

为什么80%的码农都做不了架构师?>>>   hot3.png

在 使用这个方法时 若出现一下的错误

可在pom.xml 中加入下面的代码

   org.apache.maven.plugins
   maven-compiler-plugin
   2.2
   
      
         ${java.home}/lib/rt.jar;${java.home}/lib/jce.jar
      
   
/**
 * @athor huangyuchen
 * compress image by width , height , quantity  ,ratio
 * @param input
 * @param quantity
 * @param width
 * @param height
 * @return

 */
private static InputStream compressImageByWeithAndHeight(InputStream input, float quantity, int width, int height) {
   quantity = quantity == 0f ? 1f : quantity;
   try {
      ImageInputStream imageInput = ImageIO.createImageInputStream(input);
      Image image = ImageIO.read(imageInput);
      //原图高
      int old_h = image.getHeight(null);
      //原图宽
      int old_w = image.getWidth(null);
      //新图宽
      int new_w = 0;
      //新图高
      int new_h = 0;
      //计算图片比例
      double w2 = (old_w * 1.00) / (width * 1.00);
      double h2 = (old_h * 1.00) / (height * 1.00);
      // 图片跟据长宽将图片背景色设置为黑色,成一个正方形图。
      BufferedImage oldpic;
      if (old_w > old_h) {
         oldpic = new BufferedImage(old_w, old_w,
               BufferedImage.TYPE_INT_RGB);
      } else {
         if (old_w < old_h) {
            oldpic = new BufferedImage(old_h, old_h,
                  BufferedImage.TYPE_INT_RGB);
         } else {
            oldpic = new BufferedImage(old_w, old_h,
                  BufferedImage.TYPE_INT_RGB);
         }
      }
      //设置背景色
      Graphics2D g = oldpic.createGraphics();
      g.setColor(Color.BLACK);
      if (old_w > old_h) {
         g.fillRect(0, 0, old_w, old_w);
         g.drawImage(image, 0, (old_w - old_h) / 2, old_w, old_h,
               Color.BLACK, null);
      } else {
         if (old_w < old_h) {
            g.fillRect(0, 0, old_h, old_h);
            g.drawImage(image, (old_h - old_w) / 2, 0, old_w, old_h,
                  Color.BLACK, null);
         } else {

            // g.fillRect(0,0,old_h,old_h);
            g.drawImage(image.getScaledInstance(old_w, old_h,
                  Image.SCALE_SMOOTH), 0, 0, null);
         }
      }
      g.dispose();
      image = oldpic;
      // 图片调整为正方形
      // 计算新图宽
      if (old_w > width) {
         new_w = (int) Math.round(old_w / w2);
      } else {
         new_w = old_w;
      }
      // 计算新图高
      if (old_h > height) {
         new_h = (int) Math.round(old_h / h2);
      } else {
         new_h = old_h;
      }
      /** 宽,高设定 */
      BufferedImage tag = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
      tag.getGraphics().drawImage(image, 0, 0, new_w, new_h, null);
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
      JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
      /** 压缩质量 */
      jep.setQuality(quantity, true);
      encoder.encode(tag, jep);
      ByteArrayInputStream result = new ByteArrayInputStream(out.toByteArray());
      out.close();
      return result;
   } catch (Exception e) {
      e.printStackTrace();
   }

   return input;
}

转载于:https://my.oschina.net/hycky/blog/774694

你可能感兴趣的:(图片压缩(很简单的 压缩 方法,可自定义图片的质量,宽,高))