package com.jeeplus.modules.isp.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import org.apache.commons.lang3.SystemUtils;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;
import org.im4java.process.Pipe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jeeplus.modules.isp.service.impl.MongoFileServiceImp; /**
* 图片处理工具<br>
* 代码实现类将图片装换压缩成固定的大小格式的图片<br>
* 使用工具为im4java+GraphicsMagick-1.3.24-Q8<br>
* 参考: <a href="http://im4java.sourceforge.net/">im4java</a><br>
* GraphicsMagick: <a href="ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/windows/">下载</a><br>
*
* @author xiaofei.xian
* @version
* 1.0, 2016年8月8日 下午2:53:20
*/
public class GraphicsMagicUtil { private static Logger logger = LoggerFactory.getLogger(MongoFileServiceImp.class); private static String GRAPHICS_MAGICK_PATH; private static boolean IS_WINDOWS; /**
* 缩放图片大小
*
* @throws IM4JavaException
* @throws InterruptedException
* @throws IOException
* @return
*/
public static OutputStream zoomPic(OutputStream os, InputStream is, String contentType, Integer width, Integer height)
throws IOException, InterruptedException, IM4JavaException {
IMOperation op = buildIMOperation(contentType, width, height); Pipe pipeIn = new Pipe(is, null);
Pipe pipeOut = new Pipe(null, os); ConvertCmd cmd = new ConvertCmd(true);
if (IS_WINDOWS) {
// linux下不要设置此值,不然会报错
cmd.setSearchPath(GRAPHICS_MAGICK_PATH);
}
cmd.setInputProvider(pipeIn);
cmd.setOutputConsumer(pipeOut);
cmd.run(op);
return os;
} /**
* 压缩图片,返回输入流
*
* @param is
* @param contentType
* @param width
* @param height
* @return
*/
public static InputStream convertThumbnailImage(InputStream is, String contentType, double width, double height) {
try {
IMOperation op = buildIMOperation(contentType, width, height); Pipe pipeIn = new Pipe(is, null);
ByteArrayOutputStream os = new ByteArrayOutputStream();
Pipe pipeOut = new Pipe(null, os); ConvertCmd cmd = new ConvertCmd(true);
if (IS_WINDOWS) {
// linux下不要设置此值,不然会报错
cmd.setSearchPath(GRAPHICS_MAGICK_PATH);
}
cmd.setInputProvider(pipeIn);
cmd.setOutputConsumer(pipeOut);
cmd.run(op);
return new ByteArrayInputStream(os.toByteArray());
} catch (Exception e) {
if (logger.isInfoEnabled()) {
logger.info("Failed to convert image {}", e.getMessage());
}
return null;
}
} /**
* @param contentType
* @param width
* @param height
* @return
*/
private static IMOperation buildIMOperation(String contentType, Number width, Number height) {
IMOperation op = new IMOperation(); String widHeight = width + "x" + height;
op.addImage("-"); // 命令:处输入流中读取图片
op.addRawArgs("-scale", widHeight);// 按照给定比例缩放图片
op.addRawArgs("-gravity", "center"); // 缩放参考位置 对图像进行定位
op.addRawArgs("-extent", width + "x" + height); // 限制JPEG文件的最大尺寸
op.addRawArgs("+profile", "*");// 去除Exif信息 // 设置图片压缩格式
op.addImage(contentType.substring(contentType.indexOf("/") + 1) + ":-");
return op;
} public static void setGraphicsMagickPath(String graphicsMagickPath) {
GraphicsMagicUtil.GRAPHICS_MAGICK_PATH = graphicsMagickPath;
IS_WINDOWS = SystemUtils.IS_OS_WINDOWS;
} }

  

最新文章

  1. Keil环境中建立带FreeRTOS的STM32L项目
  2. CCF真题之数列分段
  3. 10+ powerful debugging tricks with Visual Studio
  4. Android最新锁屏病毒分析及解锁
  5. iOS-NSString-Base64String-Base64原理
  6. 狗狗40题~ (Volume C)
  7. Windows 桌面边栏小工具开发入门
  8. HYSBZ1588 营业额统计【Splay】
  9. linux c coding style
  10. 201521123105 《Java程序设计》第1周学习总结
  11. python 标准模块 string
  12. 使用 BenchmarkDotnet 测试代码性能
  13. vue 倒计时组件
  14. C++ 实现分数的四则运算
  15. Servlet交互与JSP
  16. whu 643 Soul Artist(二维BIT 区间更新,单点查询)
  17. Python习题(第3课)
  18. leetcode_目录
  19. 稳重商务风格教师求职简历免费word模板
  20. BZOJ 1036 树的统计 | 树链剖分模板题

热门文章

  1. vue编辑回显问题
  2. 获取CAD安装路径
  3. 运维是做什么的?史上最全互联网Linux工作规划!十分钟找到linux运维工程师职业方向!
  4. Oracle行转列/列转行
  5. Luogu P2298 Mzc和男家丁的游戏
  6. linu学习第一天:基础知识
  7. Problem 42
  8. linux修改mysql表结构
  9. Awesome Python(中文对照)
  10. [bzoj3676]回文串[后缀数组+Manacher]