- 论坛徽章:
- 0
|
V4L图像采集总结
采用中星微ZC301摄像头的V4L图像采集
/**********v4l.h*******************
/*name :v4l.h
date:2009-5-20
author:kevin
copyright:all is reserved
**************************/
#ifndef _V4L_H_
#define _V4L_H_
#include
#include
//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
/***********************v4l.c****************************/
/*name :v4l.c
date:2009-5-20
author:kevin
copyright:all is reserved
************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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))fd,VIDIOCGCAP,&(vd->capability))fd,VIDIOCGPICT,&(vd->picture))capability.channels; i++) {
vd->channel.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)map=mmap(0,vd->mbuf.size,PROT_READ|PROT_WRITE,MAP_SHARED,vd->fd,0))fd,VIDIOCGMBUF,&(vd->mbuf))mbuf.size);
return 0;
}
int v4l_grab_start(v4ldevice *vd,int frame)
{
vd->mmap.frame=frame;
if(ioctl(vd->fd,VIDIOCMCAPTURE,&(vd->mmap))framestat[frame]=1;
return 0;
}
int v4l_grab_sync(v4ldevice *vd,int frame)
{
if(ioctl(vd->fd,VIDIOCSYNC,&frame)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;
}
/*************************************************************/
/******************main.c************************/
/*name :main.c
date:2009-5-20
author:kevin
copyright:all is reserved
************************************/
#include "v4l.h"
#include
#include
#include
#include
#include
#include
#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;
}
本文来自CSDN博客,转载请标明出处:
http://blog.csdn.net/beyonddream2008/archive/2009/05/20/4204594.aspx
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/105052/showart_2121229.html |
|