java 大图片处理GraphicsMagick + im4java [缩放,旋转,裁剪]
ImageMagick
主页:http://www.imagemagick.org/script/index.php
GraphicsMagick
主页:http://www.graphicsmagick.org/
两个图片处理软件我就不说了,因为我没那个评论的本事,其实这些软件都会有命令行的指令,然后我们用java调用来对图片进行编辑,调用什么指令可能学一下才知道,不过我们也不用自己写指令吧,因为别人已经封装好了那些指令的接口(JNI),下面就是那些JNI
jmagick
主页:http://www.jmagick.org/index.html
下载地址:http://downloads.jmagick.org/
缺点:实地测试后发现,速度果然提高了不少,但是质量却大大下降了,在大量测试数据下,每生成100张图片约会有5张图片生成出现错误,还会出现down机的情况。
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和软件本身的使用吧
好吧,我忽悠完了,把时间交给你们了.
附上代码:[好纠结的代码编辑器,只能这样了]
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 {
002 | * * 获得图片文件大小[小技巧来获得图片大小] * * @param filePath * 文件路径 * |
007 | public int getSize(String imagePath) { |
009 | FileInputStream inputStream = null ; |
011 | inputStream = new FileInputStream(imagePath); |
012 | size = inputStream.available(); |
015 | } catch (FileNotFoundException e) { |
017 | System.out.println( "文件未找到!" ); |
018 | } catch (IOException e) { |
020 | System.out.println( "读取文件大小错误!" ); |
023 | if ( null != inputStream) { |
026 | } catch (IOException e) { |
027 | System.out.println( "关闭文件读入流异常" ); |
043 | public int getWidth(String imagePath) { |
046 | IMOperation op = new IMOperation(); |
049 | IdentifyCmd identifyCmd = new IdentifyCmd( true ); |
050 | ArrayListOutputConsumer output = new ArrayListOutputConsumer(); |
051 | identifyCmd.setOutputConsumer(output); |
052 | identifyCmd.run(op, imagePath); |
053 | ArrayList<String> cmdOutput = output.getOutput(); |
054 | assert cmdOutput.size() == 1 ; |
055 | line = Integer.parseInt(cmdOutput.get( 0 )); |
056 | } catch (Exception e) { |
058 | System.out.println( "运行指令出错!" ); |
070 | public int getHeight(String imagePath) { |
073 | IMOperation op = new IMOperation(); |
077 | IdentifyCmd identifyCmd = new IdentifyCmd( true ); |
078 | ArrayListOutputConsumer output = new ArrayListOutputConsumer(); |
079 | identifyCmd.setOutputConsumer(output); |
080 | identifyCmd.run(op, imagePath); |
081 | ArrayList<String> cmdOutput = output.getOutput(); |
082 | assert cmdOutput.size() == 1 ; |
083 | line = Integer.parseInt(cmdOutput.get( 0 )); |
084 | } catch (Exception e) { |
086 | System.out.println( "运行指令出错!" +e.toString()); |
097 | public static String getImageInfo(String imagePath) { |
100 | IMOperation op = new IMOperation(); |
101 | op.format( "width:%w,height:%h,path:%d%f,size:%b%[EXIF:DateTimeOriginal]" ); |
103 | IdentifyCmd identifyCmd = new IdentifyCmd( true ); |
104 | ArrayListOutputConsumer output = new ArrayListOutputConsumer(); |
105 | identifyCmd.setOutputConsumer(output); |
106 | identifyCmd.run(op, imagePath); |
107 | ArrayList<String> cmdOutput = output.getOutput(); |
108 | assert cmdOutput.size() == 1 ; |
109 | line = cmdOutput.get( 0 ); |
111 | } catch (Exception e) { |
132 | * @return 返回true说明裁剪成功,否则失败 |
134 | public boolean cutImage(String imagePath, String newPath, int x, int y, |
135 | int width, int height) { |
136 | boolean flag = false ; |
138 | IMOperation op = new IMOperation(); |
139 | op.addImage(imagePath); |
140 | /** width:裁剪的宽度 * height:裁剪的高度 * x:裁剪的横坐标 * y:裁剪纵坐标 */ |
141 | op.crop(width, height, x, y); |
142 | op.addImage(newPath); |
143 | ConvertCmd convert = new ConvertCmd( true ); |
146 | } catch (IOException e) { |
147 | System.out.println( "文件读取错误!" ); |
149 | } catch (InterruptedException e) { |
151 | } catch (IM4JavaException e) { |
160 | * 根据尺寸缩放图片[等比例缩放:参数height为null,按宽度缩放比例缩放;参数width为null,按高度缩放比例缩放] |
170 | * @return 返回true说明缩放成功,否则失败 |
172 | public boolean zoomImage(String imagePath, String newPath, Integer width, |
175 | boolean flag = false ; |
177 | IMOperation op = new IMOperation(); |
178 | op.addImage(imagePath); |
180 | op.resize( null , height); |
181 | } else if (height == null ) { |
184 | op.resize(width, height); |
186 | op.addImage(newPath); |
187 | ConvertCmd convert = new ConvertCmd( true ); |
190 | } catch (IOException e) { |
191 | System.out.println( "文件读取错误!" ); |
193 | } catch (InterruptedException e) { |
195 | } catch (IM4JavaException e) { |
213 | public boolean rotate(String imagePath, String newPath, double degree) { |
214 | boolean flag = false ; |
217 | degree = degree % 360 ; |
219 | degree = 360 + degree; |
221 | IMOperation op = new IMOperation(); |
222 | op.addImage(imagePath); |
224 | op.addImage(newPath); |
225 | ConvertCmd cmd = new ConvertCmd( true ); |
228 | } catch (Exception e) { |
230 | System.out.println( "图片旋转失败!" ); |
235 | public static void main(String[] args) throws Exception { |
236 | TestGm imageUtil = new TestGm(); |
238 | System.out.println( "原图片大小:" + imageUtil.getSize( "d://test.jpg" ) + "Bit" ); |
239 | System.out.println( "原图片宽度:" + imageUtil.getWidth( "d://test.jpg" )); |
240 | System.out.println( "原图片高度:" + imageUtil.getHeight( "d://test.jpg" )); |
241 | if (imageUtil.zoomImage( "d://test.jpg" , "d://test1.jpg" , 500 , null )) { |
242 | if (imageUtil.rotate( "d://test.jpg" , "d://test2.jpg" , 15 )) { |
243 | if (imageUtil.cutImage( "d://test2.jpg" , "d://test3.jpg" , 32 , |
245 | System.out.println( "编辑成功" ); |
247 | System.out.println( "编辑失败03" ); |
250 | System.out.println( "编辑失败02" ); |
253 | System.out.println( "编辑失败01" ); |
}
相关文章:
ImageMagick图片处理工具的安装 — http://elf8848.iteye.com/blog/455675
选择ImageMagick还是GraphicsMagick(翻译)— http://co63oc.blog.51cto.com/904636/328997
转:http://my.oschina.net/roaminlove/blog/96279