package org.zoo.hippo.utils; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import javax.imageio.ImageIO; import org.im4java.core.ConvertCmd; import org.im4java.core.IMOperation; import org.im4java.core.Stream2BufferedImage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @author yankai913@gmail.com * @date 2015-1-20 */ public class ImageTool { private static final Logger logger = LoggerFactory.getLogger(ImageTool. class ); private static String METHOD_SAMPLE = "sample" ; private static String METHOD_RESIZE = "resize" ; private static String METHOD_THUMBNAIL = "thumbnail" ; private static String METHOD_SCALE = "scale" ; private static IMOperation createIMOperation( double quality, String suffix, int width, int height, String method) { IMOperation op = new IMOperation(); op.addImage(); if (METHOD_SAMPLE.equals(method)) { op.sample(width, height); } else if (METHOD_RESIZE.equals(method)) { op.resize(width, height); } else if (METHOD_THUMBNAIL.equals(method)) { op.thumbnail(width, height); } else { op.scale(width, height); } op.quality(quality); op.addImage( new String[] { suffix + ":-" }); return op; } public static byte [] sample( byte [] srcData, double quality, String suffix, int width, int height) { return scale0(srcData, quality, suffix, width, height, METHOD_SAMPLE); } public static byte [] resize( byte [] srcData, double quality, String suffix, int width, int height) { return scale0(srcData, quality, suffix, width, height, METHOD_RESIZE); } public static byte [] thumbnail( byte [] srcData, double quality, String suffix, int width, int height) { return scale0(srcData, quality, suffix, width, height, METHOD_THUMBNAIL); } public static byte [] scale( byte [] srcData, double quality, String suffix, int width, int height) { return scale0(srcData, quality, suffix, width, height, METHOD_SCALE); } private static byte [] scale0( byte [] srcData, double quality, String suffix, int width, int height, String method) { byte [] result = null ; IMOperation op = createIMOperation(quality, suffix, width, height, method); try { ConvertCmd localConvertCmd = new ConvertCmd( true ); long s = System.currentTimeMillis(); ByteArrayInputStream bais = new ByteArrayInputStream(srcData); BufferedImage inputImage = ImageIO.read(bais); long s2 = System.currentTimeMillis(); logger.info( "inputStream ==> BufferedImage, take " + (s2 - s) + "ms" ); Stream2BufferedImage streamBuffered = new Stream2BufferedImage(); localConvertCmd.setOutputConsumer(streamBuffered); localConvertCmd.run(op, new Object[] { inputImage }); long s3 = System.currentTimeMillis(); logger.info( "do convert, take " + (s3 - s2) + "ms" ); BufferedImage outputImage = streamBuffered.getImage(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(outputImage, suffix, baos); long s4 = System.currentTimeMillis(); logger.info( "BufferedImage ==> outputStream, take " + (s4 - s3) + "ms" ); result = baos.toByteArray(); } catch (Exception e) { logger.error(e.getMessage(), e); } return result; } } |