2011-07-06 25 views
2

我正在使用Java小程序截取Web瀏覽器的截圖,使用Java的Robot類。使用Java的Java小程序截圖使用Java機器人類不工作

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。我的小程序是一個簽名的小程序。

可能是什麼原因?

我的機器配置如下:

OS : MAC OS X 
Version : 10.6.4 
+0

crossposted http://www.daniweb.com/software-development/java/9 – mKorbel

回答

1

我發送的錯誤消息invalid pixel format谷歌和收到的效果(接近10.000)的一個長長的清單 - 它看起來好像問題不是Java的問題,但在Mac上的配置問題。

嘗試更改顯示器分辨率並重新運行applet。很有可能,錯誤與某些屏幕分辨率有關(外部顯示器?)。網絡上的一些建議是完全更新你的OSX。

+0

感謝Andreas_D..the理念更新的Mac OSX雪豹爲10.3至10.6爲我工作。 – user777317

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? 
}