简体   繁体   中英

Java's createScreenCapture not working on OS X, produces completely wrong colours

If I run this code:

BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "png", new File("takenwithrobot.png"));

It yields the following PNG: http://davzy.com/screenshots/takenwithrobot.png

If I take a screenshot using the built in OS X screen capture utility I get the following: http://davzy.com/screenshots/takenwithOSXscreenshotutility.png

As you can see the colours in both are completely different. After several experiments I determined that the colours in the above screenshot taken with the built in OS X utility have the correct colours, and the screenshot taken with robot have the complete wrong colours.

I am writing a script which compares pixel by pixel of screenshots, and so if robot is producing screenshots with wrong colours, it will not work. Interestingly enough if I run the above on a Windows machine it works just fine and gets the correct colours. I was thinking maybe the colour depth of robot was the cause of all the wrong colours, but I don't know if that's the problem and I don't know how to change it.

If the robot takes all screen shots, there should be no problem (two equally false pixels of two screenshots are evidently the same).

First I saw a difference between application window having focus, and one having not. Maybe that is one explanation. For the different gray on the status line of NetBeans.

Furthermore the standard screen capture might correct gamma and do anti-aliasing. Maybe that is the case here.

(Unfortunately my non-Mac monitor does not show other differences.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM