`
棋子chessman
  • 浏览: 195511 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

GraphicsMagick+IM4JAVA在mac环境安装使用及中文乱码

    博客分类:
  • GM
 
阅读更多

GM是什么,上网查查,IM4java是什么,也请上网查。以下的介绍同样适合其他语言使用gm或是ImageMagick的安装和中文乱码的情况。ImageMagick也测试通过

 

1、下载如下,并全部解压(参见官方依赖下载地址ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/delegates):

libpng-1.6.19.tar.gz 支持png格式

jpegsrc.v9.tar.gz支持Jpeg格式

ghostscript-9.15.tar.gz支持字体

GraphicsMagick-1.3.23.tar.gz主图片处理程序

freetype-2.6.2.tar.gz字体引擎

 

2、分别进入libpng、jpegsrc、freetype、ghostscript的解压目录,分别执行如下命令。

./configure

make

sudo make install

 

3、进入GraphicsMagick解压目录,执行如下命令。

./configure --prefix=/usr/local/GraphicsMagick-1.3.23 --with-quantum-depth=16   --enable-shared --enable-static  --with-windows-font-dir=/Library/Fonts --with-gslib=no

make

make install

 

注:

 --prefix:是指安装目录

 --with-quantum-depth:指图色深,16位还是8位

 --with-windows-font-dir:windows字体的安装目录

 

 GraphicsMagick执行完第一条./configure命令后,最后会有如下的内容,注意其中的Ghostscript、JPEG v1、PNG、FreeType的Configured value部分都应该是yes或是有值,这样才能基本支持jpeg\png和字体。

 

 

Option            Configure option           	Configured value
 -----------------------------------------------------------------
 Shared libraries  --enable-shared=yes    	yes
 Static libraries  --enable-static=yes    	yes
 GNU ld            --with-gnu-ld=no        	no
 Quantum depth     --with-quantum-depth=16 	16
 Modules           --with-modules=no        	no

 Delegate Configuration:
 BZLIB             --with-bzlib=yes          	yes
 DPS               --with-dps=yes              	no
 FlashPIX          --with-fpx=no              	no
 FreeType 2.0      --with-ttf=yes          	yes
 Ghostscript       None                   	gs (unknown)
 Ghostscript fonts --with-gs-font-dir=default    none
 Ghostscript lib   --with-gslib=no       	no
 JBIG              --with-jbig=yes        	no
 JPEG v1           --with-jpeg=yes        	yes
 JPEG-2000         --with-jp2=yes          	no
 LCMS v2           --with-lcms2=yes        	no
 LZMA              --with-lzma=yes        	no (failed tests)
 Magick++          --with-magick-plus-plus=yes 	yes
 PERL              --with-perl=no            	no
 PNG               --with-png=yes          	yes (-lpng16)
 TIFF              --with-tiff=yes        	no
 TRIO              --with-trio=yes        	no
 WEBP              --with-webp=yes        	no
 Windows fonts     --with-windows-font-dir=	none
 WMF               --with-wmf=yes          	no
 X11               --with-x=             	no
 XML               --with-xml=yes          	yes
 ZLIB              --with-zlib=yes        	yes

 X11 Configuration:

   Not using X11.

 Options used to compile and link:
   CC       = gcc
   CFLAGS   = -g -O2 -Wall -D_THREAD_SAFE
   CPPFLAGS = -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/libxml2
   CXX      = g++
   CXXFLAGS = -D_THREAD_SAFE
   DEFS     = -DHAVE_CONFIG_H
   LDFLAGS  = 
   LIBS     = -ljpeg -lpng16 -lbz2 -lxml2 -lz -lm -lpthread

 

 

   

4、配置环境变量(对于java中im4java调用,以下配置无意义,所以此步也可省略):

 

export GMAGICK_HOME=/usr/local/GraphicsMagick-1.3.18
export PATH="$GMAGICK_HOME/bin:$PATH" 

export IM4JAVA_TOOLPATH=$GMAGICK_HOME/bin/gm

 

 

 

5、终端中输入如下命令进行测试。

 

gm convert 111.jpg 222.png
gm convert 111.jpg -resize 100x100 out.jpg
gm convert -list formats
gm convert Arial -fill white -pointsize 24 -draw "text 10,15 'lifesinger 2006' "  /Users/xxx/Documents/temp/2.png /Users/xxx/Documents/temp/2new.png

 

6、JAVA代码例子。

注:网上的文章针对中文乱码的原因解释大多都是错的,首先可确定是GM如上版本是肯定支持中文水印之类的,也不存在什么单数中文和双数中文支持的问题。并且还看到有人先用AWT生成透明底的水印图片,再把主图与这个水印图片用GM合并。别的不说,用awt与gm的这两方案性能比较一下,你就会发现是数量级的差别,就先不提图片质量和大小等。

关键是两点:

a.要使用中文字体,使用非中文字体会乱码。

b.GM只接收UTF-8的内容,否则乱码,所以传的值要UTF-8编码。你可以整个*.java文件设置为utf-8编码,也可以对传入的水印内容String转成uft-8编码。如以下例子的WaterMark.java文件即为utf-8编码。

import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;
import org.im4java.process.Pipe;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class WaterMark {
    public static void main(String[] args) throws Exception {
        String src="/Users/xxx/Documents/temp/test/ban.jpg";  //需要加水印的源图片
        String desc="/Users/xxx/Documents/temp/test/new/abc"; //生成的水印图片的路径

//        String src1="/Users/xxx/Documents/temp/test/icon.jpg";  //需要加水印的源图片
//        String desc1="/Users/xxx/Documents/temp/test/new/icon"; //生成的水印图片的路径
        String content = "中文";

        for (int i = 0; i < 10; i++) {
            addImgText(src, content + i, desc + i + ".jpg");
        }

//        for (int i = 0; i < 1; i++) {
//            addImgText(src1, content + i, desc1 + i + ".jpg");
//        }
    }

    /**
     * 给图片加水印
     * @param srcPath   源图片路径
     */
    public static void addImgText(String srcPath, String content, String desc) throws Exception {
        IMOperation op = new IMOperation();
        op.font("/Library/Fonts/Yuanti.ttc").gravity("southeast").pointsize(18).fill("#BCBFC8").draw("text 10,15 '" + content + "'").quality(90.0);
        op.addImage();
        op.addImage();

        ConvertCmd cmd = new ConvertCmd(true);

        cmd.setAsyncMode(true);
        cmd.setSearchPath("/usr/local/GraphicsMagick-1.3.18/bin");// 如果是windows则需要设置convert路径

        try {
            cmd.run(op,srcPath,desc);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IM4JavaException e) {

            e.printStackTrace();
        }
    }

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics