java 大圖片處理GraphicsMagick + im4java [縮放,旋轉,裁剪]php
ImageMagickhtml
主頁:http://www.imagemagick.org/script/index.phpjava
GraphicsMagickwindows
主頁:http://www.graphicsmagick.org/api
兩個圖片處理軟件我就不說了,由於我沒那個評論的本事,其實這些軟件都會有命令行的指令,而後咱們用java調用來對圖片進行編輯,調用什麼指令可能學一下才知道,不過咱們也不用本身寫指令吧,由於別人已經封裝好了那些指令的接口(JNI),下面就是那些JNI編輯器
jmagickide
主頁:http://www.jmagick.org/index.html工具
下載地址:http://downloads.jmagick.org/測試
缺點:實地測試後發現,速度果真提升了很多,可是質量卻大大降低了,在大量測試數據下,每生成100張圖片約會有5張圖片生成出現錯誤,還會出現down機的狀況。 .net
im4java
主頁:http://im4java.sourceforge.net/
下載地址:http://sourceforge.net/projects/im4java/files/
API:http://im4java.sourceforge.net/api/
用那個不用說吧,看更新時間,不知道大家會選擇什麼
因此我選用了 GraphicsMagick +im4java
下載GraphicsMagick http://sourceforge.net/projects/graphicsmagick/files/graphicsmagick/1.3.17/ 其實他就是一個軟件,而後windows版本會自動將安裝目錄加入到path變量,因此在指令窗口能夠調用,即安裝好就無論它了
下載im4java來方便咱們在java調用 http://sourceforge.net/projects/im4java/files/
而後將裏面的im4java-1.3.2加入到咱們的項目引用裏面就能夠了
根據別人的代碼,本身再封裝了一下接口吧,基本夠用,那些水印的就沒加上去了,如今不須要 工具很強大,都是靠指令,因此多看看API和軟件自己的使用吧
好吧,我忽悠完了,把時間交給大家了. 附上代碼:[好糾結的代碼編輯器,只能這樣了]
<!-- lang: java --> package test;
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList;
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 TestGm { /** * * 得到圖片文件大小[小技巧來得到圖片大小] * * @param filePath * 文件路徑 * * * @return 文件大小 */
public int getSize(String imagePath) { int size = 0; FileInputStream inputStream = null; try { inputStream = new FileInputStream(imagePath); size = inputStream.available(); inputStream.close(); inputStream = null; } catch (FileNotFoundException e) { size = 0; System.out.println("文件未找到!"); } catch (IOException e) { size = 0; System.out.println("讀取文件大小錯誤!"); } finally { // 可能異常爲關閉輸入流,因此須要關閉輸入流 if (null != inputStream) { try { inputStream.close(); } catch (IOException e) { System.out.println("關閉文件讀入流異常"); } inputStream = null; } } return size; } /** * 得到圖片的寬度 * * @param filePath * 文件路徑 * @return 圖片寬度 */ public int getWidth(String imagePath) { int line = 0; try { IMOperation op = new IMOperation(); op.format("%w"); // 設置獲取寬度參數 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 = Integer.parseInt(cmdOutput.get(0)); } catch (Exception e) { line = 0; System.out.println("運行指令出錯!"); } return line; } /** * 得到圖片的高度 * * @param imagePath * 文件路徑 * @return 圖片高度 */ public int getHeight(String imagePath) { int line = 0; try { IMOperation op = new IMOperation(); op.format("%h"); // 設置獲取高度參數 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 = Integer.parseInt(cmdOutput.get(0)); } catch (Exception e) { line = 0; System.out.println("運行指令出錯!"+e.toString()); } return line; } /** * 圖片信息 * * @param imagePath * @return */ public static String getImageInfo(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 imagePath * 源圖片路徑 * @param newPath * 處理後圖片路徑 * @param x * 起始X座標 * @param y * 起始Y座標 * @param width * 裁剪寬度 * @param height * 裁剪高度 * @return 返回true說明裁剪成功,不然失敗 */ public boolean cutImage(String imagePath, String newPath, int x, int y, int width, int height) { boolean flag = false; try { IMOperation op = new IMOperation(); op.addImage(imagePath); /** width:裁剪的寬度 * height:裁剪的高度 * x:裁剪的橫座標 * y:裁剪縱座標 */ op.crop(width, height, x, y); op.addImage(newPath); ConvertCmd convert = new ConvertCmd(true); convert.run(op); flag = true; } catch (IOException e) { System.out.println("文件讀取錯誤!"); flag = false; } catch (InterruptedException e) { flag = false; } catch (IM4JavaException e) { flag = false; } finally { } return flag; } /** * 根據尺寸縮放圖片[等比例縮放:參數height爲null,按寬度縮放比例縮放;參數width爲null,按高度縮放比例縮放] * * @param imagePath * 源圖片路徑 * @param newPath * 處理後圖片路徑 * @param width * 縮放後的圖片寬度 * @param height * 縮放後的圖片高度 * @return 返回true說明縮放成功,不然失敗 */ public boolean zoomImage(String imagePath, String newPath, Integer width, Integer height) { boolean flag = false; try { IMOperation op = new IMOperation(); op.addImage(imagePath); if (width == null) {// 根據高度縮放圖片 op.resize(null, height); } else if (height == null) {// 根據寬度縮放圖片 op.resize(width); } else { op.resize(width, height); } op.addImage(newPath); ConvertCmd convert = new ConvertCmd(true); convert.run(op); flag = true; } catch (IOException e) { System.out.println("文件讀取錯誤!"); flag = false; } catch (InterruptedException e) { flag = false; } catch (IM4JavaException e) { flag = false; } finally { } return flag; } /** * 圖片旋轉 * * @param imagePath * 源圖片路徑 * @param newPath * 處理後圖片路徑 * @param degree * 旋轉角度 */ public boolean rotate(String imagePath, String newPath, double degree) { boolean flag = false; try { // 1.將角度轉換到0-360度之間 degree = degree % 360; if (degree <= 0) { degree = 360 + degree; } IMOperation op = new IMOperation(); op.addImage(imagePath); op.rotate(degree); op.addImage(newPath); ConvertCmd cmd = new ConvertCmd(true); cmd.run(op); flag = true; } catch (Exception e) { flag = false; System.out.println("圖片旋轉失敗!"); } return flag; } public static void main(String[] args) throws Exception { TestGm imageUtil = new TestGm(); System.out.println("原圖片大小:" + imageUtil.getSize("d://test.jpg") + "Bit"); System.out.println("原圖片寬度:" + imageUtil.getWidth("d://test.jpg")); System.out.println("原圖片高度:" + imageUtil.getHeight("d://test.jpg")); if (imageUtil.zoomImage("d://test.jpg", "d://test1.jpg", 500, null)) { if (imageUtil.rotate("d://test.jpg", "d://test2.jpg", 15)) { if (imageUtil.cutImage("d://test2.jpg", "d://test3.jpg", 32, 105, 200, 200)) { System.out.println("編輯成功"); } else { System.out.println("編輯失敗03"); } } else { System.out.println("編輯失敗02"); } } else { System.out.println("編輯失敗01"); } }
}
相關文章:
ImageMagick圖片處理工具的安裝 --- http://elf8848.iteye.com/blog/455675
選擇ImageMagick仍是GraphicsMagick(翻譯)--- http://co63oc.blog.51cto.com/904636/328997