摄像头程序说明.docx
《摄像头程序说明.docx》由会员分享,可在线阅读,更多相关《摄像头程序说明.docx(19页珍藏版)》请在冰豆网上搜索。
摄像头程序说明
camera.c程序代码
#include
#include
#include
#include
#include
#include
#include
#include
#defineERR_FRAME_BUFFER1
#defineERR_VIDEO_OPEN2
#defineERR_VIDEO_GCAP3
#defineERR_VIDEO_GPIC4
#defineERR_VIDEO_SPIC5
#defineERR_SYNC6
#defineERR_FRAME_USING7
#defineERR_GET_FRAME8
typedefstruct_fb_v4l
{
//FrameBuffer信息
intfbfd;//FrameBuffer设备句柄
structfb_var_screeninfovinfo;//FrameBuffer屏幕可变的信息
structfb_fix_screeninfofinfo;//FrameBuffer固定不变的信息
char*fbp;//FrameBuffer内存指针
//video4linux信息
intfd;//
structvideo_capabilitycapability;//
structvideo_bufferbuffer;//
structvideo_windowwindow;//
structvideo_channelchannel[8];//
structvideo_picturepicture;//
structvideo_tunertuner;//
structvideo_audioaudio[8];//
structvideo_mmapmmap;//
structvideo_mbufmbuf;//
unsignedchar*map;
intframe_current;//what'stheframenumberbeingcapturedcurrently?
intframe_using[VIDEO_MAX_FRAME];//帧的状态没有采集还是等待结束?
}fb_v41;
#defineDEFAULT_PALETTEVIDEO_PALETTE_RGB565
#defineFB_FILE"/dev/fb/0"
//V4L_FILE''/dev/video0''
//ZhaoyangModified
//
#defineV4L_FILE"/dev/v4l/video0"//我的板子video0已被占据
/*
structvideo_mmap
{
unsignedintframe;Frame(0-n)fordoublebuffer
intheight,width;
unsignedintformat;shouldbeVIDEO_PALETTE_*
};
structvideo_mbuf
{
intsize;Totalmemorytomap
intframes;Frames
intoffsets[VIDEO_MAX_FRAME];//32
};
*/
/*********************************************************************************************************
**Functionname:
get_grab_frame
**Descriptions:
获取图像帧,该函数调用了VIDIOCMCAPTURE的ioctl,获取一帧图片
**Input:
*vd,参数指针
**frame,帧号
**Output:
无
**Createdby:
**CreatedDate:
**-------------------------------------------------------------------------------------------------------
**Modifiedby:
**ModifiedDate:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
intget_grab_frame(fb_v41*vd,intframe)
{
//如果正在采集中
if(vd->frame_using[frame]){
fprintf(stderr,"get_grab_frame:
frame%disalreadyused.\n",frame);
returnERR_FRAME_USING;
}
vd->mmap.frame=frame;
/**
StartPicturecapturefromthismoment
/**/
if(ioctl(vd->fd,VIDIOCMCAPTURE,&(vd->mmap))<0){
perror("v4l_grab_frame");
returnERR_GET_FRAME;
}
//置为采集忙状态
vd->frame_using[frame]=1;
vd->frame_current=frame;
return0;
}
/*********************************************************************************************************
**Functionname:
get_next_frame
**Descriptions:
获取下一帧的图像
**Input:
*vd,参数指针
**Output:
无
**Createdby:
**CreatedDate:
**-------------------------------------------------------------------------------------------------------
**Modifiedby:
**ModifiedDate:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
intget_first_frame(fb_v41*vd)
{
intret;
vd->frame_current=0;
ret=get_grab_frame(vd,0);
if(ret<0)
returnret;
//等待帧同步
if(ioctl(vd->fd,VIDIOCSYNC,&(vd->frame_current))<0)
{
perror("v4l_grab_sync");
returnERR_SYNC;
}
//采集完毕
vd->frame_using[vd->frame_current]=0;
return(0);
}
/*********************************************************************************************************
**Functionname:
get_next_frame
**Descriptions:
获取下一帧的图像
**Input:
*vd,参数指针
**Output:
返回0表示正常完成返回。
**Createdby:
**CreatedDate:
**-------------------------------------------------------------------------------------------------------
**Modifiedby:
**ModifiedDate:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
intget_next_frame(fb_v41*vd)
{
intret;
vd->frame_current^=1;//两帧采集不是0就是1
ret=get_grab_frame(vd,vd->frame_current);//获取图像数据
if(ret<0)
returnret;
if(ioctl(vd->fd,VIDIOCSYNC,&(vd->frame_current))<0)//等待帧同步
{perror("v4l_grab_sync");
returnERR_SYNC;
}
vd->frame_using[vd->frame_current]=0;//采集完毕置0
return0;
}
/*********************************************************************************************************
**Functionname:
get_frame_address
**Descriptions:
获取帧地址.调用该函数可以获取当前帧的缓冲地址
**Input:
*vd,参数指针
**Output:
返回帧图像数据的指针地址.
**Createdby:
**CreatedDate:
**-------------------------------------------------------------------------------------------------------
**Modifiedby:
**ModifiedDate:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
unsignedchar*get_frame_address(fb_v41*vd)
{
return(vd->map+vd->mbuf.offsets[vd->frame_current]);//从MAP内存中找到当前帧的起始指针
}
/*********************************************************************************************************
**Functionname:
rgb_to_framebuffer
**Descriptions:
写图像数据到Framebuffer,使用该函数前必须成功执行open_framebuffer函数.
**Input:
*vd,参数指针
**width,图像的宽度vd->mmap.width
**height,图像高度
**xoffset,图在FramebufferX轴偏移量vd->vinfo.xoffset
**yoffset,图在FramebufferY轴偏移量
***img_ptr,即将写进FrameBuffer缓冲区指针
**Output:
无
**Createdby:
**CreatedDate:
**-------------------------------------------------------------------------------------------------------
**Modifiedby:
**ModifiedDate:
**
**vd->finfo.line_length
**-------------------------------
**|yoffset|
**|xoffset*|
**||
**||
**-------------------------------
**
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
voidrgb_to_framebuffer(fb_v41*vd,//
intwidth,intheight,//图像大小
intxoffset,intyoffset,//图像在Framebuffer偏移位置
unsignedshort*img_ptr)//图像数据指针
{
intx,y;
intlocation;
unsignedshort*loca_ptr;
//Figureoutwhereinmemorytoputthepixel
for(y=0;y{
location=xoffset*2+
(y+yoffset)*vd->finfo.line_length;
loca_ptr=(unsignedshort*)(vd->fbp+location);
for(x=0;x{
*(loca_ptr+x)=*img_ptr++;
}
}
}
/*
voidrgb_to_framebuffer(fb_v41*vd,//
intwidth,intheight,//图像大小
intxoffset,intyoffset,//图像在Framebuffer偏移位置
unsignedshortint*img_ptr)//图像数据指针
{
intx,y;
intlocation;
//Figureoutwhereinmemorytoputthepixel
for(y=0;y{
for(x=0;x{
location=(x+xoffset)*2+
(y+yoffset)*vd->finfo.line_length;
*((unsignedshortint*)(vd->fbp+location))=*img_ptr++;
}
}
}
*/
/*********************************************************************************************************
**Functionname:
open_framebuffer
**Descriptions:
该函数用于初始化FrameBuffer设备,在该函数中打开FrameBuffer设备,并将设备影射到内存
**Input:
*ptr,打开Framebuffer设备路径指针
***vd,参数指针
**Output:
返回非0值表示出错
**Createdby:
**CreatedDate:
**-------------------------------------------------------------------------------------------------------
**Modifiedby:
**ModifiedDate:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
intopen_framebuffer(char*ptr,fb_v41*vd)
{
intfbfd,screensize;
//Openthefileforreadingandwriting
fbfd=open(ptr,O_RDWR);
if(fbfd<0)
{
printf("Error:
cannotopenframebufferdevice.%x\n",fbfd);
returnERR_FRAME_BUFFER;
}
printf("Theframebufferdevicewasopenedsuccessfully.\n");
vd->fbfd=fbfd;//保存打开FrameBuffer设备的句柄
//Getfixedscreeninformation获取FrameBuffer固定不变的信息
if(ioctl(fbfd,FBIOGET_FSCREENINFO,&vd->finfo))
{
printf("Errorreadingfixedinformation.\n");
returnERR_FRAME_BUFFER;
}
//Getvariablescreeninformation获取FrameBuffer屏幕可变的信息
if(ioctl(fbfd,FBIOGET_VSCREENINFO,&vd->vinfo))
{
printf("Errorreadingvariableinformation.\n");
returnERR_FRAME_BUFFER;
}
printf("%dx%d,%dbpp,xoffset=%d,yoffset=%d\n",vd->vinfo.xres,
vd->vinfo.yres,vd->vinfo.bits_per_pixel,vd->vinfo.xoffset,vd->vinfo.yoffset);
//Figureoutthesizeofthescreeninbytes
screensize=vd->vinfo.xres*vd->vinfo.yres*vd->vinfo.bits_per_pixel/8;
//Mapthedevicetomemory
vd->fbp=(char*)mmap(0,screensize,PROT_READ|PROT_WRITE,MAP_SHARED,fbfd,0);//影射Framebuffer设备到内存
if((int)vd->fbp==-1)
{
printf("Error:
failedtomapframebufferdevicetomemory.\n");
returnERR_FRAME_BUFFER;
}
printf("Theframebufferdevicewasmappedtomemorysuccessfully.\n");
return0;
}
/*********************************************************************************************************
**Functionname:
open_video
**Descriptions:
通过该函数初始化视频设备
**Input:
*fileptr,打开的文件名指针
***vd,参数指针
**dep,像素深度
**pal,调色板
**width,宽度
**height,高度
**Output:
无
**Createdby:
**CreatedDate:
**-------------------------------------------------------------------------------------------------------
**Modifiedby:
**ModifiedDate:
**------------------------------------------------------------