之前一直想把关于Linux下使用V4L2做视频采集、Qt界面显示实时画面的源码整理一下发出来,不过太懒了我,同时那版bug太多不想花精力去修改了,今天不忙就赶紧整理一下把程序删删减减做一个学习版的demo出来。这个demo源码只是供大家学习一下V4L2,如何在Linux下简单的打开一个摄像头设备并且可以获取视频流中的图像帧并显示出来,不然光看V4L2的结构体以及它的example还是不够直观的。
1.我对V4L2采集摄像头数据的部分封装了一个LPF_V4L2.c文件,在这个文件中可以去获取所有设备数量、按指定索引查询设备名称、打开关闭视频流、按指定索引获取视频格式以及获取设备分辨率的属性等等:

LPF_V4L2.h文件:
#ifndef __MAJORV4L2_H_
#define __MAJORV4L2_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <linux/videodev2.h>
#ifdef __cplusplus
extern "C" {
#endif
extern unsigned char *rgb24;
extern int videoIsRun;
int LPF_GetDeviceCount();
char *LPF_GetDeviceName(int index);
char *LPF_GetCameraName(int index);
int LPF_StartRun(int index);
int LPF_GetFrame();
int LPF_StopRun();
char *LPF_GetDevFmtDesc(int index);
int LPF_GetDevFmtWidth();
int LPF_GetDevFmtHeight();
int LPF_GetDevFmtSize();
int LPF_GetDevFmtBytesLine();
int LPF_GetResolutinCount();
int LPF_GetResolutionWidth(int index);
int LPF_GetResolutionHeight(int index);
int LPF_GetCurResWidth();
int LPF_GetCurResHeight();
#ifdef __cplusplus
}
#endif
#endif
关于如何获取视频图像在这里很简单,在打开视频流后,可以通过LPF_GetFrame去取一帧图像到buffer中,在这个函数内部会将取到的YUYV图像数据进行转换,转换为RGB24格式,这是QImage可以直接加载的类型,之后图像处理线程在调用LPF_GetFrame后,可以通过rgb24这个变量拿到图像数据到QImage中,此时Qt就可以去显示在label上了;
LPF_V4L2.c文件:
#include "LPF_V4L2.h"
#ifdef __cplusplus
extern "C" {
#endif
static struct buffer{
void *start;
unsigned int length;
}*buffers;
int buffers_length;
char runningDev[15] = "";
char devName[15] = "";
char camName[32] = "";
char devFmtDesc[4] = "";
int fd = -1;
int videoIsRun = -1;
int deviceIsOpen = -1;
unsigned char *rgb24 = NULL;
static int WIDTH, HEIGHT;
//V4l2相关结构体
static struct v4l2_capability cap;
static struct v4l2_fmtdesc fmtdesc;
static struct v4l2_frmsizeenum frmsizeenum;
static struct v4l2_format format;
static struct v4l2_queryctrl queryctrl;
static struct v4l2_requestbuffers reqbuf;
static struct v4l2_buffer buffer;
static struct v4l2_streamparm streamparm;
//用户控制项ID
static __u32 brightness_id = -1; //亮度
static __u32 contrast_id = -1; //对比度
static __u32 saturation_id = -1; //饱和度
static __u32 hue_id = -1; //色调
static __u32 white_balance_temperature_auto_id = -1; //白平衡色温(自动)
static __u32 white_balance_temperature_id = -1; //白平衡色温
static __u32 gamma_id = -1; //伽马
static __u32 power_line_frequency_id = -1; //电力线频率
static __u32 sharpness_id = -1; //锐度,清晰度
static __u32 backlight_compensation_id = -1; //背光补偿
//扩展摄像头项ID
static __u32 exposure_auto_id = -1; //自动曝光
static __u32 exposure_absolute_id = -1;
void StartVideoPrePare();
void StartVideoStream();
void EndVideoStream();
void EndVideoStreamClear();
int test_device_exist(char *devName);
static int convert_yuv_to_rgb_pixel(int y, int
523



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



