GraphicsMagick+im4java实现高质量大图的处理

下载 .tar.gz 的源码包,进行解压

tar -xvzf GraphicsMagick-1.3.12.tar.gz

解压后,原来在的gz文件就变成了tar文件,进入文件夹

cd GraphicsMagick-1.3.12

安装之前,因为是图片处理,所以需要系统中安装了libpng和libjpeg的开发包,否则的话不会安装这两种文件的支持。

使用 configure 来进行自动的配置、build和安装

./configure

当然,可以通过 –prefix=PATH 来指定参数,还可以指定其他编译时的变量,这里使用了一个经过测试的 configure 配置,同时添加了 enable-sybol-prefix ,这样就避免了和系统中已有的 ImageMagick 的冲突,下面是完成的配置参数:

./configure  '--build=i686-redhat-linux-gnu' '--host=i686-redhat-linux-gnu' '--target=i386-redhat-linux-gnu' '--program-prefix=' '--prefix=/usr/local/sinasrv2' '--exec-prefix=/usr/local/sinasrv2' '--bindir=/usr/local/sinasrv2/bin' '--sbindir=/usr/local/sinasrv2/sbin' '--sysconfdir=/usr/local/sinasrv2/etc' '--datadir=/usr/local/sinasrv2/share' '--includedir=/usr/local/sinasrv2/include' '--libdir=/usr/local/sinasrv2/lib' '--libexecdir=/usr/local/sinasrv2/libexec' '--localstatedir=/usr/local/sinasrv2/var' '--sharedstatedir=/usr/local/sinasrv2/share/com' '--mandir=/usr/local/sinasrv2/share/man' '--infodir=/usr/local/sinasrv2/share/info' '--enable-libtool-verbose' '--with-included-ltdl' '--enable-shared' '--disable-static' '--with-modules' '--with-frozenpaths' '--without-perl' '--without-magick-plus-plus' '--with-quantum-depth=8' --enable-symbol-prefix

接下来就是安装

make

make install

 

程序中代码:

001 package imageUtils; 002 003 import java.io.IOException; 004 import java.util.ArrayList; 005 006 import org.im4java.core.ConvertCmd; 007 import org.im4java.core.IMOperation; 008 009 /** 010 * @author hegh E-mail: heguanhua@tjhq.com 011 * @version 创建时间:Mar 13, 2012 10:43:12 AM 类说明 012 */ 013 public class ImageMagick { 014 015 /** * ImageMagick的路径 */ 016 publicstatic String imageMagickPath =null; 017 static{/**获取ImageMagick的路径 */ 018 //Properties prop = new PropertiesFile().getPropertiesFile(); 019 //linux下不要设置此值,不然会报错 020 //imageMagickPath = prop.getProperty("imageMagickPath"); 021 } 022 023 /** * 根据坐标裁剪图片 024 * @param srcPath 要裁剪图片的路径 025 * @param newPath 裁剪图片后的路径 026 * @param x 起始横坐标 027 * @param y 起始挫坐标 028 * @param x1 结束横坐标 029 * @param y1 结束挫坐标 030 */ 031 publicstatic void cutImage(String srcPath, String newPath, intx, int y, int x1, int y1) 032 throwsException { 033 intwidth = x1 - x; intheight = y1 - y; 034 IMOperation op =new IMOperation(); 035 op.addImage(srcPath); 036 /** 037 * width:裁剪的宽度 038 * height:裁剪的高度 039 * x:裁剪的横坐标 040 * y:裁剪的挫坐标 041 */ 042 op.crop(width, height, x, y); 043 op.addImage(newPath); 044 ConvertCmd convert =new ConvertCmd(); 045 //linux下不要设置此值,不然会报错 046 //convert.setSearchPath(imageMagickPath); 047 convert.run(op); 048 } 049 050 /** 051 * 根据尺寸缩放图片 052 * @param width 缩放后的图片宽度 053 * @param height 缩放后的图片高度 054 * @param srcPath 源图片路径 055 * @param newPath 缩放后图片的路径 056 * @param type 1为比例处理,2为大小处理,如(比例:1024x1024,大小:50%x50%) 057 */ 058 publicstatic String cutImage(intwidth, int height, String srcPath, String newPath,inttype,String quality) throwsException { 059 IMOperation op =new IMOperation(); 060 ConvertCmd cmd =new ConvertCmd(true); 061 op.addImage(); 062 String raw =""; 063 if(type ==1){ 064 //按像素 065 raw = width+"x"+height+"^"; 066 }else{ 067 //按像素百分比 068 raw = width+"%x"+height+"%"; 069 } 070 op.addRawArgs("-sample", raw ); 071 if((quality !=null&& quality.equals(""))){ 072 op.addRawArgs("-quality", quality ); 073 } 074 op.addImage(); 075 076 String osName = System.getProperty("os.name").toLowerCase(); 077 if(osName.indexOf("win") != -1) { 078 //linux下不要设置此值,不然会报错 079 cmd.setSearchPath("C://Program Files//GraphicsMagick-1.3.14-Q16"); 080 } 081 082 try{ 083 cmd.run(op, srcPath, newPath); 084 }catch(Exception e){ 085 e.printStackTrace(); 086 } 087 returnnewPath; 088 } 089 090 091 /** 092 * 给图片加水印 093 * @param srcPath 源图片路径 094 */ 095 publicstatic void addImgText(String srcPath) throws Exception { 096 IMOperation op =new IMOperation(); 097 op.font("宋体").gravity("southeast").pointsize(18).fill("#BCBFC8").draw("text 100,100 co188.com"); 098 op.addImage(); 099 op.addImage(); 100 101 String osName = System.getProperty("os.name").toLowerCase(); 102 ConvertCmd cmd =new ConvertCmd(true); 103 if(osName.indexOf("win") != -1) { 104 //linux下不要设置此值,不然会报错 105 cmd.setSearchPath("C://Program Files//GraphicsMagick-1.3.14-Q16"); 106 } 107 108 try{ 109 cmd.run(op, srcPath, srcPath); 110 }catch(Exception e){ 111 e.printStackTrace(); 112 } 113 } 114 115 publicstatic void main(String[] args) throws Exception{ 116 //cutImage("D:\\apple870.jpg", "D:\\apple870eee.jpg",98, 48, 370, 320); 117 Long start = System.currentTimeMillis(); 118 //cutImage(100,100, "e:\\37AF7D10F2D8448A9A5.jpg","e:\\37AF7D10F2D8448A9A5_bak2.jpg",2,"100"); 119 addImgText("e:\\37AF7D10F2D8448A9A5_bak2.jpg"); 120 Long end = System.currentTimeMillis(); 121 System.out.println("time:"+(end-start)/3600); 122 } 123 } 124 通过GraphicsMagick+im4java实现高质量大图的处理,解决了100M以上,以及图片像素10000以上处理是出现内存溢出的问


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值