天天看點

使用mogrify 轉化圖檔格式為RGB

mogrify 下載下傳位址:http://www.imagemagick.org/script/binary-releases.php#windows

cmd執行結果:

使用mogrify 轉化圖檔格式為RGB

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

說明:最後一個參數是要轉化的圖檔全路徑.

測試代碼:

使用mogrify 轉化圖檔格式為RGB

@test  

        public void test02()  

        {  

            process p = null;  

            string []command = null;  

            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  

            bufferedreader reader = null;  

            try  

            {  

                p = runtime.getruntime().exec(command, null);//)  

                reader = new bufferedreader(new inputstreamreader(p  

                        .geterrorstream(),"gbk"));  

                stringbuilder sb = new stringbuilder();  

                string readedline = null;  

                try  

                {  

                    while ((readedline = reader.readline()) != null)  

                    {  

                        sb.append(readedline);  

                        sb.append("\r\n");  

                    }  

                }  

                catch (ioexception e)  

                    e.printstacktrace();  

                finally  

                    try  

                        reader.close();  

                        p.destroy();  

                    catch (ioexception e)  

                        e.printstacktrace();  

                string content = sb.tostring();  

                system.out.println(content);  

            }  

            catch (ioexception e)  

                e.printstacktrace();  

        }  

 執行報錯:

使用mogrify 轉化圖檔格式為RGB

 '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"));

正确的代碼如下:

使用mogrify 轉化圖檔格式為RGB

//          command = new string[]{"cmd"," /c ","dir"};//cmd /c is needed  

                string commandfolder="d:\\program files\\imagemagick-6.8.9-q16\\";  

                p = runtime.getruntime().exec(command, null,new file(commandfolder/*指令的所在目錄*/));//)  

  mogrify網盤下載下傳位址:http://pan.baidu.com/s/1i3vhpoh

安裝完成之後目錄情況:

使用mogrify 轉化圖檔格式為RGB

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

注意:

(1)java 執行dir或ipconfig的指令不需要執行指令所在目錄,但是執行使用者安裝上的exe就必須執行指令所在目錄;

(2)java執行作業系統指令時一定要加上"cmd  /c"

繼續閱讀