使用方法:首先要安裝ImageMagick這個工具,安裝好這個工具后,再下載im4java包放到項目lib目錄里就行了。
package com.stu.util; import java.io.IOException; import java.util.ArrayList; import org.im4java.core.CompositeCmd; import org.im4java.core.ConvertCmd; import org.im4java.core.IM4JavaException; import org.im4java.core.IMOperation; import org.im4java.core.IdentifyCmd; import org.im4java.process.ArrayListOutputConsumer; public class ImagesUtil { /** * 根據坐標裁剪圖片 * * @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(); convert.run(op); } /** * 根據尺寸縮放圖片 * @param width 縮放后的圖片寬度 * @param height 縮放后的圖片高度 * @param srcPath 源圖片路徑 * @param newPath 縮放后圖片的路徑 */ public static void zoomImage(Integer width, Integer height, String srcPath, String newPath) throws Exception { IMOperation op = new IMOperation(); op.addImage(srcPath); if(width == null){//根據高度縮放圖片 op.resize(null, height); }else if(height == null){//根據寬度縮放圖片 op.resize(width, null); }else { op.resize(width, height); } op.addImage(newPath); ConvertCmd convert = new ConvertCmd(); convert.run(op); } /** * 給圖片加水印 * @param srcPath 源圖片路徑 */ public static void addImgText(String srcPath,String content) throws Exception { IMOperation op = new IMOperation(); op.font("微軟雅黑"); op.gravity("southeast"); op.pointsize(18).fill("#BCBFC8").draw("text 0,0 "+content); //("x1 x2 x3 x4") x1 格式,x2 x軸距離 x3 y軸距離 x4名稱 op.addImage(); op.addImage(); ConvertCmd convert = new ConvertCmd(); try { convert.run(op,srcPath,srcPath); } catch (Exception e) { e.printStackTrace(); } } /** * 圖片水印 * * @param srcImagePath 源圖片 * @param waterImagePath 水印 * @param destImagePath 生成圖片 * @param gravity 圖片位置 * @param dissolve 水印透明度 */ public static void waterMark(String waterImagePath, String srcImagePath, String destImagePath, String gravity, int dissolve) { IMOperation op = new IMOperation(); op.gravity(gravity); op.dissolve(dissolve); op.addImage(waterImagePath); op.addImage(srcImagePath); op.addImage(destImagePath); CompositeCmd cmd = new CompositeCmd(); try { cmd.run(op); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } /** * 圖片旋轉 * * @param srcImagePath * @param destImagePath * @param angle */ public static void rotate(String srcImagePath, String destImagePath, double angle) { try { IMOperation op = new IMOperation(); op.rotate(angle); op.addImage(srcImagePath); op.addImage(destImagePath); ConvertCmd cmd = new ConvertCmd(); cmd.run(op); } catch (Exception e) { e.printStackTrace(); } } /** * 圖片信息 * * @param imagePath * @return */ public static String showImageInfo(String imagePath) { String line = null; try { IMOperation op = new IMOperation(); op.format("width:%w,height:%h,path:%d%f,size:%b%[EXIF:DateTimeOriginal]"); op.addImage(1); IdentifyCmd identifyCmd = new IdentifyCmd(true); ArrayListOutputConsumer output = new ArrayListOutputConsumer(); identifyCmd.setOutputConsumer(output); identifyCmd.run(op, imagePath); ArrayList<String> cmdOutput = output.getOutput(); assert cmdOutput.size() == 1; line = cmdOutput.get(0); } catch (Exception e) { e.printStackTrace(); } return line; } /** * 圖片合成 * @param args * @param maxWidth * @param maxHeight * @param newpath * @param mrg * @param type 1:橫,2:豎 */ public static void montage(String[] args,Integer maxWidth,Integer maxHeight,String newpath,Integer mrg,String type) { IMOperation op = new IMOperation(); ConvertCmd cmd = new ConvertCmd(); String thumb_size = maxWidth+"x"+maxHeight+"^"; String extent = maxWidth+"x"+maxHeight; if("1".equals(type)){ op.addRawArgs("+append"); }else if("2".equals(type)){ op.addRawArgs("-append"); } op.addRawArgs("-thumbnail",thumb_size); op.addRawArgs("-gravity","center"); op.addRawArgs("-extent",extent); Integer border_w = maxWidth / 40; op.addRawArgs("-border",border_w+"x"+border_w); op.addRawArgs("-bordercolor","#ccc"); op.addRawArgs("-border",1+"x"+1); op.addRawArgs("-bordercolor","#fff"); for(String img : args){ op.addImage(img); } if("1".equals(type)){ Integer whole_width = ((mrg / 2) +1 + border_w + maxWidth + border_w + (mrg / 2) +1)*args.length - mrg; Integer whole_height = maxHeight + border_w + 1; op.addRawArgs("-extent",whole_width + "x" +whole_height); }else if("2".equals(type)){ Integer whole_width = maxWidth + border_w + 1; Integer whole_height = ((mrg / 2) +1 + border_w + maxHeight + border_w + (mrg / 2) +1)*args.length - mrg; op.addRawArgs("-extent",whole_width + "x" +whole_height); } op.addImage(newpath); try { cmd.run(op); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception{ //addImgText("e://a2.jpg"); //zoomImage(300, 150, "e://a.jpg", "e://a1.jpg"); //zoomImage(300, 150, "e://b.jpg", "e://b1.jpg"); //zoomImage(300, 150, "e://c.jpg", "e://c1.jpg"); //zoomImage(300, 150, "e://d.jpg", "e://d1.jpg"); //zoomImage(300, 150, "e://e.jpg", "e://e1.jpg"); // zoomImage(300, 150, "e://f.jpg", "e://f1.jpg"); //waterMark("e://cc.jpg", "e://aa.jpg", "e://bb.jpg", "southeast", 30); //rotate("e://aa.jpg", "e://ee.jpg", 90); //System.out.println(showImageInfo("e://aa.jpg")); String[] files = new String[5]; files[0] = "e://a1.jpg"; files[1] = "e://b1.jpg"; files[2] = "e://c1.jpg"; files[3] = "e://d1.jpg"; files[4] = "e://e1.jpg"; //montage(files, 280, 200, "e://liboy1.jpg", 0,"2"); //cropImage("e://a.jpg", "e://liboy22.jpg", 1024, 727, 500, 350); cutImage("e://a.jpg", "e://liboy222.jpg", 5, 10, 100, 120); } }
//如果是在windows下運行,則需要配置ImageMagick的路徑(),如果找不到convert的話,把目錄下的magick.exe拷貝一份命名convert.exe
ConvertCmd convert = new ConvertCmd();
convert.setSearchPath("E:\\ImageMagick-7.0.5-Q16");
convert.run(op);