V4L图像采集总结(转)

来源:互联网 发布:破解苹果mac密码 编辑:程序博客网 时间:2024/05/29 04:43
 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/beyonddream2008/archive/2009/05/20/4204594.aspx
采用中星微ZC301摄像头的V4L图像采集

 

#ifndef _V4L_H_
#define _V4L_H_

#include <linux/videodev.h>
#include <sys/types.h>
//PAL CIF NTSC tuner mode
#define PAL_WIDTH 768 
#define PAL_HEIGHT 576
#define CIF_WIDTH 352
#define CIF_HEIGHT 288
#define NTSC_WIDTH 80  //设置获取图像的大小
#define NTSC_HEIGHT 60
#define DEFAULT_PALETTE VIDEO_PALETTE_RGB32

struct _v4l_device
{
    int fd;//设备号
    struct video_capability capability;//摄像头属性
    struct video_picture picture;//图像的属性,亮度、色度、对比度、灰度、编码属性
    struct video_window window;//包含capture area 的信息
    struct video_channel channel[8];//采集的通道
    struct video_mbuf mbuf;//利用mmap映射得侦信息
    struct video_capture capture;
    struct video_buffer buffer;
    struct video_mmap mmap;
    unsigned char *map;
    int frame;
    int framestat[2];
};
typedef struct _v4l_device v4ldevice;

extern int v4l_open(char *,v4ldevice *);
extern int v4l_set_norm(v4ldevice *, int);
extern int v4l_get_capability(v4ldevice *);
extern int v4l_get_window(v4ldevice *);
extern int v4l_set_window(v4ldevice *);
extern int v4l_get_picture(v4ldevice *);
extern int v4l_mmap_init(v4ldevice *);
extern int v4l_grab_init(v4ldevice *,int ,int);
extern int v4l_grab_start(v4ldevice *,int );
extern int v4l_grab_sync(v4ldevice *,int);
extern int v4l_get_capture(v4ldevice *);//,int);
extern int v4l_set_capture(v4ldevice *);//,int);
extern unsigned char *v4l_get_address(v4ldevice *);
extern int v4l_close(v4ldevice *);
extern void set(v4ldevice *);
#endif

 

 

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <error.h>
#include <fcntl.h>
#include <sys/types.h>
#include <linux/videodev.h>
#include "v4l.h"
#define DEFAULT_DEVICE "/dev/video0"
int v4l_open(char *dev,v4ldevice *vd)
{
    if (!dev)
              dev = DEFAULT_DEVICE;

    if((vd->fd=open(dev,O_RDWR,10705))<0)
    {
        return -1;
    };
    if(v4l_get_capability(vd)<0)
        return -1;
    if(v4l_get_picture(vd)<0)
        return -1;
    return 0;
}

int v4l_get_capability(v4ldevice *vd)
{
    if(ioctl(vd->fd,VIDIOCGCAP,&(vd->capability))<0)
    {   
        perror("v4l_get_capability:");
        return -1;
    }

   return 0;

}

int v4l_get_picture(v4ldevice *vd)
{
    if(ioctl(vd->fd,VIDIOCGPICT,&(vd->picture))<0)
    {
        perror("v4l_get_picture");
        return -1;       
    }

   return 0;

}

int v4l_set_norm(v4ldevice *vd, int norm)
{
   int i;

   for (i = 0; i < vd->capability.channels; i++) {
      vd->channel[i].norm = norm;

  return 0;

}

int v4l_grab_init(v4ldevice *vd,int width,int height)
{
    vd->mmap.width=width;
    vd->mmap.height=height;
    vd->mmap.format=vd->picture.palette;
    vd->frame=0;
    vd->framestat[0]=0;
    vd->framestat[1]=0;   
    return 0;
}

int v4l_mmap_init(v4ldevice *vd)
{
    if(v4l_get_mbuf(vd)<0)
        return -1;
    if((vd->map=mmap(0,vd->mbuf.size,PROT_READ|PROT_WRITE,MAP_SHARED,vd->fd,0))<0)
    {
        return -1;
    }
    return 0;
}

int v4l_get_mbuf(v4ldevice *vd)//查询实际可用的缓存数
{
    if(ioctl(vd->fd,VIDIOCGMBUF,&(vd->mbuf))<0)
    {
        perror("v4l_get_mbuf:");   
        return -1;
    }
    printf("size=%d\n",vd->mbuf.size);
return 0;
}

int v4l_grab_start(v4ldevice *vd,int frame)
{
    vd->mmap.frame=frame;
    if(ioctl(vd->fd,VIDIOCMCAPTURE,&(vd->mmap))<0)//////////////////////
    {
        exit(-1);
        return -1;
    }
    vd->framestat[frame]=1;
    return 0;
}
int v4l_grab_sync(v4ldevice *vd,int frame)
{
    if(ioctl(vd->fd,VIDIOCSYNC,&frame)<0)
    {
        return -1;   
    }
    vd->framestat[frame]=0;
    return 0;
}

unsigned char * v4l_get_address(v4ldevice *vd)
{
    return (vd->map+vd->mbuf.offsets[vd->frame]);
}
int v4l_close(v4ldevice *vd)
{
    close(vd->fd);
    return 0;
}

 

 

 

#include "v4l.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#define norm VIDEO_MODE_NTSC
#define DEFAULT_FILE_NAME "picture"
int main()
{

    char *buffer=NULL;
    v4ldevice VD;
    v4ldevice *vd=&VD;

    int frame=0;
    int f_d;

    f_d=open(DEFAULT_FILE_NAME,O_RDWR|O_CREAT,0666);//获取文件的描述符
   
    if(0==v4l_open("/dev/video0",vd)) //打开设备
        printf("open success!\n");
    else
        printf("open failure\n");
//    set (vd);
    if(0==v4l_set_norm(vd,norm))
        printf("set_norm success\n");
    else
        printf("set_norm failure\n");
    if(0==v4l_grab_init(vd,NTSC_WIDTH,NTSC_HEIGHT))//初始化设备,定义获取图像的大小
        printf("init success!\n");
    else
        printf("init failure\n");
    if(0==v4l_mmap_init(vd))//内存映射
        printf("memory map success!\n");
    else
        printf("memory map failure\n");
    if(0==v4l_grab_start(vd,frame))//开始获取图像
        printf("get picture success!\n");
    else
        printf("get picture failure\n");    

   v4l_grab_sync(vd,frame);//等待传完一帧
       
        buffer=(char *)v4l_get_address(vd);//得到这一帧的地址
        printf("img address %p\n",buffer);

    write(f_d,buffer,NTSC_WIDTH*3*NTSC_HEIGHT);//报存到文件中   

    v4l_close(vd);
    return 0;
   
}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 这几天我的内裤上带点血丝怎么办 一个月宝宝私处捂白了怎么办 工资算了字签了老板没把钱怎么办 百合种球叶子都变黄了怎么办 新买的富贵竹叶子发黄怎么办水养 朋友总是以隐私为借口欺骗我怎么办 三星手机版本低下载不了微信怎么办 选了动漫制作技术但不会画画怎么办 做主播高薪可是心累不愿做了怎么办 pr导出的avi无压缩太大怎么办 捡了个小米max被绑定了怎么办 二十岁时头发开始掉了怎么办 在酒店换衣服忘记关窗帘了怎么办 淘宝店铺装修更改图片要收费怎么办 惠阳市教育考试考证号忘记怎么办 高考完被被骗去读自考以后怎么办 孩子学习遇到瓶颈期了老师该怎么办 微信家长群有不好的言论出现怎么办 铃木汽车后备箱电动锁没有电怎么办 坐飞机没有连号座位带孩子怎么办 白沙的衣服洗衣服时染上颜色怎么办 网购商家少发了货怎么办 我想成为安利的员工怎么办会员 安利皇后锅锅盖吸在桌子上怎么办 淘宝客服退款返佣金诈骗后怎么办 第一试用网的钱提现出现问题怎么办 一个手机号注册两个京东账号怎么办 白色衣服被洗衣粉泡白了怎么办 白色衣服染成一块块荧光色了怎么办 中脉远红镇痛护腰不会发热了怎么办 用完悦诗风吟脸变黑不均匀怎么办 护肤品开封后一年还没用完怎么办 兰蔻化妆品套装正品和假怎么办 月经期间卫生巾搞得屁股疼怎么办 大姨妈特别多用卫生巾老是漏怎么办 夏天用卫生巾不透气摩擦红了怎么办 在日本的洗手间用完的姨妈巾怎么办 想穿短裙但是膝盖怕凉怎么办 裤子被卫生巾粘住扯不下来怎么办 医生说来姨妈不可以用卫生巾怎么办 隆胸以后摸起来感觉假体会动怎么办