使用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"

相关文章
|
存储 编解码 Android开发
NV21、NV12、YV12、RGB、YUV、RGBA、RGBX8888等图像色彩编码格式区别
NV21、NV12、YV12、RGB、YUV、RGBA、RGBX8888都是常见的图像颜色编码格式,它们之间的主要区别在于色彩空间和数据排列方式。
197 0
|
开发者 Kotlin
将JPG图像根据色彩范围转换为PNG透明图像(kotlin)
这实际上是一个十分普遍的需求,在kotlin中如何完成这一任务呢?其实这样简单的操作不需要任何三方库,只需要BufferedImage的原生功能就能做到,约60行代码
169 0
|
5月前
|
存储 传感器 编解码
图像格式:常见图像格式RAW, RGB, YUV&&图像格式的解析、格式转换和看图软件
图像格式:常见图像格式RAW, RGB, YUV&&图像格式的解析、格式转换和看图软件
1067 1
|
5月前
|
存储 编解码
解码失败会显示绿屏,及yuv和rgb简单理解
解码失败会显示绿屏,及yuv和rgb简单理解
142 0
|
12月前
|
C#
C# 图片RGB处理判断
C# 图片RGB处理判断 需要:根据一张原始图的RGB平均值和新的图片的RGB平均值的差距,来判断图中是否出现除原图中物体外的其他物体 前提:.Net framework 4.8 及以上 示例代码: 程序集:using System;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imagin...
46 0
|
计算机视觉
计算机图形|RGB与HSV模型的转化
计算机图形|RGB与HSV模型的转化
144 0
计算机图形|RGB与HSV模型的转化
|
计算机视觉
将TIF图像格式转化为PNG或者JPG格式
安装好cv2库,如果没有安装,请使用pip install opencv-python进行安装。
220 0
|
C++
C++实现对RGB图片进行编码
介绍了如何利用得到的RGB信息重新对RGB图片进行编码,以及对其他图片如BMP所得到的RGB信息进行编码从而得到*.jpg文件。
154 0
CMKY与RGB的转换
CMKY与RGB的转换
102 0