检查视频是否已结束

时间:2015-10-28 09:05:53

标签: java sikuli-script

我正在使用SikuliX检查网站上的视频何时结束。

我通过将我的区域(我的活动网络浏览器窗口)与我在视频播放时拍摄的区域的屏幕截图进行比较来实现此目的。

如果它不匹配,则表示视频仍在播放,我将进行新的屏幕捕获,将再次通过while循环进行比较。 如果匹配,则表示视频已停止并将退出while循环。

它首次通过循环时有效。 while循环返回null,表示视频正在播放。但是,在它第二次循环时,它将退出while循环并告诉我我的视频已经停止,但它显然没有。

我的逻辑是否有缺陷?

// Get dimensions of the bounding rectangle of the specified window
WinDef.HWND hwnd = User32.INSTANCE.GetForegroundWindow();
WinDef.RECT dimensions = new WinDef.RECT();

// Get screen coordinates of upper-left and lower-right corners of the window in dimensions
User32.INSTANCE.GetWindowRect(hwnd, dimensions);

Rectangle window = new Rectangle(dimensions.toRectangle());

int x = window.x;
int y = window.y;
int width = window.width;
int height = window.height;

// Initialize screen region for Sikuli to match
Region region = new Region(x, y, width, height);

Robot robot;
Image image;
Pattern p;

try {
    robot = new Robot(); // Gets and saves a reference to a new Robot object 
} catch (AWTException e) {
    throw new RuntimeException(
        "Failed to initialize robot..."); 
}

robot.delay(3000); // Delay robot for 3 seconds 

// Take a screen capture of the region
BufferedImage capture = robot.createScreenCapture(dimensions.toRectangle());
image = new Image(capture);
p = new Pattern(image);

region.wait(1.0); // Wait 1 second

// Check if region content is still the same
while (region.exists(p.similar((float) 0.99), 0) == null) {
    System.out.println("Video is playing");

    // Take a new screen capture of the region
    BufferedImage captureLoop = robot.createScreenCapture(dimensions.toRectangle());
    image = new Image(captureLoop);
    p = new Pattern(image);

    region.wait(1.0); // Wait 1 second
}   

System.out.println("Video has stopped");

1 个答案:

答案 0 :(得分:0)

不使用while (region.exists(p.similar((float) 0.99), 0) == null),而是使用while (region.compare(image).getScore() == 1.0)将区域与屏幕截图进行比较,得到了我想要的结果。