`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;` `/**` `* 图片处理工具` `* 代码实现类将图片装换压缩成固定的大小格式的图片` `* 使用工具为im4java+GraphicsMagick-1.3.24-Q8` `* 参考: im4java` `* GraphicsMagick: 下载` `*` `* @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;` `}` `}`
-
Java
3203 引用 • 8217 回帖 • 1 关注
Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于