V4L2测试程序

#include <stdio.h>
#include <stdlib.h>
#include <linux/videodev2.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>

int fd;/*设备号*/

/*设备文件*/
#define CAMERA_DEVICE "/dev/video0"


int main()
{
    int i, ret;


    // Open Device
    fd = open(CAMERA_DEVICE, O_RDWR);
    if (fd < 0) {
        printf("Open %s failed\n", CAMERA_DEVICE);
        return -1;
    }
    else
    {
        printf("Open %s ok!!\n", CAMERA_DEVICE);
    }
    //Query Capability
    struct v4l2_capability cap;

    ret = ioctl(fd, VIDIOC_QUERYCAP, &cap);
    if (ret < 0) {
        printf("VIDIOC_QUERYCAP failed (%d)\n", ret);
        return ret;
    }
    else
    {
        printf("VIDIOC_QUERYCAP ok!! (%d)\n", ret);
    }
    // Print capability infomations
    printf("Capability Informations:\n");
    printf("driver: %s\n", cap.driver);
    printf("card: %s\n", cap.card);
    printf("bus_info: %s\n", cap.bus_info);
    printf("version: %08X\n", cap.version);
    printf("capabilities: %08X\n", cap.capabilities);


    //enum format
    struct v4l2_fmtdesc fmt;

    memset(&fmt, 0, sizeof(fmt));

    fmt.index = 0;
    fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

    while ((ret = ioctl(fd, VIDIOC_ENUM_FMT, &fmt)) == 0)
    {
        fmt.index++;

        printf("{ pixelformat = ''%c%c%c%c'', description = ''%s'' }\n",

        fmt.pixelformat & 0xFF, (fmt.pixelformat >> 8) & 0xFF,
        (fmt.pixelformat >> 16) & 0xFF, (fmt.pixelformat >> 24) & 0xFF,
        fmt.description);
    }
    
    //query control id
    struct v4l2_queryctrl qctrl;
    
    qctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;

    while (0 == ioctl (fd, VIDIOC_QUERYCTRL, &qctrl))
    {
        // Print control infomations
        printf("**************************************************************\n");
        printf("control Informations:\n");
        printf("id: 0x%0x\t",qctrl.id);
        printf("type: %d\t", qctrl.type);
        printf("name: %s\n", qctrl.name);
        printf("minimum: %d\t", qctrl.minimum);
        printf("maximum: %d\t", qctrl.maximum);
        printf("step: %d\n", qctrl.step);
        printf("default_value: %d\t", qctrl.default_value);
        printf("flags: %d\t", qctrl.flags);
        printf("reserved: %d,%d\n", qctrl.reserved[0],qctrl.reserved[1]);
        
        if(qctrl.type == V4L2_CTRL_TYPE_MENU)
        {
            int idx;
            struct v4l2_querymenu qmenu;
            for (idx = qctrl.minimum; idx <= qctrl.maximum; idx++)
            {
                qmenu.id = qctrl.id;
                qmenu.index = idx;
                if (ioctl(fd, VIDIOC_QUERYMENU, &qmenu)==0)
                {
                    printf("qurey menu Informations:\n");
                    printf("index = %d \t name = %s\n", qmenu.index, qmenu.name);
                }
            }
        }
 
        qctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
    }

    //print V4L2_CID_BASE values
    printf("V4L2_CID_BASE = 0x%0x\n",V4L2_CID_BASE);

    //set control
    struct v4l2_control ctrl;

    ctrl.id = V4L2_CID_AUTO_WHITE_BALANCE;
    ctrl.value = 0;
    printf("set control informations:\n");
    printf("id = %d\t value = %d\n", ctrl.id, ctrl.value);

    ret = ioctl(fd, VIDIOC_S_CTRL, &ctrl);
    if (ret < 0) {
        printf("VIDIOC_S_CTRL failed (%d)\n", ret);
        return ret;
    }
    else
    {
        printf("VIDIOC_S_CTRL ok!! (%d)\n", ret);
    }
    
   //set exposure

    ctrl.id = V4L2_CID_EXPOSURE_AUTO;
    ctrl.value = V4L2_EXPOSURE_MANUAL;
    printf("set exposure auto informations:\n");
    printf("id = %d\t value = %d\n", ctrl.id, ctrl.value);

    ret = ioctl(fd, VIDIOC_S_CTRL, &ctrl);
    if (ret < 0) {
        printf("set exposure auto failed (%d)\n", ret);
        return ret;
    }
    else
    {
        printf("set exposure auto ok!! (%d)\n", ret);
    }

    ctrl.id = V4L2_CID_EXPOSURE_ABSOLUTE;
    ctrl.value = 2000;
    printf("set exposure absolute informations:\n");
    printf("id = %d\t value = %d\n", ctrl.id, ctrl.value);

    ret = ioctl(fd, VIDIOC_S_CTRL, &ctrl);
    if (ret < 0) {
        printf("set exposure absolute failed (%d)\n", ret);
        return ret;
    }
    else
    {
        printf("set exposure absolute ok!! (%d)\n", ret);

    }


    //close Device
    close(fd);
    printf("Camera test Done.\n");

    return 0;

}

注意:在调试V4L2_CID_WHITE_BALANCE_TEMPERATURE的时候,一定要记得把V4L2_CID_AUTO_WHITE_BALANCE的值设置为0.

因为自动白平衡打开是无法设置白平衡的。这里面有这个逻辑在里面。同样的道理,设置曝光值的时候,一定要将曝光设为手动后才能进行

曝光值设定。

你可能感兴趣的:(c,struct,测试,video,include,menu)