我正在尝试运行 im4java 来比较图像,但我收到以下异常消息。
我已按照以下链接添加 ImageMagick 的步骤:https ://www.imagemagick.org/script/download.php#macosx
我遇到了同样的错误,所以我在 eclipse 的运行配置中添加了环境变量:
DYLD_LIBRARY_PATH=/Users/siva/Downloads/ImageMagick-7.0.5/lib 并开始运行,但我仍然收到相同的错误消息:
我也试过:
System.setProperty("java.library.path", "/Users/siva/Downloads/ImageMagick-7.0.5/bin/magick");
如果有人尝试在 Mac 上使用 imageMagick,请建议我必须执行哪些步骤。
我能够从终端成功运行以下命令:比较 imageone.png imagetwo.png diffimage.png
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import org.apache.commons.lang.StringUtils;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;
import org.im4java.core.ImageMagickCmd;
import org.im4java.core.Info;
import org.im4java.process.ArrayListOutputConsumer;
public class UseImageM {
private static final String COMPARE_COMMAND = "compare";
private static final String ABSOLUTE_ERROR = "AE";
private static String imageOnePath = "/Users/siva/Documents/imageEclipse/UsingImagemagick/screenshot.png";
private static String imageTwoPath = "/Users/siva/Documents/imageEclipse/UsingImagemagick/screenshotfinal.png";
private static String outputImageName = "outputoftheimageCompare";
private static final int COUNT_OF_DECIMAL = 10;
public static void main(String[] args) {
IMOperation imOperation = new IMOperation();
imOperation.metric(ABSOLUTE_ERROR);
imOperation.addImage(imageOnePath);
imOperation.addImage(imageTwoPath);
imOperation.addImage(outputImageName);
ImageMagickCmd compare = new ImageMagickCmd(COMPARE_COMMAND);
ArrayListOutputConsumer outputConsumer = new ArrayListOutputConsumer();
compare.setOutputConsumer(outputConsumer);
BigDecimal diffPercentage = BigDecimal.ZERO;
Info imageInfo;
long imageSize = 0;
try {
imageInfo = new Info(imageTwoPath, true);
imageSize = imageInfo.getImageWidth() * imageInfo.getImageHeight();
compare.run(imOperation);
final ArrayList<String> errorText = compare.getErrorText();
if (errorText.size() == 1) {
final BigDecimal diffCount = new BigDecimal(errorText.get(0));
if (!BigDecimal.ZERO.equals(diffCount)) {
diffPercentage = diffCount.divide(
new BigDecimal(imageSize), COUNT_OF_DECIMAL,
BigDecimal.ROUND_DOWN);
}
}
} catch (final IOException | InterruptedException e) {
System.out.println((String.format(
"Exception happened in comparison between %s and %s.",
imageOnePath, imageTwoPath) + e));
} catch (final IM4JavaException e) {
e.printStackTrace();
// error message
if (!StringUtils.isNumeric(compare.getErrorText().get(0))) {
final String errorMsg = String.format(
"Exception happened in comparison between %s and %s.",
imageOnePath, imageTwoPath);
System.out.println(errorMsg + e);
}
final BigDecimal diffCount = new BigDecimal(compare.getErrorText()
.get(0));
diffPercentage = diffCount.divide(new BigDecimal(imageSize),
COUNT_OF_DECIMAL, BigDecimal.ROUND_DOWN);
}
System.out.println(diffPercentage);
}
}
Console Logs:
org.im4java.core.InfoException: org.im4java.core.CommandException: java.io.IOException: Cannot run program "identify": error=2, No such file or directory
at org.im4java.core.Info.getBaseInfo(Info.java:360)
at org.im4java.core.Info.<init>(Info.java:151)
at UseImageM.main(UseImageM.java:36)
Caused by: org.im4java.core.CommandException: java.io.IOException: Cannot run program "identify": error=2, No such file or directory
at org.im4java.core.ImageCommand.run(ImageCommand.java:219)
at org.im4java.core.Info.getBaseInfo(Info.java:342)
... 2 more
Caused by: java.io.IOException: Cannot run program "identify": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at org.im4java.process.ProcessStarter.startProcess(ProcessStarter.java:407)
at org.im4java.process.ProcessStarter.run(ProcessStarter.java:312)
at org.im4java.core.ImageCommand.run(ImageCommand.java:215)
... 3 more
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:247)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 6 more
Exception in thread "main" java.lang.NullPointerException
at UseImageM.main(UseImageM.java:60)