使用网络摄像头录制视频并将其保存在Java中

时间:2019-06-09 16:39:24

标签: java opencv webcam javacv

我的目标是使用摄像头捕获视频并保存。我已经可以使用网络摄像头拍照。

因此,我使用sarxos提供的网络摄像头API。我现在想将图像粘贴在一起以获取视频。到目前为止,我已经为此目的使用了OpenCV。不幸的是,该视频似乎加速了,并且是蓝色的。

      Runnable recordVideo = () -> {
            final int FPS = 30;
            final int WIDTH = 640;
            final int HEIGHT = 480;

            File output = new File('test.avi');
            webcam.setViewSize(new Dimension(WIDTH, HEIGHT));
            webcam.open();
            Java2DFrameConverter java2DFrameConverter = new Java2DFrameConverter();

            OpenCVFrameRecorder recorder = new OpenCVFrameRecorder(output, WIDTH, HEIGHT);
            recorder.setFrameRate(FPS);
            recorder.setVideoCodec(opencv_videoio.VideoWriter.fourcc((byte) 'M', (byte) 'J', (byte) 'P', (byte) 'G'));


            try {
                recorder.start();

                while (true) {
                    if (Thread.currentThread().isInterrupted()) {
                        break;
                    }
                    BufferedImage image = webcam.getImage();

                    recorder.record(java2DFrameConverter.convert(image));
                    try {
                        Thread.sleep(1000 / (long) FPS);

                    } catch (InterruptedException e) {
                        break;
                    }
                }
                recorder.stop();
                recorder.release();
                log.info("[RecorderService] Video recorded in file: " + output.getAbsolutePath());
                webcam.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        };

        recordVideoThread = new Thread(recordVideo);
        recordVideoThread.start();

pom.xml依赖项

            <groupId>org.bytedeco</groupId>
            <artifactId>javacv-platform</artifactId>
            <version>1.4.4</version>
        </dependency>
        <dependency>
            <groupId>com.github.sarxos</groupId>
            <artifactId>webcam-capture</artifactId>
            <version>0.3.12</version>
        </dependency>

如何才能将捕获速度降低为实时,并将视频更改为原始颜色?

0 个答案:

没有答案