【问题标题】:Recording Screen Java Disk Speed录制屏幕 Java 磁盘速度
【发布时间】:2012-07-06 09:07:06
【问题描述】:

我正在开发一个 Java 应用程序来记录屏幕。我正在使用机器人来截取几张屏幕截图,将它们保存到一个临时文件夹中,然后我使用 JpegImagesToMovie.java 将它们构建到 QuickTime 电影文件中。

我遇到的问题是,尽管我的脚本以 20fps 运行,但我只能达到 5fps 左右。我已将其跟踪到磁盘速度,因为将图像保存到磁盘需要很长时间,这会阻碍脚本的其余部分。

接下来,我修改了脚本以将图像存储在 BufferedImages 数组中,然后在录制停止后将它们写入磁盘以固定帧速率,但是在重新编码时,Java 将很快耗尽内存(几秒钟后记录)。

有没有人对此有任何想法或经验。我能想到的一种解决方案是,是否有办法增加 JPEG 图像的压缩率,但我不确定如何做到这一点。

任何帮助将不胜感激!

【问题讨论】:

  • 要获得 20fps,您可能应该使用真正的视频编解码器,而不是在驱动器上爆破 JPEG
  • Monte Media Library 看到的屏幕录像机可以以 20 FPS 的速度进行全屏(此处为 1920x1080 像素)视频捕捉。它使用 JMF & 直接编码为 MOV 或 AVI。
  • 这看起来像我想要的!当我下班回家报告时会玩它。

标签: java performance disk frame-rate awtrobot


【解决方案1】:

您可能要考虑的一个选项是在多个线程上进行处理。一个线程可以专门用于截屏,许多其他线程可以写入磁盘。由于写入磁盘不是 CPU 密集型操作,因此您可以让它们中的许多同时运行,每个都写入不同的文件。以下程序在我的机器上运行良好,堆大小为 512M:

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class ImageWritingMain
{
  public static void main(String[] args) throws Exception
  {
    // a queue
    final BlockingQueue<BufferedImage> queue = 
        new LinkedBlockingQueue<BufferedImage>();

    // schedule a thread to take 20 images per second and put them in 
    // the queue
    int fps = 20;
    final ScreenShotRecorder recorder = 
        new ScreenShotRecorder(new Robot(), queue);
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(recorder, 0, (1000L/fps));

    // make a directory to hold the screenshot images
    String id = new Date().toString().replace(' ', '-').replace(':', '-');
    File imageDir = new File("images-" + id);
    imageDir.mkdirs();

    // start 10 threads, and each thread reads from the queue and 
    // writes the image to a file
    int nWriterThreads = 10;
    ExecutorService threadPool = Executors.newFixedThreadPool(nWriterThreads);
    for (int i = 0; i < nWriterThreads; i++)
    {
      ImageWriter task = new ImageWriter(queue, imageDir);
      threadPool.submit(task);
    }
    System.out.println("Started all threads ..");

    // wait as long as you want the program to run (1 minute, for example) ...
    Thread.sleep(60 * 1000L);
    // .. and shutdown the threads
    System.out.println("Shutting down all threads");
    threadPool.shutdownNow();
    timer.cancel();

    if (! queue.isEmpty())
    {
      System.out.println("Writing " + queue.size() + " remaining images");
      // write the remaining images to disk in the main thread
      ImageWriter writer = new ImageWriter(queue, imageDir);
      BufferedImage img = null;
      while ((img = queue.poll()) != null)
      {
        writer.writeImageToFile(img);
      }
    }
  }
}

class ScreenShotRecorder extends TimerTask
{
  private static final Rectangle screenRect = 
      new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
  private static final AtomicInteger counter = new AtomicInteger();
  private final Robot robot;
  private final BlockingQueue<BufferedImage> imageQueue;

  ScreenShotRecorder(Robot robot, BlockingQueue<BufferedImage> imageQueue)
  {
    this.robot = robot;
    this.imageQueue = imageQueue;
  }

  @Override
  public void run()
  {
    try
    {
      BufferedImage image = robot.createScreenCapture(screenRect);
      imageQueue.put(image);
      System.out.println(Thread.currentThread() + 
          ": Took screenshot #" + counter.incrementAndGet());
    }
    catch (InterruptedException e)
    {
      System.out.println("Finishing execution of " + Thread.currentThread());
      return;
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
}

class ImageWriter implements Runnable
{
  private static final AtomicInteger counter = new AtomicInteger();
  private final BlockingQueue<BufferedImage> imageQueue;
  private final File dir;

  ImageWriter(BlockingQueue<BufferedImage> imageQueue, File dir)
  {
    this.imageQueue = imageQueue;
    this.dir = dir;
  }

  @Override
  public void run()
  {
    while (true)
    {
      try
      {
        BufferedImage image = imageQueue.take();
        writeImageToFile(image);
      }
      catch (InterruptedException e)
      {
        System.out.println("Finishing execution of " + Thread.currentThread());
        return;
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }
  }

  public void writeImageToFile(BufferedImage image) throws IOException
  {
    File file = new File(dir, "screenshot-" + counter.incrementAndGet());
    ImageIO.write(image, "JPG", file);
    System.out.println(Thread.currentThread() + 
        ": Wrote " + file.getCanonicalPath());
  }
}

【讨论】:

    【解决方案2】:

    mmymoose 说的。也许您可以尝试降低 JPEGS 的分辨率以提供更多内存,但您可能不得不使用视频编解码器。您也可以尝试创建一个处理程序以仅在您移动鼠标时进行录制,或者如果您真的打算以 JPEG 格式录制,请键入。

    【讨论】:

    • 编解码器的想法听起来很有希望,但我从来没有用 Java 或任何其他语言处理过它们,如果你能给我任何链接/指针,那将是一个很大的帮助!如果我沿着编解码器路线走,我还会使用机器人还是需要使用不同的东西?最后,我计划将此应用程序分发给新手用户(我也在考虑使用 webstart),因此我确实需要将所有内容都包含在应用程序中,而用户无需在他们的计算机上安装其他软件。感谢您一直以来的帮助!
    • 对不起,我没有真正使用过编解码器,所以我对它们了解不多。我只知道拍摄屏幕截图并将它们转换为视频非常耗费内存。我建议阅读它们,然后提出一个关于它们的新问题。
    猜你喜欢
    • 2017-09-18
    • 1970-01-01
    • 2014-01-05
    • 2013-12-16
    • 2021-06-06
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    相关资源
    最近更新 更多