java jmagick实现图片完美缩放(命令版)

操作系统:winXP
软件下载地址:http://downloads.jmagick.org/
用的是6.3.9
下载ImageMagick-6.3.9-0-Q8-windows-dll.exe和jmagick-win-6.3.9-Q8.zip
安装ImageMagick-6.3.9-0-Q8-windows-dll.exe,按照网上说法把安装后根目录下的所有.dll文件拷贝到C:\WINDOWS\system32下。不过没拷贝成功。略之不理。
jmagick-win-6.3.9-Q8.zip解压缩。将里面的jmagick.dll拷贝至C:\WINDOWS\system32下最好JAVAHOME/bin下也放一份。
配置环境变量path加入C:\Program Files\ImageMagick-6.3.9-Q8(自己视情况在而变)
将jmagick-win-6.3.9-Q8.zip里jmagick.jar放入自己的工程
这里用的是java调用命令操作。
程序例子代码:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class Aa {
	public static String CONVERT_PROG = "C:\\Program Files\\ImageMagick-6.3.9-Q8\\convert.exe";//视情况而变
		
	public static void main(String[] args) {
		File in = new File("C:\\1.gif");//源文件
		File out = new File("C:\\2.gif");//输出文件
		convert(in,out,300,100,100,10);
	}
	

	/*
	 * Uses a Runtime.exec()to use imagemagick to perform the given conversion
	 * operation. Returns true on success, false on failure. Does not check if
	 * either file exists.
	 * 
	 * @param in Description of the Parameter @param out Description of the
	 * Parameter @param newSize Description of the Parameter @param quality
	 * Description of the Parameter @return Description of the Return Value
	 */
	@SuppressWarnings("unchecked")
	private static boolean convert(File in, File out, int width, int height,
			int quality,int newSize) {
		System.out.println("convert(" + in.getPath() + ", " + out.getPath()
				+ ", " + newSize + ", " + quality);

		if (quality < 0 || quality > 100) {
			quality = 75;
		}

		ArrayList command = new ArrayList(10);

		// note: CONVERT_PROG is a class variable that stores the location of
		// ImageMagick's convert command
		// it might be something like "/usr/local/magick/bin/convert" or
		// something else, depending on where you installed it.
		command.add(CONVERT_PROG);
		command.add("-geometry");
		command.add(width + "x" + height);
		command.add("-quality");
		command.add("" + quality);
		command.add(in.getAbsolutePath());
		command.add(out.getAbsolutePath());

		System.out.println(command);

		return exec((String[]) command.toArray(new String[1]));
	}

	/**
	 * Tries to exec the command, waits for it to finsih, logs errors if exit
	 * status is nonzero, and returns true if exit status is 0 (success).
	 * 
	 * @param command
	 *            Description of the Parameter
	 * @return Description of the Return Value
	 */
	private static boolean exec(String[] command) {
		Process proc;

		try {
			// System.out.println("Trying to execute command " +
			// Arrays.asList(command));
			proc = Runtime.getRuntime().exec(command);
		} catch (IOException e) {
			System.out
					.println("IOException while trying to execute " + command);
			return false;
		}

		// System.out.println("Got process object, waiting to return.");

		int exitStatus;

		while (true) {
			try {
				exitStatus = proc.waitFor();
				break;
			} catch (java.lang.InterruptedException e) {
				System.out.println("Interrupted: Ignoring and waiting");
			}
		}
		if (exitStatus != 0) {
			System.out.println("Error executing command: " + exitStatus);
		}
		return (exitStatus == 0);
	}

}

你可能感兴趣的:(java,C++,c,windows,C#)