Java 网络摄像头api:最大化720p捕获

Java 网络摄像头api:最大化720p捕获,java,webcam-capture,Java,Webcam Capture,我正试图通过一个外置摄像头Logitec C922捕捉视频记录。使用java,我可以通过WebCamAPI实现这一点 <dependency> <groupId>com.github.sarxos</groupId> <artifactId>webcam-capture</artifactId> <version>0.3.10</version> &

我正试图通过一个外置摄像头Logitec C922捕捉视频记录。使用java,我可以通过WebCamAPI实现这一点

    <dependency>
        <groupId>com.github.sarxos</groupId>
        <artifactId>webcam-capture</artifactId>
        <version>0.3.10</version>
    </dependency>
    <dependency>
        <groupId>xuggle</groupId>
        <artifactId>xuggle-xuggler</artifactId>
        <version>5.4</version>
    </dependency>
然后我拍摄了这些图像:

while (continueRecording) {
    // capture the webcam image
    final BufferedImage webcamImage = ConverterFactory.convertToType(webcam.getImage(),
                    BufferedImage.TYPE_3BYTE_BGR);
    final Date timeOfCapture = new Date();

    // convert the image and store
    final IConverter converter = ConverterFactory.createConverter(webcamImage, IPixelFormat.Type.YUV420P);
    final IVideoPicture frame = converter.toPicture(webcamImage,
                    (System.currentTimeMillis() - start) * 1000);

    frame.setKeyFrame(false);
    frame.setQuality(0);
    writer.encodeVideo(0, frame);

}
我的作者定义如下:

final Dimension size = WebcamResolution.HD720.getSize();
final IMediaWriter writer = ToolFactory.makeWriter(videoFile.getName());
writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_H264, size.width, size.height);

老实说,我不确定是什么在我的代码可能导致这一点。考虑到我降低了分辨率,我没有问题。(480p)问题是否与我使用的代码有关

正如一些评论所提到的,引入队列确实解决了这个问题。以下是执行所需步骤的一般逻辑。注意,我已经将代码设置为较低的分辨率,因为它允许我每秒捕获100FPS。根据需要进行调整

类链接图像/视频捕获,类编辑图像/视频捕获:

public class WebcamRecorder {

    final Dimension size = WebcamResolution.QVGA.getSize();
    final Stopper stopper = new Stopper();

    public void startRecording() throws Exception {

        final Webcam webcam = Webcam.getDefault();
        webcam.setViewSize(size);
        webcam.open(true);

        final BlockingQueue<CapturedFrame> queue = new LinkedBlockingQueue<CapturedFrame>();
        final Thread recordingThread = new Thread(new RecordingThread(queue, webcam, stopper));
        final Thread imageProcessingThread = new Thread(new ImageProcessingThread(queue, size));

        recordingThread.start();
        imageProcessingThread.start();
    }

    public void stopRecording() {
        stopper.setStop(true);
    }

}
ImageProcessingThread:

public void run() {
    writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_H264, size.width, size.height);
    try {
        int frameIdx = 0;
        final long start = System.currentTimeMillis();
        while (true) {
            final CapturedFrame capturedFrame = queue.take();
            if (capturedFrame.isEnd()) {
                break;
            }
            final BufferedImage webcamImage = capturedFrame.getImage();
            size.height);

            // convert the image and store
            final IConverter converter = ConverterFactory.createConverter(webcamImage, IPixelFormat.Type.YUV420P);
            final long end = System.currentTimeMillis();
            final IVideoPicture frame = converter.toPicture(webcamImage, (end - start) * 1000);

            frame.setKeyFrame((frameIdx++ == 0));
            frame.setQuality(0);
            writer.encodeVideo(0, frame);
        }
    } catch (final InterruptedException e) {
        System.out.println("### threading issues during image processing:: " + e.getMessage());
    } finally {
        if (writer != null) {
            writer.close();
        }
    }

它的工作方式非常简单。WebcamRecord类创建在视频捕获和图像处理之间共享的队列实例。RecordingThread将BuffereImage发送到队列(在我的例子中,它是一个pojo,称为CapturedFrame(其中有一个BuffereImage))。ImageProcessingThread将侦听并从队列中提取数据。如果它没有收到写入应该结束的信号,循环就不会消失。

您使用的是哪种网络摄像头API?我找到了
API
。我正在查看示例代码并将其与您的代码进行比较,我注意到您的
线程
/循环没有
线程。sleep(100)>。我想这就是他保持每秒帧数的方式。您熟悉JavaFX吗?我在
JavaFX
中编写了一个示例应用程序。如果CPU是瓶颈,请尝试将调整大小/编码操作委托给线程池。这是一个python示例,但您会注意到FPS是如何通过引入线程从35跳到140+的。你必须在你的案例中使用同样的方法。一个线程捕获图像中的原始数据并将其放入队列,另一个线程处理并写入
public class WebcamRecorder {

    final Dimension size = WebcamResolution.QVGA.getSize();
    final Stopper stopper = new Stopper();

    public void startRecording() throws Exception {

        final Webcam webcam = Webcam.getDefault();
        webcam.setViewSize(size);
        webcam.open(true);

        final BlockingQueue<CapturedFrame> queue = new LinkedBlockingQueue<CapturedFrame>();
        final Thread recordingThread = new Thread(new RecordingThread(queue, webcam, stopper));
        final Thread imageProcessingThread = new Thread(new ImageProcessingThread(queue, size));

        recordingThread.start();
        imageProcessingThread.start();
    }

    public void stopRecording() {
        stopper.setStop(true);
    }

}
public void run() {
    try {
        System.out.println("## capturing images began");
        while (true) {
            final BufferedImage webcamImage = ConverterFactory.convertToType(webcam.getImage(),
                    BufferedImage.TYPE_3BYTE_BGR);
            final Date timeOfCapture = new Date();
            queue.put(new CapturedFrame(webcamImage, timeOfCapture, false));
            if (stopper.isStop()) {
                System.out.println("### signal to stop capturing images received");
                queue.put(new CapturedFrame(null, null, true));
                break;
            }
        }
    } catch (InterruptedException e) {
        System.out.println("### threading issues during recording:: " + e.getMessage());
    } finally {
        System.out.println("## capturing images end");
        if (webcam.isOpen()) {
            webcam.close();
        }
    }
}
public void run() {
    writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_H264, size.width, size.height);
    try {
        int frameIdx = 0;
        final long start = System.currentTimeMillis();
        while (true) {
            final CapturedFrame capturedFrame = queue.take();
            if (capturedFrame.isEnd()) {
                break;
            }
            final BufferedImage webcamImage = capturedFrame.getImage();
            size.height);

            // convert the image and store
            final IConverter converter = ConverterFactory.createConverter(webcamImage, IPixelFormat.Type.YUV420P);
            final long end = System.currentTimeMillis();
            final IVideoPicture frame = converter.toPicture(webcamImage, (end - start) * 1000);

            frame.setKeyFrame((frameIdx++ == 0));
            frame.setQuality(0);
            writer.encodeVideo(0, frame);
        }
    } catch (final InterruptedException e) {
        System.out.println("### threading issues during image processing:: " + e.getMessage());
    } finally {
        if (writer != null) {
            writer.close();
        }
    }