有没有办法用codenameone截屏(特别是对于Android)?

时间:2019-06-11 10:20:54

标签: java codenameone screenshot

我试图每秒拍摄一次屏幕截图,以了解用户是否在另一个应用程序的菜单中。我在Display中找到了一个名为capturePhoto(...)的函数,但这是一个无效函数。我也用Robot尝试过,但是不支持。

我用Google搜索了它,但只发现了文本,没有代码可使用。

我发现的功能:Display.getInstance().capturePhoto(null);

我不知道该怎么办而不是null

1 个答案:

答案 0 :(得分:2)

要使用Codename One截屏,可以使用以下静态方法:

    /**
     * Returns a screenshot of the currently displayed Form, or null if no form
     * is shown
     *
     * @return
     */
    public static Image getScreenshot() {
        Form form = Display.getInstance().getCurrent();
        if (form != null) {
            Image screenshot = Image.createImage(form.getWidth(), form.getHeight());
            form.paintComponent(screenshot.getGraphics(), true);
            return screenshot;
        } else {
            return null;
        }
    }

它可以在Codename One支持的所有平台上使用,不仅适用于Android。