博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 图片压缩 缩放
阅读量:4114 次
发布时间:2019-05-25

本文共 1926 字,大约阅读时间需要 6 分钟。

废话不多说,直接上代码,静态方法可直接调用,中间用流来处理的

/**   * 图片缩放(未考虑多种图片格式和等比例缩放)   * @param filePath 图片路径   * @param height 高度   * @param width 宽度   * @param picType 图片格式   * @param bb 比例不对时是否需要补白  */  @Deprecated  public static byte[] resize(byte[] imageBuffer, int height, int width, String picType, boolean bb) {    byte[] targetBuffer = null;    try {      BufferedImage fromImage = ImageIO.read(new ByteArrayInputStream(imageBuffer));      BufferedImage scaleImage = zoomImage(fromImage, width, height);      targetBuffer = writeHighQuality(scaleImage);    } catch (IOException e) {      logger.error("ImageUtils Resize Exception." , e);    }    return targetBuffer;  }/**   * @param im   *            原始图像   * @param resizeTimes   *            倍数,比如0.5就是缩小一半,0.98等等double类型   * @return 返回处理后的图像   */  public static BufferedImage zoomImage(BufferedImage im, int toWidth, int toHeight) {    /* 原始图像的宽度和高度 */    int width = im.getWidth();    int height = im.getHeight();    /* 调整后的图片的宽度和高度 */    //    int toWidth = (int) (Float.parseFloat(String.valueOf(width)) * resizeTimes);    //    int toHeight = (int) (Float.parseFloat(String.valueOf(height)) * resizeTimes);    /* 新生成结果图片 */    BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);    result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);    return result;  }        public static byte[] writeHighQuality(BufferedImage im) {    try {      ByteArrayOutputStream imageStream = new ByteArrayOutputStream();      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(imageStream);      JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(im);      /* 压缩质量 */      jep.setQuality(1f, true);      encoder.encode(im, jep);      /* 近JPEG编码 */      imageStream.close();      return imageStream.toByteArray();    } catch (Exception e) {      e.printStackTrace();      return null;    }      }

转载地址:http://tcrsi.baihongyu.com/

你可能感兴趣的文章
Objective-首个OC的类
查看>>
Objective-C点语法
查看>>
Objective-C自定义构造方法和description方法
查看>>
Objective-self关键字
查看>>
Objective-C空指针与野指针
查看>>
Android开发中的Extras
查看>>
Intent使用意图(二)
查看>>
【整理】Xcode中的iOS模拟器(iOS Simulator)的介绍和使用心得
查看>>
IOS中实现动画的三种方式
查看>>
Iphone控件大全
查看>>
IOS随机获取想要的内容
查看>>
IOS中Json解析的四种方法
查看>>
Android抽象布局——include、merge 、ViewStub
查看>>
Listview 控件的头尾布局
查看>>
含有CheckBOX 的listview控件实现全选全不选的功能
查看>>
Android错误信息的汇总
查看>>
iOS应用程序状态切换相关
查看>>
Android内存管理
查看>>
android中的文件操作详解以及内部存储和外部存储
查看>>
java中几种常用的排序方法
查看>>