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

 /**
* 图片缩放(未考虑多种图片格式和等比例缩放)
* @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;
} }

最新文章

  1. Oracle 的基本操作符
  2. Spring的简单demo
  3. ASP.NET MVC 概述
  4. 今天又学了一招,牛逼!!!解决"-bash:No such file or directory"问题
  5. 面试iOS遇到这种笔试《操作评估》
  6. js计算日期之间的月份差
  7. linux卸载openJDK并安装sun jdk
  8. 转 BHO API HOOK Wininet基于IE编程的一些资料
  9. ubuntu cron.hourly不运行
  10. Strider 持续集成(gitlab)
  11. Android(java)学习笔记169:Activity中的onCreate()方法分析
  12. php之文件上传简单介绍
  13. 【Android】用Cubism 2制作自己的Live2D——官方App样例源码学习(3)!
  14. BBS论坛(十七)
  15. 简单数据库开发之dao层开发
  16. guava Lists.transform使用
  17. [poj P1475] Pushing Boxes
  18. caffe drawnet.py 用Python画网络框架
  19. Netty 启动过程源码分析 (本文超长慎读)(基于4.1.23)
  20. 性能测试loadrunner安装

热门文章

  1. js 立即执行函数
  2. JDK 之 NIO 2 WatchService、WatchKey(监控文件变化)
  3. wcf 使用sqlMembership证书认证
  4. C++ Crypto++ RSA加密资料收集
  5. SQL 中的Begin...End语句
  6. CentOS7安装ms8可能出现的错误
  7. 2017/2/14springmvc基础学习
  8. 数字统计(NOIP2010)
  9. JS高级-String- RegExp- Math- Date:
  10. IT资产管理详解