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调用命令操作。
程序例子代码:
Java代码 复制代码  收藏代码 java jmagick实现图片完美缩放(命令版) - 我爱我的老婆 - 游龙
  1. import java.io.File;   
  2. import java.io.IOException;   
  3. import java.util.ArrayList;   
  4.   
  5. public class Aa {   
  6.     public static String CONVERT_PROG = "C:\\Program Files\\ImageMagick-6.3.9-Q8\\convert.exe";//视情况而变   
  7.            
  8.     public static void main(String[] args) {   
  9.         File in = new File("C:\\1.gif");//源文件   
  10.         File out = new File("C:\\2.gif");//输出文件   
  11.         convert(in,out,300,100,100,10);   
  12.     }   
  13.        
  14.   
  15.     /*  
  16.      * Uses a Runtime.exec()to use imagemagick to perform the given conversion  
  17.      * operation. Returns true on success, false on failure. Does not check if  
  18.      * either file exists.  
  19.      *   
  20.      * @param in Description of the Parameter @param out Description of the  
  21.      * Parameter @param newSize Description of the Parameter @param quality  
  22.      * Description of the Parameter @return Description of the Return Value  
  23.      */  
  24.     @SuppressWarnings("unchecked")   
  25.     private static boolean convert(File in, File out, int width, int height,   
  26.             int quality,int newSize) {   
  27.         System.out.println("convert(" + in.getPath() + ", " + out.getPath()   
  28.                 + ", " + newSize + ", " + quality);   
  29.   
  30.         if (quality < 0 || quality > 100) {   
  31.             quality = 75;   
  32.         }   
  33.   
  34.         ArrayList command = new ArrayList(10);   
  35.   
  36.         // note: CONVERT_PROG is a class variable that stores the location of   
  37.         // ImageMagick's convert command   
  38.         // it might be something like "/usr/local/magick/bin/convert" or   
  39.         // something else, depending on where you installed it.   
  40.         command.add(CONVERT_PROG);   
  41.         command.add("-geometry");   
  42.         command.add(width + "x" + height);   
  43.         command.add("-quality");   
  44.         command.add("" + quality);   
  45.         command.add(in.getAbsolutePath());   
  46.         command.add(out.getAbsolutePath());   
  47.   
  48.         System.out.println(command);   
  49.   
  50.         return exec((String[]) command.toArray(new String[1]));   
  51.     }   
  52.   
  53.     /**  
  54.      * Tries to exec the command, waits for it to finsih, logs errors if exit  
  55.      * status is nonzero, and returns true if exit status is 0 (success).  
  56.      *   
  57.      * @param command  
  58.      *            Description of the Parameter  
  59.      * @return Description of the Return Value  
  60.      */  
  61.     private static boolean exec(String[] command) {   
  62.         Process proc;   
  63.   
  64.         try {   
  65.             // System.out.println("Trying to execute command " +   
  66.             // Arrays.asList(command));   
  67.             proc = Runtime.getRuntime().exec(command);   
  68.         } catch (IOException e) {   
  69.             System.out   
  70.                     .println("IOException while trying to execute " + command);   
  71.             return false;   
  72.         }   
  73.   
  74.         // System.out.println("Got process object, waiting to return.");   
  75.   
  76.         int exitStatus;   
  77.   
  78.         while (true) {   
  79.             try {   
  80.                 exitStatus = proc.waitFor();   
  81.                 break;   
  82.             } catch (java.lang.InterruptedException e) {   
  83.                 System.out.println("Interrupted: Ignoring and waiting");   
  84.             }   
  85.         }   
  86.         if (exitStatus != 0) {   
  87.             System.out.println("Error executing command: " + exitStatus);   
  88.         }   
  89.         return (exitStatus == 0);   
  90.     }   
  91.   
  92. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值