切图异常: Cannot run program "gm": error=2, No such file or directory 处理

切图异常: Cannot run program “gm”: error=2, No such file or directory 处理

使用im4java、ImageMagick/GraphicsMagick进行切图,结果在测试环境运行时提示gm找不到这个异常,

org.im4java.core.CommandException: java.io.IOException: Cannot run program "gm": error=2, No such file or directory
    at org.im4java.core.ImageCommand.run(ImageCommand.java:219) ~[im4java-1.4.0.jar:na]
    at com.test.craw.utils.ImageUtils.zoomImage(ImageUtils.java:182) [classes/:na]
    at com.test.craw.utils.ImageUtils.zoomImageWidth(ImageUtils.java:202) [classes/:na]
    at com.test.craw.service.BeautyService.crop(BeautyService.java:205) [classes/:na]
    at com.test.craw.service.BeautyService.updateGallery(BeautyService.java:107) [classes/:na]
    at com.test.craw.task.TestSpiderTask$TestBeautyThread.run(TestSpiderTask.java:113) [classes/:na]
    at java.lang.Thread.run(Thread.java:745) [na:1.7.0_80]
Caused by: java.io.IOException: Cannot run program "gm": error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047) ~[na:1.7.0_80]
    at org.im4java.process.ProcessStarter.startProcess(ProcessStarter.java:407) ~[im4java-1.4.0.jar:na]
    at org.im4java.process.ProcessStarter.run(ProcessStarter.java:312) ~[im4java-1.4.0.jar:na]
    at org.im4java.core.ImageCommand.run(ImageCommand.java:215) ~[im4java-1.4.0.jar:na]
    ... 6 common frames omitted
Caused by: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.forkAndExec(Native Method) ~[na:1.7.0_80]
    at java.lang.UNIXProcess.(UNIXProcess.java:187) ~[na:1.7.0_80]
    at java.lang.ProcessImpl.start(ProcessImpl.java:130) ~[na:1.7.0_80]
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028) ~[na:1.7.0_80]
    ... 9 common frames omitted
[ERROR][2017-08-15 14:30:24] - [com.test.craw.service.BeautyService] - 切图失败:Logger[com.test.craw.service.BeautyService]
java.lang.Exception: org.im4java.core.CommandException: java.io.IOException: Cannot run program "gm": error=2, No such file or directory
    at com.test.craw.utils.ImageUtils.zoomImage(ImageUtils.java:191) ~[classes/:na]
    at com.test.craw.utils.ImageUtils.zoomImageWidth(ImageUtils.java:202) ~[classes/:na]
    at com.test.craw.service.BeautyService.crop(BeautyService.java:205) [classes/:na]
    at com.test.craw.service.BeautyService.updateGallery(BeautyService.java:107) [classes/:na]
    at com.test.craw.task.TestSpiderTask$TestBeautyThread.run(TestSpiderTask.java:113) [classes/:na]
    at java.lang.Thread.run(Thread.java:745) [na:1.7.0_80]
Caused by: org.im4java.core.CommandException: java.io.IOException: Cannot run program "gm": error=2, No such file or directory
    at org.im4java.core.ImageCommand.run(ImageCommand.java:219) ~[im4java-1.4.0.jar:na]
    at com.test.craw.utils.ImageUtils.zoomImage(ImageUtils.java:182) ~[classes/:na]
    ... 5 common frames omitted
Caused by: java.io.IOException: Cannot run program "gm": error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047) ~[na:1.7.0_80]
    at org.im4java.process.ProcessStarter.startProcess(ProcessStarter.java:407) ~[im4java-1.4.0.jar:na]
    at org.im4java.process.ProcessStarter.run(ProcessStarter.java:312) ~[im4java-1.4.0.jar:na]
    at org.im4java.core.ImageCommand.run(ImageCommand.java:215) ~[im4java-1.4.0.jar:na]
    ... 6 common frames omitted
Caused by: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.forkAndExec(Native Method) ~[na:1.7.0_80]
    at java.lang.UNIXProcess.(UNIXProcess.java:187) ~[na:1.7.0_80]
    at java.lang.ProcessImpl.start(ProcessImpl.java:130) ~[na:1.7.0_80]
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028) ~[na:1.7.0_80]
    ... 9 common frames omitted

在命令行直接输入gm,能正常输入信息

GraphicsMagick 1.3.26 2017-07-04 Q8 http://www.GraphicsMagick.org/
Copyright (C) 2002-2017 GraphicsMagick Group.
Additional copyrights and licenses apply to this software.
See http://www.GraphicsMagick.org/www/Copyright.html for details.
Usage: gm command [options ...]

Where commands include: 
    animate - animate a sequence of images
      batch - issue multiple commands in interactive or batch mode
  benchmark - benchmark one of the other commands
    compare - compare two images
  composite - composite images together
    conjure - execute a Magick Scripting Language (MSL) XML script
    convert - convert an image or sequence of images
    display - display an image on a workstation running X
       help - obtain usage message for named command
   identify - describe an image or image sequence
     import - capture an application or X server screen
    mogrify - transform an image or sequence of images
    montage - create a composite image (in a grid) from separate images
       time - time one of the other commands
    version - obtain release version

在环境变量文件/etc/profile中页设置了对应的环境变量

export GMAGICK_HOME="/usr/local/GraphicsMagick-1.3.26"
export PATH="$GMAGICK_HOME/bin:$PATH"
LD_LIBRARY_PATH=$GMAGICK_HOME/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH 
export IM4JAVA_TOOLPATH=/usr/local/GraphicsMagick-1.3.26/bin

通过验证,发现需要手动在ConvertCmd 初始化后,设置一个命令查询目录【convert.setSearchPath(“/usr/local/GraphicsMagick-1.3.26/bin”);】

IMOperation op = new IMOperation();
op.addImage(imagePath);
op.crop(width, height, x, y);
op.addImage(newPath);
ConvertCmd convert = new ConvertCmd(true);

convert.setSearchPath("/usr/local/GraphicsMagick-1.3.26/bin");

convert.run(op);

不仅仅是ConvertCmd 需要添加, 所有继承 ImageCommand 的类,使用中都需要,比如:IdentifyCmd

下面是继承 ImageCommand 的所有类;
切图异常: Cannot run program

使用的时候最好加上 setSearchPath("/usr/local/GraphicsMagick-1.3.26/bin")

再次运行,就不会提示异常了!

你可能感兴趣的:(图片处理,异常处理)