比较Sikuli中的两幅图像

时间:2017-11-14 05:48:04

标签: java automation sikuli

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.sikuli.basics.Settings;
import org.sikuli.natives.OCR;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Finder;
import org.sikuli.script.Match;
import org.sikuli.script.Pattern;
import org.sikuli.script.Region;
import org.sikuli.script.Screen;

public class DesktopAutomate {

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

        Screen screen = new Screen();
        // here is the cheque
        Pattern pattern = new Pattern("C:\\Users\\vk250281\\Desktop\\VIEWER\\hdfc-bank-cheque-new.PNG");

        // String file1 = "C:\\Users\\vk250281\\Desktop\\website\\new.jpg";

        Pattern pa1 = new Pattern("C:\\Users\\vk250281\\Desktop\\VIEWER\\Capture11.PNG");

        // Image image1 = Toolkit.getDefaultToolkit().getImage(file1);

        System.setProperty("webdriver.chrome.driver",
                "C:\\Users\\vk250281\\Downloads\\chromedriver_win32\\chromedriver.exe");

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-fullscreen");

        WebDriver driver = new ChromeDriver(options);
        // to on ocr settings

        Settings.OcrTextSearch = true;

        Settings.OcrTextRead = true;

        // here i was trying to break image in text but that to is not producing
        // expected results

        driver.get("file:///C:/Users/vk250281/Desktop/VIEWER/ImageViewer_dev.html");

        WebElement webElement = driver.findElement(By.xpath("//*[@id=\"frontImageViewer\"]/div[2]/div/img[1]"));

        Thread.sleep(5000);// wait added for taking proper screen shot

        /*
         * Finder finder1 = new Finder(pa1.getImage());// to find the image on
         * the screenshot captured
         * 
         * 
         * if(finder1.find(pa1) != null) { System.out.println("aaaa");
         * screen.mouseMove(pa1);
         * 
         * screen.click(pa1);
         * 
         * //screen.click(pa1);
         * 
         * }
         */

        ImageIO.write(screen.capture().getImage(), "PNG", new File(System.getProperty("user.dir")
                + "/TestImages/test.png"));
        /*
         * OCR ocr = OCR. // can be set to "numbers" if you need only digits to
         * be read String text = ocr.read(new Rectangle(200, 300, 100, 50));
         * System.out.println("Text is: " + text);
         */

        // here it captures the screenshot and compare with the image in
        // database but even though the images are same iit doesn't produce
        // expected results

        Finder finder = new Finder(screen.capture().getImage());

        finder.find(pattern.similar(1));

        if (finder.hasNext()) {
            Match m = finder.next();
            System.out.println("MATCH FOUND with " + (m.getScore()) * 100 + "%");
            finder.destroy();
            driver.close();
        } else {
            System.out.println("No Match Found");
            driver.close();
        }

    }

}

有没有替代方法?

P.S。 This是我要比较的图片

1 个答案:

答案 0 :(得分:0)

在我的案例中,我正在比较的图像具有不同的分辨率,而sikuli比较具有相同分辨率的图像,因此它对我不起作用,但是我使用OpenCv库实现了我想要的...。谢谢。