Tegra(ubuntu)调用摄像头V4L2,并用YUV转RGB

首先检查摄像头是否可用,按步骤装好luvcview并测试

地址:http://developer.t-firefly.com/thread-957-1-1.html

一般的摄像头支持JPEG和YUV,有专门函数测试是否支持RGB的。如果支持就能省去YUV到RGB的步骤了。

然后新建capture_mmap.c

#include "captrue_mmap.h"
 
static int WIDTH = 640;
static int HEIGHT = 360;
 
static   void  open_device( void ) {
 
    struct  stat st;
    if  (-1 == stat(dev_name, &st)) {
 
        fprintf(stderr, "Cannot identify '%s': %d, %s\n" , dev_name, errno,
                strerror(errno));
        exit(EXIT_FAILURE);
    }
    if  (!S_ISCHR(st.st_mode)) {
 
        fprintf(stderr, "%s is no device\n" , dev_name);
        exit(EXIT_FAILURE);
    }
    fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
    if  (-1 == fd) {
 
        fprintf(stderr, "Cannot open '%s': %d, %s\n" , dev_name, errno,
                strerror(errno));
        exit(EXIT_FAILURE);
    }
}
 
static   void  init_device( void ) {
 
    struct  v4l2_capability cap;
    struct  v4l2_cropcap cropcap;
    struct  v4l2_crop crop;
    struct  v4l2_format fmt;
    unsigned int  min;
    if  (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap))   {
 
        if  (EINVAL == errno)
       {
 
            fprintf(stderr, "%s is no V4L2 device\n" , dev_name);
            exit(EXIT_FAILURE);
        }
        else
       {
 
            errno_exit("VIDIOC_QUERYCAP" );
        }
    }
    if  (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
   {
 
        fprintf(stderr, "%s is no video capture device\n" , dev_name);
        exit(EXIT_FAILURE);
    }
 
    if  (!(cap.capabilities & V4L2_CAP_STREAMING))
   {
 
            fprintf(stderr, "%s does not support streaming i/o\n" , dev_name);
            exit(EXIT_FAILURE);
    }
 
    //not all capture support crop!!!!!!!
    /* Select video input, video standard and tune here. */
    printf("-#-#-#-#-#-#-#-#-#-#-#-#-#-\n" );
    CLEAR (cropcap);
    cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    if  (0 == xioctl(fd, VIDIOC_CROPCAP, &cropcap)) {
 
        crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
#ifndef CROP_BY_JACK
        crop.c = cropcap.defrect; /* reset to default */
#else
        crop.c.left = cropcap.defrect.left;
        crop.c.top = cropcap.defrect.top;
        crop.c.width = 480;
        crop.c.height = 320;
#endif
        printf("----->has ability to crop!!\n" );
        printf("cropcap.defrect = (%d, %d, %d, %d)\n" , cropcap.defrect.left,
                cropcap.defrect.top, cropcap.defrect.width,
                cropcap.defrect.height);
        if  (-1 == xioctl(fd, VIDIOC_S_CROP, &crop)) {
 
            switch  (errno) {
 
            case  EINVAL:
                /* Cropping not supported. */
                break ;
            default :
                /* Errors ignored. */
                break ;
            }
            printf("-----!!but crop to (%d, %d, %d, %d) Failed!!\n" ,
                    crop.c.left, crop.c.top, crop.c.width, crop.c.height);
        } else  {
 
            printf("----->sussess crop to (%d, %d, %d, %d)\n" , crop.c.left,
                    crop.c.top, crop.c.width, crop.c.height);
        }
    } else  {
 
        /* Errors ignored. */
        printf("!! has 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值