需要进行简单的音视频编程,如果不是特别数据C/C++,那么JavaCV应该是比较好的选择,下面记录一下使用JavaCV采集摄像头的两头方式。
1. OpenCV
使用OpenCVFrameGrabber采集指定摄像头(索引)的视频数据:
public class Sample01_Camera {
public static void main(String[] args) throws Exception {
OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
grabber.setImageWidth(1280);
grabber.setImageHeight(720);
grabber.start();
CanvasFrame canvas = new CanvasFrame("米虫2022");
canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.setAlwaysOnTop(true);
Java2DFrameConverter converter = new Java2DFrameConverter();
int fps = 1000 / 40;
while (canvas.isDisplayable()) {
canvas.showImage(converter.convert(grabber.grab()));
TimeUnit.MILLISECONDS.sleep(fps);
}
grabber.close();
}
}
使用OpenCV采集的视频数据会泛白,可以使用Java2DFrameConverter转化一下。
采集效果:
2. FFmpeg
使用FFmpeg采集摄像头需要知道摄像头的名称,可以使用下面的ffmpeg命令查看设备上的摄像头名称:
ffmpeg.exe -list_devices true -f dshow -i dummy
跟OpenCV类似,只需要将OpenCVFrameGrabber换成FFmpegFrameGrabber,并设置format为dshow即可。
参考雷神的博客:https://blog.csdn.net/leixiaohua1020/article/details/38284961
public class Sample02_Camera {
public static void main(String[] args) throws Exception {
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("video=Integrated Camera");
grabber.setImageWidth(1280);
grabber.setImageHeight(720);
grabber.setFormat("dshow");
grabber.start();
CanvasFrame canvas = new CanvasFrame("米虫2022");
canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.setAlwaysOnTop(true);
int fps = 1000 / 40;
while (canvas.isDisplayable()) {
canvas.showImage(grabber.grab());
TimeUnit.MILLISECONDS.sleep(fps);
}
grabber.close();
}
}
标签:canvas,音视频,采集,grabber,new,JavaCV,摄像头
From: https://www.cnblogs.com/michong2022/p/17016210.html