使用 Java Robot 类使用 Java 小程序截取屏幕截图不起作用

发布于 2024-11-18 13:40:08 字数 528 浏览 6 评论 0原文

我正在使用 Java 小程序来使用 Java 的 Robot 类来截取 Web 浏览器的屏幕截图。

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

这个东西在Windows系统下运行良好,可以截图。但如果是 Mac OS XI,则会得到空白图像。

当我检查事件查看器时,我看到以下错误:

invalid context
invalid pixel format
CoreAnimation: rendering error 506

所有浏览器 Safari、Firefox 和 Chrome 都会出现此问题。我的小程序是一个签名的小程序。

可能是什么原因?

我的机器配置如下:

OS : MAC OS X
Version : 10.6.4

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

Robot objRobot = 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘你却要生生世世 2024-11-25 13:40:08

我已向 google 发送了错误消息无效的像素格式,并收到了一长串结果(接近 10.000) - 看来问题不是 Java 问题,而是 Mac 上的配置问题。

尝试更改显示分辨率并重新运行您的小程序。很有可能,该错误与某些屏幕分辨率(外部显示器?)有关。网上的一些建议是完全更新您的 OSX。

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.

我家小可爱 2024-11-25 13:40:08
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?
}
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?
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文