使用mogrify 转化图片格式为RGB

mogrify 下载地址:http://www.imagemagick.org/script/binary-releases.php#windows

cmd执行结果:

 mogrify -colorspace RGB -quality 100 "D:\software\eclipse\workspace2\demo_channel_terminal\src\main\resources\mini.jpg"

说明:最后一个参数是要转化的图片全路径.

 

那么如何使用Java 来调用呢?

测试代码:

Java代码   收藏代码
  1. @Test  
  2.         public void test02()  
  3.         {  
  4.             Process p = null;  
  5.             String []command = null;  
  6.   
  7.             command = new String[]{"cmd"," /c ","mogrify.exe" ,"-colorspace""RGB""-quality" ,"100" ,"\"D:\\software\\eclipse\\workspace2\\demo_channel_terminal\\src\\main\\resources\\mini.jpg\""};//cmd /c is needed  
  8.             BufferedReader reader = null;  
  9.             try  
  10.             {  
  11.                 p = Runtime.getRuntime().exec(command, null);//)  
  12.                 reader = new BufferedReader(new InputStreamReader(p  
  13.                         .getErrorStream(),"gbk"));  
  14.                 StringBuilder sb = new StringBuilder();  
  15.                 String readedLine = null;  
  16.                 try  
  17.                 {  
  18.                     while ((readedLine = reader.readLine()) != null)  
  19.                     {  
  20.                         sb.append(readedLine);  
  21.                         sb.append("\r\n");  
  22.                     }  
  23.                 }  
  24.                 catch (IOException e)  
  25.                 {  
  26.                     e.printStackTrace();  
  27.                 }  
  28.                 finally  
  29.                 {  
  30.                     try  
  31.                     {  
  32.                         reader.close();  
  33.                         p.destroy();  
  34.                     }  
  35.                     catch (IOException e)  
  36.                     {  
  37.                         e.printStackTrace();  
  38.                     }  
  39.                 }  
  40.                 String content = sb.toString();  
  41.                 System.out.println(content);  
  42.             }  
  43.             catch (IOException e)  
  44.             {  
  45.                 e.printStackTrace();  
  46.             }  
  47.   
  48.         }  

 执行报错:

 'mogrify.exe' 不是内部或外部命令,也不是可运行的程序

或批处理文件。

但是我在命令行里面执行没有问题呀!!!

为什么命令行里面没问题,java调用就有问题呢?

最后找到了原因:java执行本地命令时要指定命令(exe文件)所在路径

String commandFolder="D:\\Program Files\\ImageMagick-6.8.9-Q16\\";

           p = Runtime.getRuntime().exec(command, null,new File(commandFolder/*命令的所在目录*/));//)

           reader = new BufferedReader(new InputStreamReader(p

                   .getErrorStream(),"gbk"));

 

正确的代码如下:

Java代码   收藏代码
  1. @Test  
  2.         public void test02()  
  3.         {  
  4.             Process p = null;  
  5.             String []command = null;  
  6. //          command = new String[]{"cmd"," /c ","dir"};//cmd /c is needed  
  7.             command = new String[]{"cmd"," /c ","mogrify.exe" ,"-colorspace""RGB""-quality" ,"100" ,"\"D:\\software\\eclipse\\workspace2\\demo_channel_terminal\\src\\main\\resources\\mini.jpg\""};//cmd /c is needed  
  8.             BufferedReader reader = null;  
  9.             try  
  10.             {  
  11.                 String commandFolder="D:\\Program Files\\ImageMagick-6.8.9-Q16\\";  
  12.                 p = Runtime.getRuntime().exec(command, null,new File(commandFolder/*命令的所在目录*/));//)  
  13.                 reader = new BufferedReader(new InputStreamReader(p  
  14.                         .getErrorStream(),"gbk"));  
  15.                 StringBuilder sb = new StringBuilder();  
  16.                 String readedLine = null;  
  17.                 try  
  18.                 {  
  19.                     while ((readedLine = reader.readLine()) != null)  
  20.                     {  
  21.                         sb.append(readedLine);  
  22.                         sb.append("\r\n");  
  23.                     }  
  24.                 }  
  25.                 catch (IOException e)  
  26.                 {  
  27.                     e.printStackTrace();  
  28.                 }  
  29.                 finally  
  30.                 {  
  31.                     try  
  32.                     {  
  33.                         reader.close();  
  34.                         p.destroy();  
  35.                     }  
  36.                     catch (IOException e)  
  37.                     {  
  38.                         e.printStackTrace();  
  39.                     }  
  40.                 }  
  41.                 String content = sb.toString();  
  42.                 System.out.println(content);  
  43.             }  
  44.             catch (IOException e)  
  45.             {  
  46.                 e.printStackTrace();  
  47.             }  
  48.   
  49.         }  

  mogrify网盘下载地址:http://pan.baidu.com/s/1i3vHPOh

安装完成之后目录情况:

 

参考:http://iaiai.iteye.com/blog/1461370

注意:

(1)java 执行dir或ipconfig的命令不需要执行命令所在目录,但是执行用户安装上的exe就必须执行命令所在目录;

(2)java执行操作系统命令时一定要加上"cmd  /c"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值