我正在使用Java applet使用Java的Robot类来截取Web浏览器的屏幕截图。
Robot objRobot = new Robot ();
BufferedImage objBufferedImage = objRobot.createScreenCapture(objRectArea);
这个东西在Windows系统中很好用,截图。但是对于Mac OS X,我得到一张空白图像。
当我检查事件查看器时,我看到以下错误:
invalid context
invalid pixel format
CoreAnimation: rendering error 506
所有浏览器Safari,Firefox和Chrome都出现了问题。我的applet是一个签名的小程序。
可能是什么原因?
我的机器配置如下:
OS : MAC OS X
Version : 10.6.4
答案 0 :(得分:1)
我已将错误消息invalid pixel format
发送给谷歌,并收到一长串结果列表(接近10.000) - 看起来问题不是Java问题,而是Mac上的配置问题。< / p>
尝试更改显示分辨率并重新运行小程序。很有可能,错误与某些屏幕分辨率(外部显示器?)有关。网上的一些建议是完全更新你的OSX。
答案 1 :(得分:0)
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?
}