0

I am trying to make a screen recording app. I have code that takes a screenshot using java.awt.Robot.createScreenCapture and then stores the output in an arraylist. The arraylist needs to store 7500 images. I need to be able to access any of the BufferedImages very quickly. I have tried converting the BufferedImages into byte[] and then storing them, but converting them back to bufferedimages takes too long (about 1 second). Is there a way I could do this without having to add command line arguments?

Error: Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space

Code:

static ArrayList < BufferedImage > bilist = new ArrayList < BufferedImage > ();
public static Timer recordingTimer = new Timer (40, new ActionListener () {

    public void actionPerformed ( ActionEvent e ) {

        try {

            BufferedImage bimage = robot.createScreenCapture(wholescreen);
            bilist.add(bimage);
            if ( bilist.size() > 7500 ) bilist.remove(7500);

        } catch ( Exception ex ) {

            ex.printStackTrace();

        }

    }

});
XMB5
  • 1,384
  • 1
  • 13
  • 17
  • Have you done the maths? What is the size of one screen? How much memory is actually available for the array? – Mike Jul 23 '16 at 17:21
  • 2
    If the images would have the resolution of 1920x1080 and use 4 bytes for each pixel you'd have to provide 7500x1920x1080*4 / (1024x1024x1024) = 59.75 GB in order to keep that in your RAM. So why do you have to keep 7500 images in memory? – Stefan Falk Jul 23 '16 at 17:22
  • I need 7500 images so that if something happens on the computer, you can press a button that saves the last 5 minutes of what happened (7500 frames) The screen is 1920 * 1080, and the program has 1GB of memory – XMB5 Jul 23 '16 at 17:23
  • Yeah, you'll have to find a more practicable solution if you want to allow larger resolutions because of mentioned reasons. – Stefan Falk Jul 23 '16 at 17:24
  • 1
    Write the images to disk as you're generating them, and when someone presses the button, you launch another thread that copies the 7500 images to another place on the disk. This assumes that you have a terabyte of disk space available. Also, you need to upgrade to a computer with at lest 4GB and probably 8GB of memory. – Gilbert Le Blanc Jul 23 '16 at 17:48

1 Answers1

0

Real solution: compress frames with a hardware-accelerated video encoder (or software encoder if you can afford CPU)

Old answer:

I have solved my problem! What I did was I changed 5 minutes of recording to 15 seconds, then I changed the type of the BufferedImages to TYPE_BYTE_INDEXED, then I halved the images dimensions, and then I lowered the frame rate. In the future, I might make this same program working with Gilbert Le Blanc's system (look at comment above).

XMB5
  • 1,384
  • 1
  • 13
  • 17