package imageUtils; import java.io.IOException; import java.util.ArrayList; import org.im4java.core.ConvertCmd; import org.im4java.core.IMOperation; /** * @author hegh E-mail: heguanhua@tjhq.com * @version 建立時間:Mar 13, 2012 10:43:12 AM 類說明 */ public class ImageMagick { /** * ImageMagick的路徑 */ public static String imageMagickPath = null; static{ /**獲取ImageMagick的路徑 */ //Properties prop = new PropertiesFile().getPropertiesFile(); //linux下不要設置此值,否則會報錯 //imageMagickPath = prop.getProperty("imageMagickPath"); } /** * 根據座標裁剪圖片 * @param srcPath 要裁剪圖片的路徑 * @param newPath 裁剪圖片後的路徑 * @param x 起始橫座標 * @param y 起始挫座標 * @param x1 結束橫座標 * @param y1 結束挫座標 */ public static void cutImage(String srcPath, String newPath, int x, int y, int x1, int y1) throws Exception { int width = x1 - x; int height = y1 - y; IMOperation op = new IMOperation(); op.addImage(srcPath); /** * width:裁剪的寬度 * height:裁剪的高度 * x:裁剪的橫座標 * y:裁剪的挫座標 */ op.crop(width, height, x, y); op.addImage(newPath); ConvertCmd convert = new ConvertCmd(); //linux下不要設置此值,否則會報錯 //convert.setSearchPath(imageMagickPath); convert.run(op); } /** * 根據尺寸縮放圖片 * @param width 縮放後的圖片寬度 * @param height 縮放後的圖片高度 * @param srcPath 源圖片路徑 * @param newPath 縮放後圖片的路徑 * @param type 1爲比例處理,2爲大小處理,如(比例:1024x1024,大小:50%x50%) */ public static String cutImage(int width, int height, String srcPath, String newPath,int type,String quality) throws Exception { IMOperation op = new IMOperation(); ConvertCmd cmd = new ConvertCmd(true); op.addImage(); String raw = ""; if(type == 1){ //按像素 raw = width+"x"+height+"^"; }else{ //按像素百分比 raw = width+"%x"+height+"%"; } op.addRawArgs("-sample" , raw ); if((quality !=null && quality.equals(""))){ op.addRawArgs("-quality" , quality ); } op.addImage(); String osName = System.getProperty("os.name").toLowerCase(); if(osName.indexOf("win") != -1) { //linux下不要設置此值,否則會報錯 cmd.setSearchPath("C://Program Files//GraphicsMagick-1.3.14-Q16"); } try{ cmd.run(op, srcPath, newPath); }catch(Exception e){ e.printStackTrace(); } return newPath; } /** * 給圖片加水印 * @param srcPath 源圖片路徑 */ public static void addImgText(String srcPath) throws Exception { IMOperation op = new IMOperation(); op.font("宋體").gravity("southeast").pointsize(18).fill("#BCBFC8").draw("text 100,100 co188.com"); op.addImage(); op.addImage(); String osName = System.getProperty("os.name").toLowerCase(); ConvertCmd cmd = new ConvertCmd(true); if(osName.indexOf("win") != -1) { //linux下不要設置此值,否則會報錯 cmd.setSearchPath("C://Program Files//GraphicsMagick-1.3.14-Q16"); } try{ cmd.run(op, srcPath, srcPath); }catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args) throws Exception{ //cutImage("D:\\apple870.jpg", "D:\\apple870eee.jpg",98, 48, 370, 320); Long start = System.currentTimeMillis(); //cutImage(100,100, "e:\\37AF7D10F2D8448A9A5.jpg","e:\\37AF7D10F2D8448A9A5_bak2.jpg",2,"100"); addImgText("e:\\37AF7D10F2D8448A9A5_bak2.jpg"); Long end = System.currentTimeMillis(); System.out.println("time:"+(end-start)/3600); } } 經過GraphicsMagick+im4java實現高質量大圖的處理,解決了100M以上,以及圖片像素10000以上處理是出現內存溢出的問