简体   繁体   English

使用 Java 机器人 class 使用 Java 小程序截屏

[英]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.我正在使用 Java 小程序截取 web 浏览器的屏幕截图,使用 Java 的机器人 class。

Robot objRobot = new Robot ();
BufferedImage objBufferedImage = objRobot.createScreenCapture(objRectArea); 

The thing works good in Windows system, taking screenshot.这个东西在 Windows 系统中运行良好,截图。 But in case of Mac OS XI get a blank image.但是在 Mac OS XI 的情况下会得到一个空白图像。

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.所有浏览器 Safari、Firefox 和 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.我已将错误消息invalid pixel format发送给谷歌并收到一长串结果(接近 10.000) - 看起来问题不是 Java 问题,而是 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. web 的一些建议是为了全面更新您的 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?
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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