簡體   English   中英

使用im4java進行圖像比較

[英]Image comparison using im4java

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將其與預期的屏幕截圖進行比較,但即使實際和預期的圖像相似,它仍然會拋出錯誤,差異圖像也不會生成

我在temp文件夾中添加了預期的屏幕截圖。

有人可以建議我做錯了什么。 提前致謝

我花了太多時間來解決這個問題。 解決方案是:

  1. 您應該完全安裝ImageMagick並選中所有復選框。
  2. 您應該將其安裝路徑設置為環境變量系統路徑。
  3. 可選:重新啟動PC以使這些設置正常工作。

通過這些步驟,我解決了我的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM