有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正试图通过一个外置摄像头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>

然而,就我个人而言,我无法以每秒60帧的速度创造纪录。视频在存储时会随机出现口吃,一点也不流畅

我可以使用以下详细信息连接到相机

final List<Webcam> webcams = Webcam.getWebcams();

for (final Webcam cam : webcams) {
    if (cam.getName().contains("C922")) {
        System.out.println("### Logitec C922 cam found");
        webcam = cam;
        break;
    }
}

我将凸轮的大小设置为以下值:

final Dimension[] nonStandardResolutions = new Dimension[] { WebcamResolution.HD720.getSize(), };
webcam.setCustomViewSizes(nonStandardResolutions);
webcam.setViewSize(WebcamResolution.HD720.getSize());
webcam.open(true);

然后我拍摄了这些图像:

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)问题是否与我使用的代码有关


共 (1) 个答案

  1. # 1 楼答案

    正如一些评论所提到的,引入队列确实解决了这个问题。以下是执行所需步骤的一般逻辑。注意,我已经将代码设置为较低的分辨率,因为它允许我每秒捕获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);
        }
    
    }
    

    RecordingThread:

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

    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将侦听并从队列中提取数据。如果它没有接收到写操作应该结束的信号,那么循环就不会消失