zoukankan      html  css  js  c++  java
  • ffmpeg从USB摄像头采集一张原始图片(转)

    本文讲解使用ffmpeg从USB摄像头中采集一帧数据并写入文件保存,测试平台使用全志A20平台,其他平台修改交叉工具链即可移植。开发环境使用eclipse+CDT。交叉工具链使用arm-Linux-gcc4.4版本。

    ffmpeg库的移植和编译请参考博客http://blog.csdn.net/smilefyx/article/details/32714743,开发板usb摄像头支持配置请参照博客http://blog.csdn.net/smilefyx/article/details/29574783。转载请声明。

    1、工程建立

         使用eclipse建立一个空的C++工程,工程目录请参照http://blog.csdn.net/smilefyx/article/details/32714743博文。工程建立后在编译选项中加入-D__STDC_CONSTANT_MACROS,否则会报一个错,可以自己尝试。

    2、编写源代码

         打开刚才创建工程时创建的avcodec.cpp文件,编写测试代码,本文测试代码如下,代码比较简单,没做相应的错误检测等操作:

       

    [html] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. /*  
    2.  *FileName:avcodec.cpp  
    3.  *Author:yingxianFei  
    4.  *Description:capture one frame data from usb camera and save into file named out.yuv .  
    5.  **/  
    6.   
    7. #include <iostream>  
    8.   
    9. using namespace std;  
    10.   
    11. extern "C" {  
    12. #include <math.h>  
    13. #include <libavutil/opt.h>  
    14. #include <libavcodec/avcodec.h>  
    15. #include <libavutil/channel_layout.h>  
    16. #include <libavutil/common.h>  
    17. #include <libavutil/imgutils.h>  
    18. #include <libavutil/mathematics.h>  
    19. #include <libavutil/samplefmt.h>  
    20. #include <libavformat/avformat.h>  
    21. #include <libavdevice/avdevice.h>  
    22. #include <libavutil/dict.h>  
    23. };  
    24.   
    25.   
    26. int main(int argc, char **argv) {  
    27.     int ret;  
    28.     AVFormatContext *fmtCtx = NULL;  
    29.     AVPacket pkt1, *pcaket = &pkt1;  
    30.   
    31.     /*1、注册*/  
    32.     avcodec_register_all();  
    33.     avdevice_register_all();  
    34.     /*2、连接视频源*/  
    35.     AVInputFormat *inputFmt = av_find_input_format("video4linux2");  
    36.     if (NULL != inputFmt) {  
    37.         std::cout << "input device name:" <inputFmt->name <std::endl;  
    38.     } else {  
    39.         std::cout << "Null point!" <std::endl;  
    40.     }  
    41.     /*3、打开视频采集设备*/  
    42.     ret = avformat_open_input(&fmtCtx, "/dev/video0", inputFmt, NULL);  
    43.     if (0 == ret) {  
    44.         std::cout << "Open input device seccess!" <std::endl;  
    45.     }  
    46.     /*4、读取一帧数据,编码依据摄像头类型而定,我使用的摄像头输出的是yuv422格式*/  
    47.     av_read_frame(fmtCtx, pcaket);  
    48.     std::cout << "packet size:" << (pcaket->size) <std::endl;  
    49.     /*5、写入帧数据到文件*/  
    50.     FILE *fp = NULL;  
    51.     fp = fopen("out.yuv", "wb");  
    52.     if (NULL != fp) {  
    53.         //将数据写入文件  
    54.         fwrite(pcaket->data, 1, pcaket->size, fp);  
    55.     }  
    56.     //关闭文件  
    57.     fclose(fp);  
    58.     /*6、释放读取的帧数据*/  
    59.     av_free_packet(pcaket);  
    60.     /*7、关闭视频输入源*/  
    61.     avformat_close_input(&fmtCtx);  
    62.   
    63.     return 0;  
    64. }  

    3、编译测试

         编译工程,将生成的源文件nfs到开发板上,运行可执行文件,成功后将保存一帧数据到out.yuv文件中。

    使用Pyuv工具可以打开文件查看文件内容,打开时按照自己摄像头的参数进行选项配置,如本文测试效果如下:

    http://blog.csdn.net/smilefyx/article/details/33728881

  • 相关阅读:
    POJ 2923 Relocation ★(状态压缩+01背包)
    POJ 1062 昂贵的聘礼 (带限制的最短路)
    HDU 4355 Party All the Time (三分求凸函数极值)
    POJ 1860 Currency Exchange (BellmanFord)
    POJ 2923 Relocation ★(状态压缩+01背包)
    【HNOI2011】数学作业(BZOJ 2326)
    POJ 1062 昂贵的聘礼 (带限制的最短路)
    作为当代大学生,面对着信息增长加快,老化周期变短,你应该如何做?
    作为当代大学生,面对着信息增长加快,老化周期变短,你应该如何做?
    信息分析与预测考前模拟试题
  • 原文地址:https://www.cnblogs.com/xihong2014/p/6653609.html
Copyright © 2011-2022 走看看