1

Today, I ran into weird issue: java.awt.Robot captures black areas, instead of image content. I'm using Ubuntu 12.04 and OpenJDK6/7.

java.awt.Robot on Unix is supported by the sun.awt.X11.XRobotPeer, which, following the corresponding OpenJDK sources, uses image grabbing functions similar to those, used by xwd utililty (lines 92-162).

So, I ran the following command:

xwd -root -out test.screen.root.xwd

and then opened this file with gimp. Here's what I get:

enter image description here

Then I tried:

xwd -root | xwdtopnm | pnmtopng > Screenshot.root.png

And I got the following result: Console output:

xwdtopnm: writing PPM file
libpng warning: Invalid sBIT depth specified

And the image itself: enter image description here

What can cause this? How can I fix it?

Lex Li
  • 60,503
  • 9
  • 116
  • 147
Ostap Andrusiv
  • 4,827
  • 1
  • 35
  • 38
  • 1
    I would try your code on Oracle's official JDK, and if that doesn't work then try it on Windows on Oracle's official JDK. If either of those work with your code, then file a bug report. Either against openJDK or an official one. If none of those work with your code, then fix your code. – BillRobertson42 Nov 21 '12 at 13:00
  • I tried that on OpenJDK6/7 and OracleJDK6/7. This is Ubuntu-linux only issue. Robot works as expected on Windows. – Ostap Andrusiv Nov 21 '12 at 13:46
  • The unity tag is for Microsoft Unity. Please don't misuse it. – Lex Li Dec 31 '12 at 12:11

2 Answers2

1

Seems like the only way to fix this is to use your own native implementation of screen shotting.

Here's the detailed description of the problem at the launchpad from unity devs: launchpad conversation.

The problem is in the way unity-2d draws itself and in the use of XShaping.

Ostap Andrusiv
  • 4,827
  • 1
  • 35
  • 38
0

I had the same error message. Apparently, it is linked to xwdtopnm not handling well the color depth of your screen - causing the resulting png file to be corrupted.

An alternative solution is to use the import command from the imagemagick package to take the screenshot.

So you can just replace:

xwd -root | xwdtopnm | pnmtopng > Screenshot.root.png

with:

import -window root Screenshot.root.png

NB: if it is not installed, you can get imagemagick on Ubuntu with the following command:

sudo apt install imagemagick
Jealie
  • 6,157
  • 2
  • 33
  • 36