0

I have a question about screenshot in java: exist a library for take screenshot and save them for java? Now I'm using buffered images and AWT robot (you can find the repo here : GitHub), but them have some memory leak and use a lot of memory (foreach screen it take the memory use increment about ~ 40 MB). Can you give me a library or some trick for take screenshot using less memory as possible?

Here the code that i use for screenshot, but it has the problem mentioned.(It take a partial screen)

ImageIO.write(new Robot().createScreenCapture(Main.so.getScreenBounds()).getSubimage(
                                partialScreen.selectionBounds.x,
                                partialScreen.selectionBounds.y,
                                partialScreen.selectionBounds.width,
                                partialScreen.selectionBounds.height), "png", tempFile);

So much thanks!

luator
  • 4,769
  • 3
  • 30
  • 51
SergiX44
  • 371
  • 2
  • 5
  • 15
  • Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow – CubeJockey Jul 01 '15 at 13:52
  • @Trobbins i know that, i ask infact an help for reduce memory consumpion. – SergiX44 Jul 01 '15 at 14:11
  • _"exist a library for ... ?"_ and _"Can you give me a library..."_ – CubeJockey Jul 01 '15 at 14:12
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). But while I'm here, don't create a new `Robot` for every screenshot! Create it once and store it as a class member. BTW - from things you wrote it seems the code needs to take more than one. screenshot and possibly many. Is this for a 'movie' of the screen or for 'screen sharing', or something else? Please flesh out the detail of what all this is trying to achieve (the 'application feature'). – Andrew Thompson Jul 02 '15 at 21:30
  • @AndrewThompson The application is a screen sharing tool, like puush and screencloud, a simple MCVE is a thing like this [link](http://stackoverflow.com/questions/58305/is-there-a-way-to-take-a-screenshot-using-java-and-save-it-to-some-sort-of-image), just add an Thread.sleep and see the memory usage in the task manager (about 30 MB, what!?). The problem i think is the buffered image, because analyzing the application with a profiler you can see that the memory is used by a lot of int[] array. – SergiX44 Jul 03 '15 at 20:38
  • *"and see the memory usage in the task manager"* A developer can pay too much attention to the task manager. Does the application throw `OutOfMemoryError`? – Andrew Thompson Jul 03 '15 at 21:18
  • @AndrewThompson The application doesn't throw outofmemory, but it's not possible that after some screenshots(full screen and partial) and file uploads it consume about ~ 300 MB of memory. – SergiX44 Jul 04 '15 at 13:57
  • 1
    *"The application doesn't throw outofmemory,"* Then don't stress about it. The Windows task manager is known to provide information that scares developers (most users don't know it exists) but is not really accurate. – Andrew Thompson Jul 04 '15 at 14:15
  • Why do you first capture the entire screen, then get a subimage of that? If you want to use less memory, just capture the `partialScreen.selectionBounds` right away. – Harald K Jul 07 '15 at 10:41
  • @haraldK your solution work only on pc with only one monitor, i've already tried it – SergiX44 Jul 12 '15 at 09:26
  • @SergiX44 Maybe because the selection bounds is in a different coordinate space? Then calculate the correct bounds before capturing the screen shot. The main point remains the same: Don't capture more than you need. – Harald K Jul 12 '15 at 09:39
  • @haraldK yes, but how i can know the correct order of the monitors? this is the problem. – SergiX44 Jul 16 '15 at 11:32
  • How did you know that before? Or, if you didn't know it before, why do you think you need it now? You just need to calculate a sub-rectangle from a larger rectangle, the same way getSubimage does. – Harald K Jul 16 '15 at 11:38

0 Answers0