首先检查摄像头是否可用,按步骤装好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