ImageMagick主页:
http://www.imagemagick.org/script/index.php
这两篇文章介绍了ImageMagick的基本使用
http://www.ibm.com/developerworks/cn/linux/l-graf/
http://www.ibm.com/developerworks/cn/linux/l-graf2/
由于ImageMagick中有些命令与系统中的一些命令相冲突,如:convert命令,因此可以使用GraphicsMagick,其主页:http://www.graphicsmagick.org/,其命令用法和ImageMagick命令的用法基本一样,只在其前面加入了gm,为了避免和系统命令相冲突。
命令参考:
http://www.graphicsmagick.org/utilities.html
例如:
(1)gm identify
可以识别图片的基本信息
如:
gm identify firelily.jpg 显示基本图片信息
gm identify -format "%d %b %f %w %h %[EXIF:DateTimeOriginal]" firelily.jpg 显示指定的图片信息
-format选项可以参考官方文档
(2)gm convert
转换图片
如:
gm convert -colors 256 -depth 8 -resize 50% -quality 70 firelily.jpg test1.jpg
colors:指颜色数
depth:颜色深度数
resize:大小
quality:质量
gm convert -resize 100 -crop 100X100+0+0 -gravity center -quality 70 firelily.jpg test.jpg
resize 100:高度按照宽度缩放比例进行调整
crop:截取
100X100+0+0 指截取的宽度X截取的宽度+从图片左上角起的横坐标+从图片左上角起的纵坐标
gravity center:从中心截取
gm convert -resize 100X100! firelily.jpg test.jpg
加!号,表明不按比例对图片进行缩放
================================
ImageMagick给许多编程语言提供了编程API,对Java提供了两种:
(1)JMagic,是基于JNI的对C程序的封装调用
(2)im4java,是对ImageMagick的命令行调用进行的java封装
在这里介绍下im4java,其代码示例很少,可以看其官方提供的测试程序,网上的示例也很少,最直接的方法是读其源代码。
im4java主页:http://im4java.sourceforge.net/
源代码中有一个属性:im4java.useGM,为true,表明使用GraphicsMagick,否则使用ImageMagick
示例1:
显示图片信息
view plaincopy to clipboardprint? public static String showImageInfo(String imagePath) throws ImageProcessException { try { IMOperation op = new IMOperation(); op.format("%w#%h#%d#%f#%b#%[EXIF:DateTimeOriginal]"); op.addImage(1); IdentifyCmd identifyCmd = new IdentifyCmd(); ArrayListOutputConsumer output = new ArrayListOutputConsumer(); identifyCmd.setOutputConsumer(output); logger.info("==ImageMagic==\n" + "Op=" + op.toString() + ";Cmd=" + identifyCmd.toString()); identifyCmd.run(op, imagePath); ArrayList<String> cmdOutput = output.getOutput(); assert cmdOutput.size() == 1; String line = cmdOutput.get(0); return line; } catch (Exception e) { throw new ImageProcessException(e); } public static String showImageInfo(String imagePath) throws ImageProcessException { try { IMOperation op = new IMOperation(); op.format("%w#%h#%d#%f#%b#%[EXIF:DateTimeOriginal]"); op.addImage(1); IdentifyCmd identifyCmd = new IdentifyCmd(); ArrayListOutputConsumer output = new ArrayListOutputConsumer(); identifyCmd.setOutputConsumer(output); logger.info("==ImageMagic==\n" + "Op=" + op.toString() + ";Cmd=" + identifyCmd.toString()); identifyCmd.run(op, imagePath); ArrayList<String> cmdOutput = output.getOutput(); assert cmdOutput.size() == 1; String line = cmdOutput.get(0); return line; } catch (Exception e) { throw new ImageProcessException(e); }
示例2:
旋转图片
view plaincopy to clipboardprint? public static void rotate(String srcImagePath, String destImagePath, double angle) throws ImageProcessException { try { IMOperation op = new IMOperation(); op.rotate(angle); op.addImage(srcImagePath); op.addImage(destImagePath); ConvertCmd cmd = new ConvertCmd(); logger.info("==ImageMagic==\n" + "Op=" + op.toString() + ";Cmd=" + cmd.toString()); cmd.run(op); } catch (Exception e) { throw new ImageProcessException(e); } } public static void rotate(String srcImagePath, String destImagePath, double angle) throws ImageProcessException { try { IMOperation op = new IMOperation(); op.rotate(angle); op.addImage(srcImagePath); op.addImage(destImagePath); ConvertCmd cmd = new ConvertCmd(); logger.info("==ImageMagic==\n" + "Op=" + op.toString() + ";Cmd=" + cmd.toString()); cmd.run(op); } catch (Exception e) { throw new ImageProcessException(e); } }
示例3:
调整大小,并改变图片质量
view plaincopy to clipboardprint? public static String resize(String srcImagePath, String destImagePath, int width, int height, double quality, int colors, int depth, boolean isProportionChanged, boolean isUseFileExt) throws ImageProcessException { try { IMOperation op = new IMOperation(); if (width > 0) { if (height > 0) { if (!isProportionChanged) { op.resize(width, height); op.crop(width, height, 0, 0); } else { op.resize(width, height, '!'); op.crop(width, height, 0, 0, '!'); } } else { op.resize(width); op.crop(width); } } op.gravity("center"); op.quality(quality); op.colors(colors); op.depth(depth); op.addImage(srcImagePath); if (!isUseFileExt) { String extension = FilenameUtils.getExtension(destImagePath); destImagePath = StringUtils.removeEnd(destImagePath, extension) + Constants.DEFAULT_EXT; } op.addImage(destImagePath); ConvertCmd convCmd = new ConvertCmd(); logger.info("==ImageMagic==\n" + "Op=" + op.toString() + ";Cmd=" + convCmd.toString()); convCmd.run(op); return destImagePath; } catch (Exception e) { throw new ImageProcessException(e); } }