linux 下的v4l2运行

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <linux/videodev2.h>
#include <sys/mman.h>
#include "tool.h"




/*摄像头参数id列表*/
#define BRIGHTNESS_ID 0x00980900
#define CONTRAST_ID 0x00980901
#define SATURATION_ID 0x00980902
#define HUE_ID 0x00980903
#define WHITE_BALANCE_TEMP_AUTO_ID 0x0098090c
#define GAMMA_ID 0x00980910
#define POWER_LINE_FREQUENCY_ID 0x00980918
#define WHITE_BALANCE_TEMP_ID 0x0098091a
#define SHARPNESS_ID 0x0098091b
#define BACKLIGHT_COMPENSATION_ID 0x0098091c
#define EXPOSURE_AUTO_ID 0x009a0901
#define EXPOSURE_ABSOLUTE_ID 0x009a0902
#define EXPOSURE_AUTO_PRIORITY_ID 0x009a0903


struct v4l2_format Format;
struct v4l2_control ctrl;
struct v4l2_streamparm Stream_Parm;


int fd=-1;
char* filename="/home/zienon/xwh/test/res/frame";
struct v4l2_requestbuffers reqbuf={
.count=4,
.type=V4L2_BUF_TYPE_VIDEO_CAPTURE,
.memory=V4L2_MEMORY_MMAP
};


struct buffer{
void* start;
unsigned int length;
}*buffers;


void v4l2_open(){

fd=open("/dev/video0",O_RDWR,0);
if(fd==-1)
perror("open");
}


void set_v4l2_para(fd,value)
{
    
    printf("Set format………………………\n");
    memset(&Format,0,sizeof(struct v4l2_format));
    Format.type= V4L2_BUF_TYPE_VIDEO_CAPTURE;
    Format.fmt.pix.width =  320;
    Format.fmt.pix.height = 240;
    Format.fmt.pix.pixelformat= V4L2_PIX_FMT_YUYV;
    Format.fmt.pix.field = (enum v4l2_field)1;
    if(ioctl(fd,VIDIOC_S_FMT,&Format)==-1)
    {    
        perror("ioctl");
        exit(EXIT_FAILURE);
    }
    sleep(1);
    printf("\n");
    
    printf("Set Stream_Parm……………………\n");
    Stream_Parm.type=V4L2_BUF_TYPE_VIDEO_CAPTURE;
    Stream_Parm.parm.capture.timeperframe.denominator =30;
    Stream_Parm.parm.capture.timeperframe.numerator =1;
    if(ioctl(fd,VIDIOC_S_PARM,&Stream_Parm)==-1)
    {
        perror("ioctl");
        exit(EXIT_FAILURE);
    }
    sleep(1);
    printf("\n");
#if 0
    printf("Set Exposure Auto Type…………………\n");
    ctrl.id=0x009a0901;
    ctrl.value=3;
    if(ioctl(fd,VIDIOC_S_CTRL,&ctrl)==-1)
    {
        perror("ioctl");
        exit(EXIT_FAILURE);
    }
    sleep(1);
    printf("\n");
#endif
#if 0
    printf("Set Exposure value………………\n");
    ctrl.id=0x009a0902;
    ctrl.value=312;
    if(ioctl(fd,VIDIOC_S_CTRL,&ctrl)==-1)
    {
        perror("ioctl");
        exit(EXIT_FAILURE);
    }
    sleep(1);
    printf("\n");
#endif
#if 1
    printf("Set Exposure Auto Priority ………………\n");
    ctrl.id=0x009a0903;
    ctrl.value=2;
    if(ioctl(fd,VIDIOC_S_CTRL,&ctrl)==-1)
    {
        perror("ioctl");
        exit(EXIT_FAILURE);
    }
    sleep(1);
    printf("\n");
#endif
#ifdef XXXX_150
    printf("Set Gain value……………………\n");
    ctrl.id=0x00980913;
    if(ioctl(fd,VIDIOC_S_CTRL,&ctrl)==-1)
    {
        perror("ioctl");
        exit(EXIT_FAILURE);
    }
    sleep(1);
    printf("\n");
#endif
#if 1
    printf("Set white balance Temperature Auto……………………\n");
    ctrl.id=WHITE_BALANCE_TEMP_AUTO_ID;
    ctrl.value=value;
    if(ioctl(fd,VIDIOC_S_CTRL,&ctrl)==-1)
    {
        perror("ioctl");    
        exit(EXIT_FAILURE);
    }
    sleep(1);
    printf("\n");
#endif
    printf("Set white balance Temperature……………………\n");
    ctrl.id=WHITE_BALANCE_TEMP_ID;
    ctrl.value=value;
    if(ioctl(fd,VIDIOC_G_CTRL,&ctrl)==-1)
    {
        perror("ioctl");
        exit(EXIT_FAILURE);
    }
    sleep(1);
    printf("\n");


    printf("Set brightness value……………………\n");
    ctrl.id=BRIGHTNESS_ID;
    ctrl.value=value;
    if(ioctl(fd,VIDIOC_S_CTRL,&ctrl)==-1)
    {
        perror("ioctl");
        exit(EXIT_FAILURE);
    }
    sleep(1);
    printf("\n");


    printf("Set contrast value………………\n");
    ctrl.id=CONTRAST_ID;
    ctrl.value=value;
    if(ioctl(fd,VIDIOC_S_CTRL,&ctrl)==-1)
    {
        perror("ioctl");
        exit(EXIT_FAILURE);
    }
    sleep(1);
    printf("\n");


    printf("Set saturation value…………………\n");
    ctrl.id=SATURATION_ID;
    ctrl.value=value;
    if(ioctl(fd,VIDIOC_G_CTRL,&ctrl)==-1)
    {
        perror("ioctl");
        exit(EXIT_FAILURE);
    }
    sleep(1);
    printf("\n");
    printf("**********************All Set Success*********************\n");
}


void req_buf(){
printf("VIDIOC_REQBUFS\n");
int ret=ioctl(fd,VIDIOC_REQBUFS,&reqbuf);
printf("VIDIOC_REQBUFS\n");
if(ret==-1)
perror("ioctl");
}


void get_buf(){
int n_buffers;
buffers =(struct buffer*)calloc (reqbuf.count, sizeof (*buffers));  
if (!buffers)
perror("calloc");  
for (n_buffers = 0; n_buffers < reqbuf.count; ++n_buffers) {  
struct v4l2_buffer buf;  
memset(&buf,0,sizeof(buf));  
buf.type =V4L2_BUF_TYPE_VIDEO_CAPTURE;  
buf.memory =V4L2_MEMORY_MMAP;  
buf.index =n_buffers;  
// 查询序号为n_buffers 的缓冲区,得到其起始物理地址和大小  
if (-1 == ioctl(fd, VIDIOC_QUERYBUF, &buf))  
exit(-1);  
buffers[n_buffers].length= buf.length;  
// 映射内存  
buffers[n_buffers].start=mmap (NULL,buf.length,PROT_READ | PROT_WRITE ,MAP_SHARED,fd, buf.m.offset);  
if (MAP_FAILED== buffers[n_buffers].start)  
exit(-1);  
}
}


void put_buf_queen(){
        int i;
int ret=-1;  
enum v4l2_buf_type type;  
// 将缓冲帧放入队列  
for (i = 0; i< 4; ++i)  
{  
struct v4l2_buffer buf;  
buf.type =V4L2_BUF_TYPE_VIDEO_CAPTURE;  
buf.memory =V4L2_MEMORY_MMAP;  
buf.index = i;  
ioctl (fd,VIDIOC_QBUF, &buf);  
}  
type =V4L2_BUF_TYPE_VIDEO_CAPTURE;  
ret=ioctl (fd,VIDIOC_STREAMON, &type);
if(ret==-1)
perror("ioctl");
}


void get_frame(){

struct v4l2_buffer buf;   
buf.type =V4L2_BUF_TYPE_VIDEO_CAPTURE;  
buf.memory =V4L2_MEMORY_MMAP;
printf("---------------\n");  
// 从缓冲区取出一个缓冲帧  
ioctl (fd,VIDIOC_DQBUF, &buf);  
printf("---------------\n");  
// 图像处理 
printf("%s\n",filename); 
imageProcess_Wbmp(buffers[buf.index].start,640,480,filename,1,"T");
// 将取出的缓冲帧放回缓冲区  
ioctl (fd, VIDIOC_QBUF,&buf);
}






int main(int argc,char* argv[])
{
//打开设备
v4l2_open();
//set_v4l2_para(fd,30);
//申请缓冲区
printf("req_buf\n");
req_buf();
get_buf();
put_buf_queen();
get_frame();
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

零一2035

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值