ImageMagick是一套GPL版權的影像壓縮、處理程式,有很多種介面可以使用,
G2F (Ada), MagickCore (C), MagickWand (C), ChMagick (Ch), ImageMagickObject (COM+), Magick++ (C++), JMagick (Java), L-Magick (Lisp), NMagick (Neko/haXe), MagickNet (.NET), PascalMagick (Pascal), PerlMagick (Perl), MagickWand for PHP (PHP), IMagick (PHP), PythonMagick (Python), RMagick (Ruby), or TclMagick (Tcl/TK)
官網
http://www.imagemagick.org/script/index.php
它的壓縮品質,實在是十分的好,很多知名的網站也都使用它來做影像處理
今天要介紹的是JMagick 利用java來呼叫ImageMagick
一,從ImageMagick官網下載(以windows為例)
http://www.imagemagick.org/script/binary-releases.php#windows
我們下載ImageMagick-6.4.6-2-Q16-windows-dll.exe
安裝後,我們需要使用到它的dll檔,請指定環境變數path至安裝目錄(安裝後自動會加入 at Windows)
再來下載jmagick
http://downloads.jmagick.org/
jmagick-win-6.3.9-Q16.zip
這是Jmagick官網,不過我找不到載點
http://sourceforge.net/projects/jmagick/
下載後解開,裡面有一個dll放至windows\system32裡
而jar檔,放到開發的classpath裡,如tomcat就放到tomcat\common\lib 或tomcat\webapps\專案\WEB-INF\lib
比較建議放到common\lib裡,不然蠻容易被JVM locked 住的
再來把就撰寫程式來測試一下吧
package jmagic;
import magick.ImageInfo; import magick.MagickException; import magick.MagickImage;
public class JMagickScale {
public static String getCompressPathName(String RealPath, String Name, int width, int height, String URLPath) { if (System.getProperty("jmagick.systemclassloader") == null) { System.setProperty("jmagick.systemclassloader", "no"); } if (URLPath == null) { URLPath = ""; } URLPath = URLPath + (URLPath.lastIndexOf("\\") == URLPath.length() ? "\\" : "") + width + "-" + height + "\\"; // 確保資料來源正確 System.out.println(RealPath.lastIndexOf(java.io.File.separatorChar) + " " + RealPath.length()); if (RealPath.lastIndexOf(java.io.File.separatorChar) != RealPath .length() - 1) RealPath = RealPath + java.io.File.separatorChar; if (Name.indexOf(java.io.File.separatorChar) == 0) Name = Name.substring(1, Name.length()); try { // 存放資料夾 java.io.File path = new java.io.File(RealPath + width + "-" + height); // 建立資料夾 if (!path.exists()) { if (!path.mkdir()) throw new java.io.IOException("建立目錄錯誤!"); }
// System.out.println(path.getAbsoluteFile()+Name); java.io.File to = new java.io.File(path.getAbsoluteFile() + String.valueOf(java.io.File.separatorChar) + Name); java.io.File source = new java.io.File(RealPath + Name); if (to.exists())// 不存在的話則做縮圖 { return URLPath + to.getName(); } // System.out.println(URLPath+to.getName()); // 否則則縮圖 System.out.println("開始縮圖"); ImageInfo info = new ImageInfo(source.getAbsolutePath()); MagickImage image = new MagickImage(info);
// resize image
MagickImage scaleImg = image.scaleImage(width, height);
// write image to file scaleImg.setFileName(path.getAbsolutePath() + String.valueOf(java.io.File.separatorChar) + Name); scaleImg.writeImage(info);
to.setLastModified(source.lastModified()); System.out.println("縮圖成功" + RealPath + Name + " " + width + " " + height); return URLPath + to.getName(); } catch (MagickException e) { System.out.println("縮圖失敗:" + RealPath + Name + " " + width + " " + height); return URLPath + Name; } catch (java.io.IOException e) { System.out.println("檔案IO錯誤:" + RealPath + Name); return URLPath + Name; }
}
public static void main(String[] args) {
// JMagickScale test = new JMagickScale(); System.out.println("Test Work = " + JMagickScale.getCompressPathName(args[0], args[1], Integer .parseInt(args[2]), Integer.parseInt(args[3]), "\\"));
} } |
背景那張是直接用img width 跟height 縮小的(也就是縮小,並沒有壓縮)
而前面那個利用IrfanView打開的就是壓縮過的
差別很大吧^^