利用摄像头进行图片采集,出不来图片
用中星微芯片的摄像头zc301p,最后保存为jpg图品,为什么查看图片是空的,但是图片有大小,有48KB。
程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <error.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <linux/videodev.h>
#include <unistd.h>
typedef struct v4l
{
int fd;
struct video_capability capability;
struct video_buffer buffer;
struct video_window window;
struct video_channel channel[8];
struct video_picture picture;
struct video_tuner tuner;
struct video_audio audio[8];
struct video_mmap mmap;
struct video_mbuf mbuf;
unsigned char *map;
int frame_current;
int frame_using[VIDEO_MAX_FRAME];
}v4l;
#define ERR_FRAME_BUFFER 1
#define ERR_VIDEO_OPEN 2
#define ERR_VIDEO_GCAP 3
#define ERR_VIDEO_GPIC 4
#define ERR_VIDEO_SPIC 5
#define ERR_SYNC 6
#define ERR_FRAME_USING 7
#define ERR_GET_FRAME 8
#define V4L_FILE "/dev/video0"
#define IMAGINE_SIZE 50000
int open_video(char * fileptr,v4l *vd,int dep,int pal,int width,int height)
{
if ((vd->fd = open(fileptr, O_RDWR)) < 0) {
perror("v4l_open:");
return ERR_VIDEO_OPEN;
}
printf("open video success!\n");
//获取设备
if (ioctl(vd->fd, VIDIOCGCAP, &(vd->capability)) < 0) {
perror("v4l_get_capability:");
return ERR_VIDEO_GCAP;
}
printf("get device success!");
//获取图象
if (ioctl(vd->fd, VIDIOCGPICT, &(vd->picture)) < 0) {
perror("v4l_get_picture");
return ERR_VIDEO_GPIC;
}
printf("get picture success!\n");
//设置图像
vd->picture.palette =pal;
vd->picture.depth = dep;
if (ioctl(vd->fd, VIDIOCSPICT, &(vd->picture)) < 0) {
perror("v4l_set_palette");
return ERR_VIDEO_SPIC;
}
vd->mmap.format=pal;
vd->mmap.width=width;
vd->mmap.height=height;
vd->mmap.format=vd->picture.palette;
vd->frame_current = 0;
vd->frame_using[0] = 0;
vd->frame_using[1] = 0;
//获取缓冲映射信息
if (ioctl(vd->fd, VIDIOCGMBUF, &(vd->mbuf)) < 0) {
perror("v4l_get_mbuf");
return -1;
}
//建立设备内存映射
vd->map = mmap(0, vd->mbuf.size, PROT_READ|PROT_WRITE, MAP_SHARED, vd->fd, 0);
if(vd->map<0)
{
perror("v4l_mmap_init:mmap");
return -1;
}
printf("the video device was opened successfully.\n");
return 0;
}
int get_grab_frame(v4l *vd, int frame)
{
if (vd->frame_using[frame]) {
fprintf(stderr, "v4l_grab_frame: frame %d is already used.\n", frame);
return ERR_FRAME_USING;
}
vd->mmap.frame = frame;
if (ioctl(vd->fd, VIDIOCMCAPTURE, &(vd->mmap)) < 0) {
perror("v4l_grab_frame");
return ERR_GET_FRAME;
}
vd->frame_using[frame] = 1;
vd->frame_current = frame;
return 0;
}
int get_frist_frame(v4l * vd)
{
int ret;
vd->frame_current=0;
ret=get_grab_frame(vd,vd->frame_current);
if(ret<0)
return ret;
//等待帧同步
if (ioctl(vd->fd, VIDIOCSYNC, &(vd->frame_current)) < 0) {
perror("v4l_grab_sync");
return ERR_SYNC;
}
vd->frame_using[vd->frame_current] = 0;
return 0;
}
unsigned char * get_frame_address(v4l * vd)
{
return (vd->map+vd->mbuf.offsets[vd->frame_current]);
}
int get_next_frame(v4l * vd)
{
int ret;
vd->frame_current^=1;
ret=get_grab_frame(vd,vd->frame_current);
if(ret<0)
return ret;
if(ioctl(vd->fd, VIDIOCSYNC, &(vd->frame_current)) < 0) {
perror("v4l_grab_sync");
return ERR_SYNC;
}
vd->frame_using[vd->frame_current] = 0;
return 0;
}
int save_picture(unsigned char *image,unsigned char *name)
{
FILE *fp;
if(image==NULL)
{
printf("Cannt save picture\n");
return -1;
}
fp=fopen(name,"w");
fwrite(image,IMAGINE_SIZE,1,fp);
fclose(fp);
printf("Capture picture:%s\n",name);
return 0;
}
int main()
{
v4l vd;
int ret,i;
unsigned char *imageptr;
ret=open_video(V4L_FILE,&vd,8,21,320,240);
if(0!=ret)
{
goto err;
}
printf("Name : %s \n",vd.capability.name);
printf("Type: %d \n",vd.capability.type);
printf("Channels: %d \n",vd.capability.channels);
printf("Audios : %d \n",vd.capability.audios);
printf("picture size:%d\n",vd.mbuf.size);
while(1)
{
imageptr=get_frame_address(&vd);
save_picture(imageptr,"haha.jpg");
if(get_next_frame(&vd)!=0)
{
goto err;
}
}
err:
if(vd.fd)
close(vd.fd);
exit(0);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
回复 1# daisyzone
请问你这是v4l2下的视频驱动吗?
回复 2# mch_kot
V4L的
V4L2不是已经有支持中星微的301了吗?
回复 4# rodgerluo
是支持呀,这个不是驱动程序,是图像采集程序!
有可能是驱动的问题,可以这样调试,先写一个驱动的测试程序,保证应用层可以从驱动正确的拿到数据,另外中星微的摄像头类型繁多,未必你所用的摄像头和用的驱动是匹配的。
总结一下就是需要您先验证一下驱动是否正常工作。
————————————————————————
支持一下!
我最近也遇到这个问题
回复 1# daisyzone
谢谢楼主,问题现在解决了吗