树莓派学习专题<7>:使用V4L2驱动获取摄像头数据--获取摄像头支持的输出格式

树莓派学习专题<7>:使用V4L2驱动获取摄像头数据--获取摄像头支持的输出格式

  • 1. 获取摄像头支持的格式
  • 2. 树莓派上的实测

1. 获取摄像头支持的格式

使用如下代码获取摄像头支持的输出格式。

struct v4l2_fmtdesc    stFormatDesc ;
/**********************************************
 * other codes 
 * ********************************************/
stFormatDesc.index = 0 ;
stFormatDesc.type  = V4L2_BUF_TYPE_VIDEO_CAPTURE ;

printf("--Format descriptor --------------------------------------\n") ;

while(-1 != ioctl(g_iFDVideo, VIDIOC_ENUM_FMT, &stFormatDesc))
{
    printf("-- %d. %s (%c %c %c %c)\n", stFormatDesc.index + 1, stFormatDesc.description, 
         (stFormatDesc.pixelformat >> 24) & 0xff, 
         (stFormatDesc.pixelformat >> 16) & 0xff,  
         (stFormatDesc.pixelformat >> 8)  & 0xff,  
         (stFormatDesc.pixelformat >> 0)  & 0xff) ;
    stFormatDesc.index++ ;
}

printf("----------------------------------------------------------\n\n\n") ;

VIDIOC_ENUM_FMT 是ioctl的命令(cmd)。该定义位于头文件 videodev2.h 文件中。该命令用于枚举摄像头支持的格式。

#define VIDIOC_ENUM_FMT         _IOWR('V',  2, struct v4l2_fmtdesc)

该命令需要一个 struct v4l2_fmtdesc 类型的指针参数。该参数用于保存摄像头支持的格式 。
该结构体的定义与说明如下:

/*
 * F O R M A T   E N U M E R A T I O N
 */
struct v4l2_fmtdesc {
	__u32		    index;             /* Format number      */
	__u32		    type;              /* enum v4l2_buf_type */
	__u32               flags;
	__u8		    description[32];   /* Description string */
	__u32		    pixelformat;       /* Format fourcc      */
	__u32		    mbus_code;		/* Media bus code    */
	__u32		    reserved[3];
};

一个摄像头可能支持多种输出格式。因此需要多次查询才能获取支持的所有格式。每次查询时:

  1. index :支持的格式序号。每次查询时,该值需要从0开始递增以获得摄像头支持的其他格式。
  2. type :必须指定为 V4L2_BUF_TYPE_VIDEO_CAPTURE
  3. description :格式描述。
  4. pixelformat :像素格式。

2. 树莓派上的实测

材料:

  1. Raspberry Pi 4B计算机;
  2. IMX219摄像头组件。

运行 如下代码,并打印返回的结构体信息。

    /* 获取并打印摄像头支持的格式 */
    stFormatDesc.index = 0 ;
    stFormatDesc.type  = V4L2_BUF_TYPE_VIDEO_CAPTURE ;

    printf("--Format descriptor --------------------------------------\n") ;
    
    while(-1 != ioctl(g_iFDVideo, VIDIOC_ENUM_FMT, &stFormatDesc))
    {
        printf("-- %d. %s (%c %c %c %c)\n", stFormatDesc.index + 1, stFormatDesc.description, 
             (stFormatDesc.pixelformat >> 24) & 0xff, 
             (stFormatDesc.pixelformat >> 16) & 0xff,  
             (stFormatDesc.pixelformat >> 8)  & 0xff,  
             (stFormatDesc.pixelformat >> 0)  & 0xff) ;
        stFormatDesc.index++ ;
    }
    
    printf("----------------------------------------------------------\n\n\n") ;

返回信息如下:

--Format descriptor --------------------------------------
-- 1. Planar YUV 4:2:0 (2 1 U Y)
-- 2. YUYV 4:2:2 (V Y U Y)
-- 3. 24-bit RGB 8-8-8 (3 B G R)
-- 4. JFIF JPEG (G E P J)
-- 5. H.264 (4 6 2 H)
-- 6. Motion-JPEG (G P J M)
-- 7. YVYU 4:2:2 (U Y V Y)
-- 8. VYUY 4:2:2 (Y U Y V)
-- 9. UYVY 4:2:2 (Y V Y U)
-- 10. Y/UV 4:2:0 (2 1 V N)
-- 11. 24-bit BGR 8-8-8 (3 R G B)
-- 12. Planar YVU 4:2:0 (2 1 V Y)
-- 13. Y/VU 4:2:0 (1 2 V N)
-- 14. 32-bit XBGR 8-8-8-8 (4 2 X R)
----------------------------------------------------------

你可能感兴趣的:(树莓派学习,学习)