GraphicsMagick官网地址:http://www.graphicsmagick.org/,介绍不多说网上都有,我这里只使用了它图片缩放功能,用java调用练习。
第一步:下载安装。
我下载的是:GraphicsMagick-1.3.20-Q8-win32-dll.exe,安装目录:C:\Program Files (x86)\GraphicsMagick-1.3.20-Q8。安装完毕后,安装目录自动配置到环境变量的Path中了,安装目录里有gm.exe。测试一下,gm -version,如果打印出GraphicsMagick相关信息则ok。这时就可以在命令行里使用gm命令了。
如果使用java调用报错:Caused by: java.io.IOException: CreateProcess error=2。则表示没有gm命令找不到,也就是说环境变量有可能更改了但是没有生效,我的做法是重启eclipse,运行同样程序就ok。如果还是报错,可能环境变量还是没有生效,重启电脑,又或者是安装目录压根就没有配置到环境变量Path中去,手动配置即可。
第二步:写代码。
依赖:
org.im4java
im4java
1.4.0
代码:(api的使用和网上的都不大一样,网上的都是传入2个路径,这里是传入2个字节数组)
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;
}
}
测试:
package org.zoo.hippo;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.junit.Assert;
import org.junit.Test;
import org.zoo.hippo.utils.ImageTool;
/**
*
* @author yankai913@gmail.com
* @date 2015-1-20
*/
public class ImageToolTest {
@Test
public void test_sample() throws Exception {
FileInputStream fis = new FileInputStream("d:/Lighthouse.jpg");
byte[] srcData = new byte[fis.available()];
fis.read(srcData);
fis.close();
int width = 800;
int height = 800;
String suffix = "jpg";
double quality = 90.0;
byte[] dest = ImageTool.sample(srcData, quality, suffix, width, height);
Assert.assertNotNull(dest);
FileOutputStream fos = new FileOutputStream("d:/Lighthouse2.jpg");
fos.write(dest);
fos.close();
}
}
运行日志输出:
23:07:45.977 [main] INFO org.zoo.hippo.utils.ImageTool – inputStream ==> BufferedImage, take 104ms
23:07:46.313 [main] INFO org.zoo.hippo.utils.ImageTool – do convert, take 338ms
23:07:46.341 [main] INFO org.zoo.hippo.utils.ImageTool – BufferedImage ==> outputStream, take 28ms
到此代码完毕,感兴趣的哥们可以看下。