GraphicsMagick+im4java 实现高质量大图的处理

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值