免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4382 | 回复: 8
打印 上一主题 下一主题

[应用] 利用摄像头进行图片采集,出不来图片 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-05-20 21:16 |只看该作者 |倒序浏览
用中星微芯片的摄像头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;
}

论坛徽章:
0
2 [报告]
发表于 2011-05-21 17:29 |只看该作者
回复 1# daisyzone


    请问你这是v4l2下的视频驱动吗?

论坛徽章:
0
3 [报告]
发表于 2011-05-21 21:10 |只看该作者
回复 2# mch_kot


    V4L的

论坛徽章:
0
4 [报告]
发表于 2011-05-21 21:33 |只看该作者
V4L2不是已经有支持中星微的301了吗?

论坛徽章:
0
5 [报告]
发表于 2011-05-22 16:15 |只看该作者
回复 4# rodgerluo


    是支持呀,这个不是驱动程序,是图像采集程序!

论坛徽章:
0
6 [报告]
发表于 2011-05-25 09:51 |只看该作者
有可能是驱动的问题,可以这样调试,先写一个驱动的测试程序,保证应用层可以从驱动正确的拿到数据,另外中星微的摄像头类型繁多,未必你所用的摄像头和用的驱动是匹配的。

总结一下就是需要您先验证一下驱动是否正常工作。

————————————————————————

论坛徽章:
0
7 [报告]
发表于 2013-05-24 16:47 |只看该作者
支持一下!

论坛徽章:
0
8 [报告]
发表于 2013-07-16 13:35 |只看该作者
我最近也遇到这个问题

论坛徽章:
0
9 [报告]
发表于 2013-08-26 22:17 |只看该作者
回复 1# daisyzone
谢谢楼主,问题现在解决了吗


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP