java 大图片处理GraphicsMagick + im4java [缩放,旋转,裁剪]

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机的情况。 
jmagick

im4java

主页:http://im4java.sourceforge.net/

下载地址:http://sourceforge.net/projects/im4java/files/

API:http://im4java.sourceforge.net/api/

im4java

用那个不用说吧,看更新时间,不知道你们会选择什么

所以我选用了 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加入到我们的项目引用里面就可以了 
jar包

根据别人的代码,自己再封装了一下接口吧,基本够用,那些水印的就没加上去了,现在不需要 
工具很强大,都是靠指令,所以多看看API和软件本身的使用吧

好吧,我忽悠完了,把时间交给你们了. 
附上代码:[好纠结的代码编辑器,只能这样了]

1 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 {

001 /**
002  * * 获得图片文件大小[小技巧来获得图片大小] * * @param filePath * 文件路径 *
003  *
004  * @return 文件大小
005  */
006  
007 public int getSize(String imagePath) {
008     int size = 0;
009     FileInputStream inputStream = null;
010     try {
011         inputStream = new FileInputStream(imagePath);
012         size = inputStream.available();
013         inputStream.close();
014         inputStream = null;
015     catch (FileNotFoundException e) {
016         size = 0;
017         System.out.println("文件未找到!");
018     catch (IOException e) {
019         size = 0;
020         System.out.println("读取文件大小错误!");
021     finally {
022         // 可能异常为关闭输入流,所以需要关闭输入流
023         if (null != inputStream) {
024             try {
025                 inputStream.close();
026             catch (IOException e) {
027                 System.out.println("关闭文件读入流异常");
028             }
029             inputStream = null;
030  
031         }
032     }
033     return size;
034 }
035  
036 /**
037  * 获得图片的宽度
038  *
039  * @param filePath
040  *            文件路径
041  * @return 图片宽度
042  */
043 public int getWidth(String imagePath) {
044     int line = 0;
045     try {
046         IMOperation op = new IMOperation();
047         op.format("%w"); // 设置获取宽度参数
048         op.addImage(1);
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) {
057         line = 0;
058         System.out.println("运行指令出错!");
059     }
060     return line;
061 }
062  
063 /**
064  * 获得图片的高度
065  *
066  * @param imagePath
067  *            文件路径
068  * @return 图片高度
069  */
070 public int getHeight(String imagePath) {
071     int line = 0;
072     try {
073         IMOperation op = new IMOperation();
074  
075         op.format("%h"); // 设置获取高度参数
076         op.addImage(1);
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) {
085         line = 0;
086         System.out.println("运行指令出错!"+e.toString());
087     }
088     return line;
089 }
090  
091 /**
092  * 图片信息
093  *
094  * @param imagePath
095  * @return
096  */
097 public static String getImageInfo(String imagePath) {
098     String line = null;
099     try {
100         IMOperation op = new IMOperation();
101         op.format("width:%w,height:%h,path:%d%f,size:%b%[EXIF:DateTimeOriginal]");
102         op.addImage(1);
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);
110  
111     catch (Exception e) {
112         e.printStackTrace();
113     }
114     return line;
115 }
116  
117 /**
118  * 裁剪图片
119  *
120  * @param imagePath
121  *            源图片路径
122  * @param newPath
123  *            处理后图片路径
124  * @param x
125  *            起始X坐标
126  * @param y
127  *            起始Y坐标
128  * @param width
129  *            裁剪宽度
130  * @param height
131  *            裁剪高度
132  * @return 返回true说明裁剪成功,否则失败
133  */
134 public boolean cutImage(String imagePath, String newPath, int x, int y,
135         int width, int height) {
136     boolean flag = false;
137     try {
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);
144         convert.run(op);
145         flag = true;
146     catch (IOException e) {
147         System.out.println("文件读取错误!");
148         flag = false;
149     catch (InterruptedException e) {
150         flag = false;
151     catch (IM4JavaException e) {
152         flag = false;
153     finally {
154  
155     }
156     return flag;
157 }
158  
159 /**
160  * 根据尺寸缩放图片[等比例缩放:参数height为null,按宽度缩放比例缩放;参数width为null,按高度缩放比例缩放]
161  *
162  * @param imagePath
163  *            源图片路径
164  * @param newPath
165  *            处理后图片路径
166  * @param width
167  *            缩放后的图片宽度
168  * @param height
169  *            缩放后的图片高度
170  * @return 返回true说明缩放成功,否则失败
171  */
172 public boolean zoomImage(String imagePath, String newPath, Integer width,
173         Integer height) {
174  
175     boolean flag = false;
176     try {
177         IMOperation op = new IMOperation();
178         op.addImage(imagePath);
179         if (width == null) {// 根据高度缩放图片
180             op.resize(null, height);
181         else if (height == null) {// 根据宽度缩放图片
182             op.resize(width);
183         else {
184             op.resize(width, height);
185         }
186         op.addImage(newPath);
187         ConvertCmd convert = new ConvertCmd(true);
188         convert.run(op);
189         flag = true;
190     catch (IOException e) {
191         System.out.println("文件读取错误!");
192         flag = false;
193     catch (InterruptedException e) {
194         flag = false;
195     catch (IM4JavaException e) {
196         flag = false;
197     finally {
198  
199     }
200     return flag;
201 }
202  
203 /**
204  * 图片旋转
205  *
206  * @param imagePath
207  *            源图片路径
208  * @param newPath
209  *            处理后图片路径
210  * @param degree
211  *            旋转角度
212  */
213 public boolean rotate(String imagePath, String newPath, double degree) {
214     boolean flag = false;
215     try {
216         // 1.将角度转换到0-360度之间
217         degree = degree % 360;
218         if (degree <= 0) {
219             degree = 360 + degree;
220         }
221         IMOperation op = new IMOperation();
222         op.addImage(imagePath);
223         op.rotate(degree);
224         op.addImage(newPath);
225         ConvertCmd cmd = new ConvertCmd(true);
226         cmd.run(op);
227         flag = true;
228     catch (Exception e) {
229         flag = false;
230         System.out.println("图片旋转失败!");
231     }
232     return flag;
233 }
234  
235 public static void main(String[] args) throws Exception {
236     TestGm imageUtil = new TestGm();
237  
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"500null)) {
242         if (imageUtil.rotate("d://test.jpg""d://test2.jpg"15)) {
243             if (imageUtil.cutImage("d://test2.jpg""d://test3.jpg"32,
244                     105200200)) {
245                 System.out.println("编辑成功");
246             else {
247                 System.out.println("编辑失败03");
248             }
249         else {
250             System.out.println("编辑失败02");
251         }
252     else {
253         System.out.println("编辑失败01");
254     }
255  
256 }

}

相关文章:

ImageMagick图片处理工具的安装 — http://elf8848.iteye.com/blog/455675

选择ImageMagick还是GraphicsMagick(翻译)— http://co63oc.blog.51cto.com/904636/328997


转:http://my.oschina.net/roaminlove/blog/96279

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值