v4l2图片获取

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <getopt.h>          

#include <fcntl.h>           
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

#include <asm/types.h>       
#include <linux/videodev2.h>

#define CLEAR(x) memset (&(x), 0, sizeof (x))

struct buffer
{
 void * start;
        size_t length;
};

static char * dev_name= "/dev/video0";
static int fd= -1;
struct buffer * buffers= NULL;

FILE *file_fd;
static unsigned long file_length;
static unsigned char *file_name;

int main (int argc,char ** argv)
{
printf("1\n"); struct v4l2_capability cap;
printf("2\n"); struct v4l2_format fmt;
printf("3\n");
 file_fd = fopen("test.jpg", "w");

printf("4\n"); fd = open (dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
printf("fd: %d\n",fd);
 ioctl (fd, VIDIOC_QUERYCAP, &cap);
printf("6\n");
 CLEAR (fmt);
printf("7\n"); fmt.type= V4L2_BUF_TYPE_VIDEO_CAPTURE;
printf("8\n"); fmt.fmt.pix.width= 640;printf("9\n");
 fmt.fmt.pix.height= 480;
printf("10\n"); fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
printf("11\n"); fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
printf("12\n"); ioctl (fd, VIDIOC_S_FMT, &fmt);

printf("13\n"); file_length = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;printf("14\n");

 buffers = calloc (1, sizeof (*buffers));

printf("15\n");buffers[0].length = file_length;
printf("16\n"); buffers[0].start = malloc (file_length);

printf("17\n"); for (;;)
 {
printf("-1\n");     fd_set fds;
 printf("-2\n");    struct timeval tv;
 printf("-3\n");    int r;

 printf("-4\n");    FD_ZERO (&fds);
   printf("-5\n");  FD_SET (fd, &fds);

     /* Timeout. */
   printf("-6\n");  tv.tv_sec = 3;
   printf("-7\n");  tv.tv_usec = 0;

   printf("-8\n");  r = select (fd + 1, &fds, NULL, NULL, &tv);

   printf("-9\n");  if (-1 == r)
  {printf("-10\n");
       if (EINTR == errno)
        continue;printf("-11\n");
       printf ("select");
  }

     if (0 == r)
  {printf("-12\n");
       fprintf (stderr, "select timeout\n");
       exit (EXIT_FAILURE);
     }
printf("asdfasdfsad\n");     if (read (fd, buffers[0].start, buffers[0].length))
      break;
 printf("123214234123\n");  }

printf("18\n"); fwrite(buffers[0].start, buffers[0].length, 1, file_fd);
printf("19\n");
 free (buffers[0].start);
printf("20\n"); close (fd);printf("21\n");
 fclose (file_fd);
printf("22\n"); exit (EXIT_SUCCESS);
printf("19\n"); return 0;
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值