使用如下代码获取摄像头支持的输出格式。
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];
};
一个摄像头可能支持多种输出格式。因此需要多次查询才能获取支持的所有格式。每次查询时:
材料:
运行 如下代码,并打印返回的结构体信息。
/* 获取并打印摄像头支持的格式 */
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)
----------------------------------------------------------