开发者

How do I find the GraphicsDevice where an external process' window is opened

开发者 https://www.devze.com 2023-04-05 13:26 出处:网络
I need to find the GraphicsDevice (e.g. the physical monitor) where an external process has opened its window.To grab screen开发者_运维技巧 shots to catch changes WRT older versions. We\'re talking ap

I need to find the GraphicsDevice (e.g. the physical monitor) where an external process has opened its window. To grab screen开发者_运维技巧 shots to catch changes WRT older versions. We're talking apps running on localhost, JS is heavily used.

In my case I have a Java process (based on Selenium) that opens a browser window and needs to grab a screen shot of it (using java.awt.Robot), unfortunately the display where the window is opened depends on where the browser remembers that it was the last time it was closed.

So, is there any way I can have my Java process find out which GraphicsDevice the browser window was opened on?


One (very crude) way to do it is to take a screen-shot before the call to Desktop.browse(), and another 'several moments later'. ..Then compare them for the region that is different.

This might be fouled up if the user has any animated elements on the desk top (e.g. a clock).


An alternate route might (untested) be as follows. Since you effectively control the environment and content, it is possible to:

  1. Load the 'screenshot' page in a frame of a framed document.
  2. Load an applet in another frame.
  3. Have JS determine the content area of the target frame, and report that to the applet. Applet determines a screen shot area from a combination of its own location and the content area.
  4. The applet reports the area to (sand-boxed) the localhost or (trusted) any socket it wants.
  5. The screenshot app. use the information from the applet to control the Robot. If the applet is trusted, it might grab the screenshot and write it to disk (and skip the 'report' step entirely).


GraphicsDevice[] devices can returns that, but (I never tested) if is there profesional GPU with Multi GPU Cores or chaned GPU in SLI Mode, then this Array is multiplayed, you can testing (this code required simple modifications for Runtime) that with code pieces from SwingX

public static Point getPointForCentering(JInternalFrame window) {
        try {
            //assert window != null;
            Point mousePoint = MouseInfo.getPointerInfo().getLocation();
            GraphicsDevice[] devices = GraphicsEnvironment
                    .getLocalGraphicsEnvironment().getScreenDevices();
            for (GraphicsDevice device : devices) {
                Rectangle bounds = device.getDefaultConfiguration().
                    getBounds();
                    //check to see if the mouse cursor is within these bounds
                if (mousePoint.x >= bounds.x && mousePoint.y >= bounds.y &&
                    mousePoint.x <= (bounds.x + bounds.width) && mousePoint.y 
                    <= (bounds.y + bounds.height)) {
                    int screenWidth = bounds.width;//this is it
                    int screenHeight = bounds.height;
                    int width = window.getWidth();
                    int height = window.getHeight();
                    return new Point(((screenWidth - width) / 2) + bounds.x,
                       ((screenHeight - height) / 2) + bounds.y);
                }
            }
        } catch (Exception e) {
            LOG.log(Level.FINE, e.getLocalizedMessage() + 
               " - this can occur do to a Security exception in sandboxed apps");
        }
        return new Point(0, 0);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号