嵌入式linux学习之通过v4l读取usb摄像头数据

格式初始化部分

预定义及头文件部分
c 复制代码
#include <opencv2/opencv.hpp>  
#include <iostream>  
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>
#include <errno.h>
#include <sys/mman.h>
#include <linux/videodev2.h>
#include <linux/fb.h>
#include<assert.h>
#include<limits.h>
//#include<opencv/highgui.h>
//#include <opencv2/opencv.hpp>
//#include<iterator>
#define FB_DEV              "/dev/fb0"      //LCD设备节点
#define FRAMEBUFFER_COUNT   3               //帧缓冲数量
/*** 摄像头像素格式及其描述信息 ***/
typedef struct camera_format {
    unsigned char description[32];  //字符串描述信息
    unsigned int pixelformat;       //像素格式
} cam_fmt;
/*** 描述一个帧缓冲的信息 ***/
typedef struct cam_buf_info {
    unsigned short *start;      //帧缓冲起始地址
    unsigned long length;       //帧缓冲长度
} cam_buf_info;
static int width;                       //LCD宽度
static int height;                      //LCD高度
static int line_length;
static unsigned short *screen_base = NULL;//LCD显存基地址
static int fb_fd = -1;                  //LCD设备文件描述符
static int v4l2_fd = -1;                //摄像头设备文件描述符
static cam_buf_info buf_infos[FRAMEBUFFER_COUNT];
static cam_fmt cam_fmts[10];
static int frm_width, frm_height;   //视频帧宽度和高度
LCD缓冲初始化
c 复制代码
static int fb_dev_init(void)
{
    struct fb_var_screeninfo fb_var = {0};
    struct fb_fix_screeninfo fb_fix = {0};
    unsigned long screen_size;
    fb_fd = open(FB_DEV, O_RDWR);/* 打开framebuffer设备 */
    if (0 > fb_fd) {
        fprintf(stderr, "open error: %s: %s\n", FB_DEV, strerror(errno));
        return -1;
    }
    ioctl(fb_fd, FBIOGET_VSCREENINFO, &fb_var); /* 获取framebuffer设备信息 */
    ioctl(fb_fd, FBIOGET_FSCREENINFO, &fb_fix);
    screen_size = fb_fix.line_length * fb_var.yres;
    width = fb_var.xres;
    height = fb_var.yres;
    line_length = fb_fix.line_length / (fb_var.bits_per_pixel / 8);
    /* 内存映射 */
    screen_base = mmap(NULL, screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fb_fd, 0);
    if (MAP_FAILED == (void *)screen_base) {
        perror("mmap error");
        close(fb_fd);
        return -1;
    }
    /* LCD背景刷白 */
    memset(screen_base, 0xFF, screen_size);
    return 0;
}
v4l2设备初始化
c 复制代码
static int v4l2_dev_init(const char *device)
{
    struct v4l2_capability cap = {0};
    /* 打开摄像头 */
    v4l2_fd = open(device, O_RDWR);
    if (0 > v4l2_fd) {
        fprintf(stderr, "open error: %s: %s\n", device, strerror(errno));
        return -1;
    }
    /* 查询设备功能 */
    ioctl(v4l2_fd, VIDIOC_QUERYCAP, &cap);
    /* 判断是否是视频采集设备 */
    if (!(V4L2_CAP_VIDEO_CAPTURE & cap.capabilities)) {
        fprintf(stderr, "Error: %s: No capture video device!\n", device);
        close(v4l2_fd);
        return -1;
    }
    return 0;
}
v4l2列举可用格式
c 复制代码
static void v4l2_enum_formats(void)
{
    struct v4l2_fmtdesc fmtdesc = {0};
    /* 枚举摄像头所支持的所有像素格式以及描述信息 */
    fmtdesc.index = 0;
    fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    while (0 == ioctl(v4l2_fd, VIDIOC_ENUM_FMT, &fmtdesc)) {
        // 将枚举出来的格式以及描述信息存放在数组中
        cam_fmts[fmtdesc.index].pixelformat = fmtdesc.pixelformat;
        strcpy(cam_fmts[fmtdesc.index].description, fmtdesc.description);
        fmtdesc.index++;
    }
}
v4l2打印可用格式
c 复制代码
static void v4l2_print_formats(void)
{
    struct v4l2_frmsizeenum frmsize = {0};
    struct v4l2_frmivalenum frmival = {0};
    int i;
    frmsize.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    frmival.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    for (i = 0; cam_fmts[i].pixelformat; i++) {
        printf("format<0x%x>, description<%s>\n", cam_fmts[i].pixelformat,cam_fmts[i].description);
        /* 枚举出摄像头所支持的所有视频采集分辨率 */
        frmsize.index = 0;
        frmsize.pixel_format = cam_fmts[i].pixelformat;
        frmival.pixel_format = cam_fmts[i].pixelformat;
        while (0 == ioctl(v4l2_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize)) {
            printf("size<%d*%d> ",frmsize.discrete.width,frmsize.discrete.height);
			frmsize.index++;
            /* 获取摄像头视频采集帧率 */
            frmival.index = 0;
            frmival.width = frmsize.discrete.width;
            frmival.height = frmsize.discrete.height;
            while (0 == ioctl(v4l2_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival)) {
                printf("<%dfps>", frmival.discrete.denominator /frmival.discrete.numerator);
                frmival.index++;
            }
            printf("\n");
        }
        printf("\n");
    }
}
v4l2设置图像格式
c 复制代码
static int v4l2_set_format(void)
{
    struct v4l2_format fmt = {0};
    struct v4l2_streamparm streamparm = {0};
    /* 设置帧格式 */
    fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;//type类型
    fmt.fmt.pix.width =640;//width;  //视频帧宽度
    fmt.fmt.pix.height =480;//视频帧高度
    fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;  //像素格式 RGB565 -> YUYV
    if (0 > ioctl(v4l2_fd, VIDIOC_S_FMT, &fmt)) {
       fprintf(stderr, "ioctl error: VIDIOC_S_FMT: %s\n", strerror(errno));
       return -1;
    }
    /*** 判断是否已经设置为我们要求的YUYV像素格式,如果没有设置成功表示该设备不支持的像素格式 */
    if (V4L2_PIX_FMT_YUYV != fmt.fmt.pix.pixelformat) {
        fprintf(stderr, "Error: the device does not support YUYV format!\n");
        return -1;
    }
    frm_width = fmt.fmt.pix.width;  //获取实际的帧宽度
    frm_height = fmt.fmt.pix.height;//获取实际的帧高度
    printf("视频帧大小<W,H>:<%d * %d>\n", frm_width, frm_height);
    /* 获取streamparm */
    streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    ioctl(v4l2_fd, VIDIOC_G_PARM, &streamparm);
    /** 判断是否支持帧率设置 **/
    if (V4L2_CAP_TIMEPERFRAME & streamparm.parm.capture.capability) {
        streamparm.parm.capture.timeperframe.numerator = 1;
        streamparm.parm.capture.timeperframe.denominator = 30;//30fps
        if (0 > ioctl(v4l2_fd, VIDIOC_S_PARM, &streamparm)) {
            fprintf(stderr, "ioctl error: VIDIOC_S_PARM: %s\n", strerror(errno));
            return -1;
        }
    }
    return 0;
}
v4l2初始化缓冲器
c 复制代码
static int v4l2_init_buffer(void)
{
    struct v4l2_requestbuffers reqbuf = {0};
    struct v4l2_buffer buf = {0};
    /* 申请帧缓冲 */
    reqbuf.count = FRAMEBUFFER_COUNT;       //帧缓冲的数量
    reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    reqbuf.memory = V4L2_MEMORY_MMAP;
    if (0 > ioctl(v4l2_fd, VIDIOC_REQBUFS, &reqbuf)) {
        fprintf(stderr, "ioctl error: VIDIOC_REQBUFS: %s\n", strerror(errno));
        return -1;
    }
    /* 建立内存映射 */
    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    buf.memory = V4L2_MEMORY_MMAP;
    for (buf.index = 0; buf.index < FRAMEBUFFER_COUNT; buf.index++) {
        ioctl(v4l2_fd, VIDIOC_QUERYBUF, &buf);
        buf_infos[buf.index].length = buf.length;
        buf_infos[buf.index].start = mmap(NULL, buf.length,PROT_READ | PROT_WRITE, MAP_SHARED,v4l2_fd, buf.m.offset);
        if (MAP_FAILED == buf_infos[buf.index].start) {
            perror("mmap error");
            return -1;
        }
    }
    /* 入队 */
    for (buf.index = 0; buf.index < FRAMEBUFFER_COUNT; buf.index++) {
        if (0 > ioctl(v4l2_fd, VIDIOC_QBUF, &buf)) {
            fprintf(stderr, "ioctl error: VIDIOC_QBUF: %s\n", strerror(errno));
            return -1;
        }
    }
    return 0;
}
v4l2开启视频流
c 复制代码
static int v4l2_stream_on(void)

{
    /* 打开摄像头、摄像头开始采集数据 */
    enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    if (0 > ioctl(v4l2_fd, VIDIOC_STREAMON, &type)) {
        fprintf(stderr, "ioctl error: VIDIOC_STREAMON: %s\n", strerror(errno));
        return -1;
    }
    return 0;
}
yuyv格式数据转换为RGB565
c 复制代码
unsigned short yuyv_to_rgb565(unsigned short y, unsigned short u,unsigned short v) {  
   int16_t r, g, b;  
    // 将Y, U, V从8位无符号转换为带符号的16位值  
    int16_t y1 = y;  
     int16_t u1 = u - 128;    
     int16_t v1 = v - 128;  
    r = y1 + 1.042*(v1);
    g = y1- 0.34414*(u1) - 0.71414*v1;
    b = y1+ 1.772*u1;
    // 将r, g, b值限制在0-255的范围内  
    r = r < 0 ? 0 : (r > 255 ? 255 : r);  
    g = g < 0 ? 0 : (g > 255 ? 255 : g);  
    b = b < 0 ? 0 : (b > 255 ? 255 : b);  
    // 将r, g, b值转换为RGB565格式  
    return ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);  
}

数据读取

读取图像帧用lcd显示
c 复制代码
static void v4l2_read_data(void)

{
    struct v4l2_buffer buf = {0};
    unsigned short *base;
    unsigned short *start;
    int min_w, min_h;
    int j;
    if (width > frm_width)//以下判断仅为了适配屏幕大小
        min_w = frm_width;
    else
        min_w = width;
    if (height > frm_height)
        min_h = frm_height;
    else
        min_h = height;
    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    buf.memory = V4L2_MEMORY_MMAP;
    for ( ; ; ) {
        for(buf.index = 0; buf.index < FRAMEBUFFER_COUNT; buf.index++) { 
            ioctl(v4l2_fd, VIDIOC_DQBUF, &buf);     //出队
            printf("length:%d\n",buf_infos[buf.index].length);
            //  cv::Mat bgrImage = convertYUYVtoBGR(buf_infos[buf.index].start,frm_width,frm_height);//使用opencv将yuyv格式转化为jpg图像
            // cv::imwrite("img.jpg",bgrImage);
    for (j = 0, base=screen_base, start=buf_infos[buf.index].start;j < min_h; j++) {//base是屏幕基地址,start是帧起始地址
		for (int i = 0; i < min_w; i +=2) {  //unsigned short一次两个字节
            unsigned short  y0 = start[i] & 0x00ff;  //低字节存放的Y
            unsigned short u  = start[i] >> 8;  //高字节存放的U或者V
            unsigned short y1 = start[i + 1]  & 0x00ff;  
            unsigned short v  = start[i + 1] >> 8;  //相邻两个Y共用U和V,YUYV一共4个字节数据
            unsigned short rgb0 = yuyv_to_rgb565(y0, u, v); // 第一个像素  
            unsigned short rgb1 = yuyv_to_rgb565(y1, u, v); // 第二个像素  
            *(unsigned short *)base = rgb0;  //RGB565适用于正点原子4.3寸屏幕显示,一个像素两个字节
            base += 1;  
            *(unsigned short *)base = rgb1;  
            base += 1;     
    }                  
                base = screen_base+(j+1)* line_length;//line_length是屏幕的宽度width
                start += frm_width;//指向下一行数据
            }  
            ioctl(v4l2_fd, VIDIOC_QBUF, &buf);// 数据处理完之后、再入队、往复
        }
    }
}
使用opencv保存图像
c 复制代码
cv::Mat convertYUYVtoBGR( unsigned short * yuyvData, int w, int h) {  
    printf("frame:<w,h>:<%d,%d>\n",w,h);//w,h图像帧的宽度和高度
    cv::Mat bgrImage(w, h, CV_8UC3);  //初始化矩阵宽度及高度,CV_8UC3表示8位无符号整形,每个位置存放三个字节元素
	unsigned short *start;
	int j=0;
      for (start=yuyvData;j < h; j++) {
		for (int i = 0; i< w; i +=2) {  
            unsigned short  Y1 = start[i] & 0x00ff;  
            unsigned short U  = start[i] >> 8;  
            unsigned short Y2 = start[i + 1]  & 0x00ff;  
            unsigned short V  = start[i + 1] >> 8;  

            int U_ = U - 128;  
            int V_ = V - 128;  
            int R1 = Y1 + 1.402 * V_;  
            int G1 = Y1 - 0.34414 * U_ - 0.71414 * V_;  
            int B1 = Y1 + 1.772 * U_;  
             int R2 = Y2 + 1.402 * V_;  
            int G2 = Y2 - 0.34414 * U_ - 0.71414 * V_;  
            int B2 = Y2 + 1.772 * U_;  
            
            bgrImage.at<cv::Vec3b>(j, i)[0] = std::min(std::max(B1, 0), 255);  //B
            bgrImage.at<cv::Vec3b>(j, i)[1] = std::min(std::max(G1, 0), 255);  //G
            bgrImage.at<cv::Vec3b>(j, i)[2] = std::min(std::max(R1, 0), 255);  //R
            bgrImage.at<cv::Vec3b>(j, i+1)[0] = std::min(std::max(B2, 0), 255);  
            bgrImage.at<cv::Vec3b>(j, i+1)[1] = std::min(std::max(G2, 0), 255);  
            bgrImage.at<cv::Vec3b>(j, i+1)[2] = std::min(std::max(R2, 0), 255); 
    }               
                start += w;//指向下一行数据
            }
    return bgrImage;  
}  
主函数
c 复制代码
int main(int argc, char *argv[])//使用:./gpio /dev/video
{
    if (2 != argc) {//使用时指定视频设备
        fprintf(stderr, "Usage: %s <video_dev>\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    /* 初始化LCD */
    if (fb_dev_init())
        exit(EXIT_FAILURE);
    /* 初始化摄像头 */
    if (v4l2_dev_init(argv[1]))
        exit(EXIT_FAILURE);
    /* 枚举所有格式并打印摄像头支持的分辨率及帧率 */
    v4l2_enum_formats();
    v4l2_print_formats();
    /* 设置格式 */
    if (v4l2_set_format())
        exit(EXIT_FAILURE);
    /* 初始化帧缓冲:申请、内存映射、入队 */
    if (v4l2_init_buffer())
        exit(EXIT_FAILURE);
    /* 开启视频采集 */
    if (v4l2_stream_on())
        exit(EXIT_FAILURE);
    /* 读取数据:出队 */
    v4l2_read_data();       //在函数内循环采集数据、将其显示到LCD屏
    exit(EXIT_SUCCESS);
}
相关推荐
Code哈哈笑1 分钟前
【Java 学习】深度剖析Java多态:从向上转型到向下转型,解锁动态绑定的奥秘,让代码更优雅灵活
java·开发语言·学习
Youkiup6 分钟前
【linux 常用命令】
linux·运维·服务器
qq_2975046110 分钟前
【解决】Linux更新系统内核后Nvidia-smi has failed...
linux·运维·服务器
weixin_4373982123 分钟前
Linux扩展——shell编程
linux·运维·服务器·bash
小燚~25 分钟前
ubuntu开机进入initramfs状态
linux·运维·ubuntu
小林熬夜学编程32 分钟前
【Linux网络编程】第十四弹---构建功能丰富的HTTP服务器:从状态码处理到服务函数扩展
linux·运维·服务器·c语言·网络·c++·http
炫彩@之星36 分钟前
Windows和Linux安全配置和加固
linux·windows·安全·系统安全配置和加固
hhhhhhh_hhhhhh_1 小时前
ubuntu18.04连接不上网络问题
linux·运维·ubuntu
QQ同步助手1 小时前
如何正确使用人工智能:开启智慧学习与创新之旅
人工智能·学习·百度
冷心笑看丽美人1 小时前
探秘 DNS 服务器:揭开域名解析的神秘面纱
linux·运维·服务器·dns