免费注册 查看新帖 |

Chinaunix

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

Linux摄像头编程小结 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-06-03 16:47 |只看该作者 |倒序浏览
这次是在linux下开发摄像头的程序,主要用的是video4linux来做的,界面用qt来实现,开始准备用frame
buffer来直接写屏但是效果不怎么好,后来就用qt来做了,这样用起来效果还蛮好的,帧率也可以,可以上到30fps;运用v4l来编程主要掌握其
api,要提高帧率最重要的是用到内存映射,其实用qt和frame
buffer的时候都要用到内存映射来做,只有这样才可以达到较高的帧率,不过要注意资源的利用问题.mmap后一定要munmap.对于frame
buffer是很有意思的一个东西,特别是驱动的设计.
代码如下:
1.open device:
      video_dev = open("\dev\video0",O_RDWR));
2.get the information of the video device
    struct video_capability video_cap;
    memset(&video_cap,0,sizeof(video_cap));
    if(ioctl(video_dev,VIDIOCGCAP,&video_cap) == -1)
    {
        perror("Cann't get the information of the video device");
        close(video_dev);
        exit(1);
    }
3.get the information of the channels
    struct video_channel video_chan;
    memset(&video_chan,0,sizeof(video_chan));
    for(channel = 0;channel < video_cap.channels;channel++)
    {
        video_chan.channel = channel;
        if(ioctl(video_dev,VIDIOCGCHAN,&video_chan) == -1)
        {
            perror("Cann't get the information of the channels");
            close(video_dev);
            exit(3);
        }
        if(video_chan.type == VIDEO_TYPE_TV)
        {
                  #ifdef DEBUG
            printf("NO.%d channel is %s,type is tv!\n",channel,video_chan.name);
                  #endif
        }
        if(video_chan.type == VIDEO_TYPE_CAMERA)
        {
            if(ioctl(video_dev,VIDIOCSCHAN,&video_chan) == -1)
            {
                perror("Cann't set the camera channel!");
                close(video_dev);
                exit(4);
            }
        }
    }
4.get the capture attributes
    struct video_picture video_pic;
    memset(&video_pic,0,sizeof(video_pic));
    if(ioctl(video_dev,VIDIOCGPICT,&video_pic) == -1)
    {
        perror("Cann't get the picture attributes of the device!");
        close(video_dev);
        exit(5);
    }
5.set capture attributes
      video_pic.palette = VIDEO_PALETTE_*****;//different camera has different palette
      video_pic.brightness = 65535;
    video_pic.colour = 0;
    if(ioctl(video_dev,VIDIOCSPICT,&video_pic) == -1)
    {
        perror("Cann't set the picture attributs!");
        close(video_dev);
        exit(6);
    }
6.mmap the device
    struct video_mbuf mbuf;
    unsigned char *frames;
    memset(&mbuf,0,sizeof(mbuf));
    if(ioctl(video_dev,VIDIOCGMBUF,&mbuf) == -1)   
    {
        perror("Cann't get the buffer information of the video device!");
        close(video_dev);
        exit(7);
    }
    frames = (unsigned char *)::mmap(0,mbuf.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_dev,0);
    if(!frames || frames == (unsigned char *)(long)(-1))
    {
        perror("The device cann't be memory mapped!");
        close(video_dev);
        exit(8);
    }
7.capture
            if(ioctl(video_dev,VIDIOCMCAPTURE,&mmap) == -1)
        {
            perror("Capture failed!");
            close(video_dev);
            exit(9);
        }
   
        else
        {
                 
//display or save the picture file
            }
               
               
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/7547/showart_122619.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP