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

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

可在pom.xml 中加入下面的代码
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>2.2</version>
   <configuration>
      <compilerArguments>
         <bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath>
      </compilerArguments>
   </configuration>
</plugin>
/**
 * @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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值