OK210-uvc摄像头采集并显示在屏幕上(v4l2编程)

手头有一个UVC(usb video class)摄像头(也称为免驱摄像头),就顺便学习了一下V4L2编程 ,写代码的过程中参考了前辈的博客,觉得写的非常的好,特将链接贴在这里
http://www.cnblogs.com/emouse/archive/2013/03/04/2943243.html
关于V4L2讲解的可以学习前辈的博客,这里只是写了一个实例代码供看了知识点还无从下手写代码的新手作为参考。

平台描述:
    OK210开发板。
    屏幕是开发板自带的800*480的RGB32格式屏幕。
    摄像头输出格式为 640*480 的 YUYV422格式

关于YUYV格式请看这篇博客
http://www.cnblogs.com/azraelly/archive/2013/01/01/2841269.html

关于RGB32的
rgb32 : low memory address —-> high memory address
| pixel | pixel | pixel | pixel | pixel | pixel |…
|——-|——-|——-|——-|——-|——-|…
|B|G|R|A|B|G|R|A|B|G|R|A|B|G|R|A|B|G|R|A|B|G|R|A|..
A表示透明度,0表示不透明,255表示透明度最高

全部代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/fb.h>
#include <linux/videodev2.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>


int status = 1;   //停止标志位,1开始,0停止

/* 监听线程 当启动摄像头后从键盘输入q,结束程序 */
void * listen(void * arg) 
{
    while(1) {
        char buf[20];
        scanf("%s", buf);
        if(strcmp("q", buf) == 0) {
            status = 0;
            break;
        }
        usleep(10);
    }
}

/* 屏幕初始化和销毁函数 */
char * fb_init(char * devname, int * fd, int * len);
void fb_destory(int fd, char * screen_bbf, int screenlen); 

/* 
    函数功能:进行图像转换,将uvc输出的yuyv格式图像转换成RGB32格式的图像
    返回值:无
    参数: 
        yuv yuyv格式图像存储地址
        buf RGB32格式图像存储地址
        length 图像的大小(单位:字节)
*/
void process_image(unsigned char * yuv, unsigned char *buf, int length);
/*
    函数功能:向屏幕输出图像
    返回值:无
    参数 : screen_bbf  内存映射后屏幕在程序中的地址
                    buf                 RGB32格式的数据地址
                    width               图像的宽度
                    height          图像的高度
*/
void show_image(char * screen_bbf, char *buf, int width, int height);


/* 保存摄像头内存映射后的内存地址和数据长度 */
struct buffer {
    char * start;
    unsigned int length;
};

int width,height;

int main(int argc, char ** argv)
{
    /* 摄像头采集的是YUYV格式的图像, 
        屏幕显示需要RGB32格式的图像,
        而且屏幕大小和摄像头的视野大小也不一样
        这里申请一块内存作为缓冲区,存储格式转换后的数据
     */
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值