【问题标题】:Image comparison using im4java使用 im4java 进行图像比较
【发布时间】:2016-02-04 12:45:43
【问题描述】:
public class im4jav extends test{

public static void main(String[] args) throws IOException {

    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.google.co.in/?gfe_rd=cr&ei=fkKzVqasB8yl8wefrraoCg&gws_rd=ssl");

    File scrFile = ((TakesScreenshot)driver).
    getScreenshotAs(OutputType.FILE);

    String current = "c:/temp/image.png";
    FileUtils.copyFile(scrFile, new File(current));


    boolean compareSuccess = compareImages("c:/temp/expected.png", current, "c:/temp/difference.png");

    System.out.println("run ok ? :"+compareSuccess);

    driver.close();


}}class test{

static boolean compareImages (String exp, String cur, String diff) {
      // This instance wraps the compare command
      CompareCmd compare = new CompareCmd();

      // For metric-output
      compare.setErrorConsumer(StandardStream.STDERR);
      IMOperation cmpOp = new IMOperation();
      // Set the compare metric
      cmpOp.metric("mae");

      // Add the expected image
      cmpOp.addImage(exp);

      // Add the current image
      cmpOp.addImage(cur);

      // This stores the difference
      cmpOp.addImage(diff);

      try {
        // Do the compare
        compare.run(cmpOp);
        return true;
      }
      catch (Exception ex) {
        return false;
      }
    }

}

大家好,

在上面的代码中,我试图截取页面的屏幕截图并使用 im4java 将其与预期的屏幕截图进行比较,但是即使实际图像和预期图像相似,它仍然会抛出错误并且也不会生成差异图像

我在临时文件夹中添加了预期的屏幕截图。

有人可以建议我做错了什么。提前致谢

【问题讨论】:

  • 异常说明了什么?在return false;之前使用ex.printStacktrace()
  • org.im4java.core.CommandException: java.io.IOException: Cannot run program "compare": CreateProcess error=2, 系统找不到指定的文件
  • The system cannot find the file specified 可能其中一张图片不存在。确保当前和预期的图像确实存在于给定路径中
  • 我遇到了同样的错误,两个图像都存在于路径中。有人帮忙吗?

标签: java image-comparison im4java


【解决方案1】:

我花了太多时间来解决问题。解决办法是:

  1. 您应该通过选中所有复选框来完全安装 ImageMagick。
  2. 您应该将其安装路径设置为环境变量 系统路径。
  3. 可选:重新启动您的 PC 以使这些设置生效 正确。

通过这些步骤,我解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-29
    • 2011-03-29
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多