GraphicsMagick下载地址:http://www.skycn.com/soft/appid/398142.html
im4java maven 配置
<dependency>
<groupId>org.im4java</groupId>
<artifactId>im4java</artifactId>
<version>1.4.0</version>
</dependency>
ImageUtils.java
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import org.im4java.core.CompositeCmd;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;
import org.im4java.core.IdentifyCmd;
public final class ImageUtils {
private static String gmPath;
static {
File file1;
File file2;
if (gmPath == null) {
String osName = System.getProperty("os.name").toLowerCase();
if (osName.indexOf("windows") >= 0) {
String path = System.getenv("Path");
if (path != null) {
String[] paths = path.split(";");
for (String str : paths) {
file1 = new File(str.trim() + "/gm.exe");
file2 = new File(str.trim() + "/gmdisplay.exe");
if ((file1.exists()) && (file2.exists())) {
gmPath = str.trim();
break;
}
}
}
}
}
try {
IMOperation imo = new IMOperation();
imo.version();
IdentifyCmd identifyCmd = new IdentifyCmd(true);
if (gmPath != null) {
identifyCmd.setSearchPath(gmPath);
}
identifyCmd.run(imo, new Object[0]);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @param srcFile
* 源文件
* @param destFile
* 生成文件
* @param destWidth
* 生成文件的宽度
* @param destHeight
* 生成文件的高度
* @param type
* 类型 1:像素2:百分比
*/
public static void zoom(File srcFile, File destFile, int destWidth,
int destHeight, int type) {
IMOperation op = new IMOperation();
ConvertCmd cmd = new ConvertCmd(true);
op.addImage();
String raw = "";
if(type == 1){
//按像素
raw = destWidth+"x"+destHeight+"^";
}else{
//按像素百分比
raw = destWidth+"%"+destHeight+"%";
}
op.addRawArgs("-sample",raw);
op.addRawArgs("-gravity","center");
op.addRawArgs("-quality","100");
op.addImage();
String osName = System.getProperty("os.name").toLowerCase();
if(osName.indexOf("win") != -1) {
//linux下不要设置此值,不然会报错
cmd.setSearchPath("F:/GraphicsMagick-1.3.9-Q16");
}
try{
cmd.run(op, srcFile.getPath(), destFile.getPath());
}catch(Exception e){
e.printStackTrace();
}
}
public static void initialize() {
}
}
GraphicsMagick常用方法
1. 显示图像文件详细信息
gm identify a.jpg
2.更改当前目录下*.jpg的尺寸大小,并保存于目录.thumb里面
gm mogrify -output-directory .thumbs -resize 320×200 *.jpg
3. 将三幅图像和并为一副图像
gm montage -mode concatenate -tile 3×1 image1.ppm image2.ppm image3.ppm concatenated.miff
4. 显示图像
gm display ‘vid:*.jpg’
5. 格式转换
gm convert a.bmp a.jpg
gm convert a.bmp a.pdf(转换为pdf)
6. 调整图像dpi和大小
gm convert -density 288 -geometry 25% image.gif image.gif
(缩小为原先的1/4,并且dpi为288)
gm convert -resize 640×480 image.gif image.gif
(转换为640×480的图像)
7. 在图像上添加文字
gm convert -font Arial -fill blue -pointsize 18 -draw “text 10,10 ‘your text here’” test.tif test.png
8. 从gif文件中抽取第一帧
gm convert “Image.gif[0]” first.gif
9. 建立gif图像
gm convert -delay 20 frame*.gif animation.gif
gm convert -loop 50 frame*.gif animation.gif
(让动画循环50次)
gm convert -delay 20 frame1.gif -delay 10 frame2.gif -delay 5 frame3.gif animation.gif
(对每一帧手动指定延时)
10. 截屏
gm import a.jpg
用鼠标点击所要截取的窗口,或者选择截屏区域,保存为a.jpg
gm import -frame a.jpg
保留窗口的边框
GraphicsMagick API
http://www.graphicsmagick.org/GraphicsMagick.html