最近在研究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 ( &fmtCtx, "/dev/video0", inputFmt,
sizeof(inputFmtParameter),&inputFmtParameter) < 0)
{
printf("can not open_input_file\n");
return;
}
//从摄像头获取一帧图像
av_read_frame(fmtCtx, pcaket);
//输出图像的大小
printf("data length = %d\n",pcaket->size); FILE *fp;
//打开(新建)文件
fp = fopen("out.yuv", "wb");
if (fp < 0)
{
printf("open frame data file failed\n");
return ;
}
//将数据写入文件
fwrite(pcaket->data, 1, pcaket->size, fp);
//关闭文件
fclose(fp); //关闭设备文件
av_close_input_file(fmtCtx);
} int main()
{
avcodec_init();
avcodec_register_all();
avdevice_register_all(); captureOneFrame(); return 0;
}

注意:采集出来的图像的是YV12格式的。

使用FFmpeg捕获一帧摄像头图像的更多相关文章

  1. [转载] 使用FFmpeg捕获一帧摄像头图像

    最近在研究FFmpeg,比较惊讶的是网上一大堆资料都是在说如何从已有的视频中截取一帧图像,却很少说到如何直接从摄像头中捕获一帧图像,其实我一直有个疑问,就是在Linux下,大家是用什么库来采集摄像头的 ...

  2. Python-opencv摄像头图像捕获

    实例一 (灰色调度) #!/usr/bin/env python # _*_ coding:utf-8 _*_ import cv2 as cv import numpy as np capture ...

  3. 在Jetson TX2上捕获、显示摄像头视频

    参考文章:How to Capture and Display Camera Video with Python on Jetson TX2 与参考文章大部分都是相似的,如果不习惯看英文,可以看看我下 ...

  4. 使用JavaCV/OpenCV抓取并存储摄像头图像

    http://blog.csdn.net/ljsspace/article/details/6702178  分类: 图形图像(3)  版权声明:本文为博主原创文章,未经博主允许不得转载. 本程序通过 ...

  5. 在python3下使用OpenCV 抓取摄像头图像提取蓝色

    工作中需要对摄像头进行调试, Python平台大大提高调试效率. 从网找到段代码, 可以从摄像头图像中抠出蓝色. import cv2 import numpy as np cap  = cv2.Vi ...

  6. FFmpeg解码视频帧为jpg图片保存到本地

    FFmpeg解码视频帧为jpg图片保存到本地 - CSDN博客 https://blog.csdn.net/qq_28284547/article/details/78151635

  7. ADAS摄像头图像环视拼接算法

    ADAS摄像头图像环视拼接算法 输入输出接口 Input: (1)4个摄像头采集的图像视频分辨率 (整型int) (2)4个摄像头采集的图像视频格式 (RGB,YUV,MP4等) (3)摄像头标定参数 ...

  8. ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片

    这里请注意,在编译ffmpeg时,不要使用--disable-devices选项. 使用 --enable-encoder=rawvideo --enable-decoder=rawvideo 启用r ...

  9. [转载] ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片

    这里请注意,在编译ffmpeg时,不要使用--disable-devices选项. 使用 --enable-encoder=rawvideo --enable-decoder=rawvideo 启用r ...

随机推荐

  1. Java中把JSON和List结果集互转的代码片段整理

    1.将JSON字符串转换成List结果集的方式: //查询结果集 PageDatums picPageDatums = pictureLibraryService.selectPagePictureF ...

  2. JSP标签JSTL(5)--常用的标签函数

    在使用JSTL的标签函数的时候请务必加上如下代码 <!-- 添加jsp标签的核心库 --> <%@ taglib uri="http://java.sun.com/jsp/ ...

  3. 分布式进阶(八)Linux提示Unable to locate package该如何处理?

    Linux提示Unable to locate package该如何处理? 当你在修改Linux软件源的时候,提示Unable to locate package错误,这是由什么原因导致的呢?又该如何 ...

  4. 9.4、Libgdx简单字符输入

    (官网:www.libgdx.cn) 如果应用需要输入一个字符,比如用户名和密码,可以通过简单的对话框实现. 在桌面中使用一个Swing对话框,提示用户输入字符. 在Android中将会打开一个标准的 ...

  5. java实现http的post和get

    前话说一句:conn.setDefaultRequestProperty(key, value);这个函数是设置属性的,其实可以没有!   自己写了一个简单的get,容易控制 public stati ...

  6. libevent之event

    就如libevent官网上所写的“libevent - an event notification library”,libevent就是一个基于事件通知机制的库,可以看出event是整个库的核心.e ...

  7. Leetcode_62_Unique Paths

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43404205 A robot is located at ...

  8. 【一天一道LeetCode】#15 3Sum

    一天一道LeetCode系列 (一)题目 Given an array S of n integers, are there elements a, b, c in S such that a + b ...

  9. LeetCode之“树”:Path Sum && Path Sum II

    Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path suc ...

  10. 《UNIX网络编程 卷1》之"学习环境搭建"(CentOS 7)

    <UNIX网络编程 卷1>的源码可以从www.unpbook.com下载得到.解压之后的目录为unpv13e. 详细步骤 编译 进入unpv13e目录,按如下步骤编译: ./configu ...