Search code examples
javascreenshot

Java : Take screenshot of visible area in the browser


Is there a way to take the screenshot of the display in a browser in Java minus the browser window. The flow is :

Load the webpage in a browser. Then make an AJAX call to a Java service which captures the screenshot using the code from : How to take a screenshot in Java? .

The problem here is that I only want to capture the visible area in the browser excluding the browser window, the windows task bar and the other unwanted stuff.

Thanks


Solution

  • As @OscarRyz already mentioned AJAX call is irrelevant here. You have to create java applet that calls new Robot().checkScreenCaptureAllowed(). This will capture whole screen.

    The problem is how to understand where the visible area is. You can probably do it. You have to create at least one visible element in your applet. This element could be even very small (e.g. one pixel), so user will not see it. Then you can invoke getLocationOnScreen() that gives you the absolute coordinate on screen. If your java applet is in the top-left corner this is the point where your visible area starts.

    JavaScript allows you to know the height and width of current window: window.innerWidht and window.innerHeight. So, you now you can take your screen capture and cut the area that you need.

    But please take into consideration that new Robot().checkScreenCaptureAllowed() checks READ_DISPLAY_PIXELS_PERMISSION and I do not believe you have such permissions for unsigned applet, so you will have to sign your applet.

    To avoid this problem take a look on this: http://html2canvas.hertzen.com/ - script that "captures" screen and uses JavaScript only.