开发者

Taking screenshot with Java applet using Java Robot class not working

I am using a Java applet to take a screenshot of the web browser, using Java's Robot class.

Robot o开发者_开发知识库bjRobot = new Robot ();
BufferedImage objBufferedImage = objRobot.createScreenCapture(objRectArea); 

The thing works good in Windows system, taking screenshot. But in case of Mac OS X I get a blank image.

When I check the event viewer, I see the following error:

invalid context
invalid pixel format
CoreAnimation: rendering error 506

The problem is occurring for all the browsers Safari, Firefox and Chrome. My applet is a signed applet.

What might be the reason?

My machine configuration is as follows:

OS : MAC OS X
Version : 10.6.4


I've sent the error message invalid pixel format to google and received a long list of results (close to 10.000) - it looks as if the problem is not a Java problem but a configuration issue on your Mac.

Try to change display resolutions and re-run your applet. Good chance, that the error is linked to some screen resolutions (external display?). Some suggestions on the web were to fully update you OSX.


dir Robot objRobot = null;
                 try
                 {
                    objRobot = new Robot();
                 } catch(Exception ex)
                 {

         }

Dimension screenDim =  Toolkit.getDefaultToolkit().getScreenSize(); 

BufferedImage objBufferedImage =  objRobot.createScreenCapture(new Rectangle(0, 0, (int)screenDim.getWidth(), (int)screenDim.getHeight()));

int areaToExportWidth = 1024;
int areaToExportHeight = 768;

//Create the image 
BufferedImage exportImage =objRobot.createScreenCapture(new Rectangle(0, 0, (int)screenDim.getWidth(), (int)screenDim.getHeight()));

//Get graphics - Get the layer we can actually draw on
Graphics2D imageGraphics = (Graphics2D) exportImage.getGraphics();



//Cleanup after ourselves
imageGraphics.dispose();

//Setup to write the BufferedImage to a file
String pathToFile = "dir";
File outputDirectory = new File(pathToFile);
File outputFile = new File(pathToFile+"\\"+counter+"MyImage.png");
//Here we make sure the directory exists.
/*
 * Returns TRUE if:
 *  The directory is MISSING
 *  and/or the directory IS NOT a directory
 */
if(!outputDirectory.exists() || !outputDirectory.isDirectory()){
    outputDirectory.mkdirs(); //Make the directory
} // Else do nothing

//Write the file
try { //Attempt the write
    ImageIO.write(exportImage, "png", outputFile);
} catch (IOException e) { //For some reason it failed so...
    e.printStackTrace(); //... why did it fail?
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜