klxedu / blog

2 stars 0 forks source link

IM4JAVA在windows下调用ImageMagick命令异常问题。convert: unable to load module 'C:\Program Files\ImageMagick-7.0.5-Q16\modules\coders\IM_MOD_RL_JPEG_.dll' #16

Open songhuiqing88 opened 7 years ago

songhuiqing88 commented 7 years ago

异常描述

在windows下使用im4java或java cmd方法执行imageMagick convert命令异常。但是使用cmd窗口直接转换是正常的,具体异常如下:

Exception in thread "main" org.im4java.core.CommandException: org.im4java.core.CommandException: convert: unable to load module 'C:\Program Files\ImageMagick-7.0.5-Q16\modules\coders\IM_MOD_RL_JPEG_.dll': �Ҳ���ָ����ģ�顣
    at org.im4java.core.ImageCommand.run(ImageCommand.java:219)
    at org.gtiles.components.mediaservices.cmd.Im4Java.resize(Im4Java.java:81)
    at org.gtiles.components.mediaservices.cmd.Im4Java.main(Im4Java.java:37)
Caused by: org.im4java.core.CommandException: convert: unable to load module 'C:\Program Files\ImageMagick-7.0.5-Q16\modules\coders\IM_MOD_RL_JPEG_.dll': �Ҳ���ָ����ģ�顣
    at org.im4java.core.ImageCommand.finished(ImageCommand.java:253)
    at org.im4java.process.ProcessStarter.run(ProcessStarter.java:314)
    at org.im4java.core.ImageCommand.run(ImageCommand.java:215)
    ... 2 more

解决过程

通过伟大的google,最终找到解决方案如下。在执行Processbuilder.start()之前,设置执行目录。

if (isWindows) {
    builder.inheritIO();
    builder.redirectErrorStream();
    if (super.getSearchPath() != null) {
        builder.directory(new File(super.getSearchPath()));
    } else if (super.getGlobalSearchPath() != null) {
        builder.directory(new File(super.getGlobalSearchPath()));
    }
}