如何使用java捕获其他应用程序的选定屏幕?

10 java screenshot

我们正在尝试开发一个屏幕捕获实用程序.

我们如何使用Java捕获另一个应用程序的选定屏幕?我们如何在捕获的屏幕上添加标注?

Ada*_*ter 11

基于Prajakta对项目的描述,我相信操作屏幕截图的一些解释是有序的(我认为John在解释如何使用java.awt.Robot类捕获屏幕截图方面表现非常出色).请记住,正如Steve McLeod所说,Java可能无法自动定位您想要在屏幕上捕获的窗口的位置.这很重要,因为Robot类需要自动或手动了解此位置.

当您调用屏幕截图的BufferedImage的createGraphics()方法时,可以通过您收到的Graphics2D对象将标注,文本,图像等添加到屏幕截图中.我强烈建议您查看Graphics2D的API以更好地了解它的功能.我还建议找一些教程,也许从Sun的2D图形教程开始.这本名为" 肮脏的富客户 " 的书也可能有用.

当您最终要保存此修改过的屏幕截图时,可以使用ImageIO类的"write"方法之一.

这是一个非常简单的,从头到尾的例子.您需要填写必要的详细信息.

我希望这有点帮助!

Robot robot = new Robot();

// The hard part is knowing WHERE to capture the screen shot from
BufferedImage screenShot = robot.createScreenCapture(x, y, width, height);
Graphics2D graphics = screenShot.createGraphics();

// Add a label to the screen shot
Color textColor = Color.RED;
graphics.setColor(textColor);
graphics.drawString("Some text", textX, textY);

// Save your screen shot with its label
ImageIO.save(screenShot, "png", new File("myScreenShot.png"));
Run Code Online (Sandbox Code Playgroud)


Joh*_*ner 5

Robot r = new Robot();
Toolkit t = Toolkit.getDefaultToolkit();
Dimension d = t.getScreenSize();
Image i = r.createScreenCapture( 0, 0, d.width, d.height );
Run Code Online (Sandbox Code Playgroud)

应该为您提供整个屏幕的整体图像.如果你有多台显示器,不确定是否能让你获得全部东西,不过......


wut*_*aer 5

使用这段代码我可以在windows10中制作某些窗口的屏幕,不要忘记依赖关系。

学分转到:Windows:如何获取所有可见窗口的列表?

<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna</artifactId>
    <version>4.5.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

代码:

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.imageio.ImageIO;

import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.win32.StdCallLibrary;

public class Main {
    public static void main(String[] args) throws AWTException, IOException {

        int hWnd = User32.instance.FindWindowA(null, "Minesweeper X");
        WindowInfo w = getWindowInfo(hWnd);
        User32.instance.SetForegroundWindow(w.hwnd);
        BufferedImage createScreenCapture = new Robot().createScreenCapture(new Rectangle(w.rect.left, w.rect.top, w.rect.right - w.rect.left, w.rect.bottom - w.rect.top));
        ImageIO.write(createScreenCapture, "png", new File("screen.png"));

        // listAllWindows();
    }

    private static void listAllWindows() throws AWTException, IOException {
        final List<WindowInfo> inflList = new ArrayList<WindowInfo>();
        final List<Integer> order = new ArrayList<Integer>();
        int top = User32.instance.GetTopWindow(0);
        while (top != 0) {
            order.add(top);
            top = User32.instance.GetWindow(top, User32.GW_HWNDNEXT);
        }

        User32.instance.EnumWindows(new WndEnumProc() {
            public boolean callback(int hWnd, int lParam) {
                WindowInfo info = getWindowInfo(hWnd);
                inflList.add(info);
                return true;
            }

        }, 0);
        Collections.sort(inflList, new Comparator<WindowInfo>() {
            public int compare(WindowInfo o1, WindowInfo o2) {
                return order.indexOf(o1.hwnd) - order.indexOf(o2.hwnd);
            }
        });
        for (WindowInfo w : inflList) {
            System.out.println(w);
        }
    }

    public static  WindowInfo getWindowInfo(int hWnd) {
        RECT r = new RECT();
        User32.instance.GetWindowRect(hWnd, r);
        byte[] buffer = new byte[1024];
        User32.instance.GetWindowTextA(hWnd, buffer, buffer.length);
        String title = Native.toString(buffer);
        WindowInfo info = new WindowInfo(hWnd, r, title);
        return info;
    }

    public static interface WndEnumProc extends StdCallLibrary.StdCallCallback {
        boolean callback(int hWnd, int lParam);
    }

    public static interface User32 extends StdCallLibrary {
        public static final String SHELL_TRAY_WND = "Shell_TrayWnd";
        public static final int WM_COMMAND = 0x111;
        public static final int MIN_ALL = 0x1a3;
        public static final int MIN_ALL_UNDO = 0x1a0;

        final User32 instance = (User32) Native.loadLibrary("user32", User32.class);

        boolean EnumWindows(WndEnumProc wndenumproc, int lParam);

        boolean IsWindowVisible(int hWnd);

        int GetWindowRect(int hWnd, RECT r);

        void GetWindowTextA(int hWnd, byte[] buffer, int buflen);

        int GetTopWindow(int hWnd);

        int GetWindow(int hWnd, int flag);

        boolean ShowWindow(int hWnd);

        boolean BringWindowToTop(int hWnd);

        int GetActiveWindow();

        boolean SetForegroundWindow(int hWnd);

        int FindWindowA(String winClass, String title);

        long SendMessageA(int hWnd, int msg, int num1, int num2);

        final int GW_HWNDNEXT = 2;
    }

    public static class RECT extends Structure {
        public int left, top, right, bottom;

        @Override
        protected List<String> getFieldOrder() {
            List<String> order = new ArrayList<>();
            order.add("left");
            order.add("top");
            order.add("right");
            order.add("bottom");
            return order;
        }
    }

    public static class WindowInfo {
        int hwnd;
        RECT rect;
        String title;

        public WindowInfo(int hwnd, RECT rect, String title) {
            this.hwnd = hwnd;
            this.rect = rect;
            this.title = title;
        }

        public String toString() {
            return String.format("(%d,%d)-(%d,%d) : \"%s\"", rect.left, rect.top, rect.right, rect.bottom, title);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Cla*_*ler 2

您需要提供更具体的信息才能获得有意义的帮助。首先,这需要在哪些操作系统上运行?您是否需要捕获单个窗口或整个显示的内容(您在原始帖子中使用了含糊不清的术语“其他应用程序的选定屏幕”)。当您“向捕获的屏幕添加标注”时,您具体想看到什么?