最近在研究FFmpeg,比较惊讶的是网上一大堆资料都是在说如何从已有的视频中截取一帧图像,却很少说到如何直接从摄像头中捕获一帧图像,其实我一直有个疑问,就是在Linux下,大家是用什么库来采集摄像头的(opencv?)?还是自己写v4l2的代码来实现?我之前一直都是用v4l2来采集摄像头的。经过一些时间的研究,最后成功地用FFmpeg实现了从摄像头采集一帧图像,实现代码也非常简单。不多说,上代码。
#include
#include
#include
#include
#include
#include
#include
#include
void captureOneFrame()
{
AVFormatContext *fmtCtx = NULL;
AVFormatParameters inputFmtParameter;
AVPacket *pcaket;
//输入格式(V4L2)
AVInputFormat *inputFmt = av_find_input_format ("video4linux2");
if (inputFmt == NULL)
{
printf("can not find_input_format\n");
return;
}
memset (&inputFmtParameter, 0, sizeof(inputFmtParameter));
//采集图像的高度
inputFmtParameter.height = 240;
//采集图像的宽度
inputFmtParameter.width = 320;
//打开摄像头设备
if (av_open_input_file ( &fmtCt

本文介绍了如何使用FFmpeg在Linux系统中直接从摄像头捕获一帧图像,而非从已有视频中截取。通过调用`video4linux2`输入格式,设置图像尺寸后打开摄像头设备,读取一帧数据并将其保存为YUV格式的文件。代码示例展示了这一过程。
最低0.47元/天 解锁文章
1807

被折叠的 条评论
为什么被折叠?



