在 ArrayList 中存储数千个 BufferedImages 而不会用完所有内存 - Java

Store thousands of BufferedImages in ArrayList without using up all memory - Java

我正在尝试制作一个屏幕录制应用程序。我的代码使用 java.awt.Robot.createScreenCapture 截取屏幕截图,然后将输出存储在数组列表中。数组列表需要存储7500张图片。我需要能够非常快速地访问任何 BufferedImages。我试过将 BufferedImages 转换为 byte[] 然后存储它们,但是将它们转换回缓冲图像需要很长时间(大约 1 秒)。有没有一种方法可以在不添加命令行参数的情况下执行此操作?

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

代码:

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();

        }

    }

});

真正的解决方案:使用硬件加速视频编码器压缩帧(或软件编码器,如果你负担得起CPU)

旧答案:

我的问题解决了!我所做的是将 5 分钟的录制时间更改为 15 秒,然后将 BufferedImages 的类型更改为 TYPE_BYTE_INDEXED,然后将图像尺寸减半,然后降低帧速率。将来,我可能会用 Gilbert Le Blanc 的系统制作同样的程序(请看上面的评论)。