Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 1.16 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Managing memory usage when getting many screenshots in Java
  2. public BufferedImage getScreenShot() {
  3.  
  4.     BufferedImage screenImage = null;
  5.     try {
  6.  
  7.         GraphicsEnvironment ge= GraphicsEnvironment.getLocalGraphicsEnvironment();
  8.         GraphicsDevice[] screens=ge.getScreenDevices();
  9.         Rectangle allScreenBounds=new Rectangle();
  10.         for(GraphicsDevice screen: screens)
  11.         {
  12.             Rectangle screenbounds = screen.getDefaultConfiguration().getBounds();
  13.             allScreenBounds.width+=screenbounds.width;
  14.             allScreenBounds.height=Math.max(allScreenBounds.height, screenbounds.height);
  15.         }
  16.  
  17.  
  18.         Robot robot = new Robot();
  19.         screenImage = robot.createScreenCapture(allScreenBounds);
  20.     } catch (Exception ex) {
  21.         Logger.getLogger(Screen.class.getName()).log(Level.SEVERE, null, ex);
  22.     }
  23.     return screenImage;
  24. }
  25.        
  26. while(true){
  27.   FileImageOutputStream out = new FileImageOutputStream(f);
  28.   writer.setOutput(out);
  29.   IIOImage image = new IIOImage(ImageUtils.resize(getScreenShot(), width, height),null, null);
  30.   writer.write(null, image, iwp);
  31.   writer.dispose();
  32. }
  33.        
  34. writer.write(null, image, iwp);
  35.        
  36. image=null;