gpt4 book ai didi

java - 网络摄像头 api : Maximizing the 720p capture

转载 作者:搜寻专家 更新时间:2023-11-01 03:46:42 25 4
gpt4 key购买 nike

我正在尝试通过外部摄像头 Logitec C922 捕捉视频记录。使用 java,我可以通过网络摄像头 api 实现这一点。

    <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>

然而,对于我来说,我无法让它以 60FPS 的速度录制。视频在存储时随机卡顿,一点也不流畅。

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

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

}

我的writer定义如下:

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

}

录音线程:

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

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

关于java - 网络摄像头 api : Maximizing the 720p capture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48727205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com