#根据网上常见的一个测试程序修改而来 by rockie cheng
#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)
{
struct v4l2_capability cap; 
struct v4l2_format fmt;

file_fd = fopen("test.jpg", "w");

fd = open (dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);

ioctl (fd, VIDIOC_QUERYCAP, &cap);

CLEAR (fmt);
fmt.type                = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width       = 640; 
fmt.fmt.pix.height      = 480;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
fmt.fmt.pix.field       = V4L2_FIELD_INTERLACED;
ioctl (fd, VIDIOC_S_FMT, &fmt);

file_length = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;

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

buffers[0].length = file_length;
buffers[0].start = malloc (file_length);

for (;;) 
{
   fd_set fds;
   struct timeval tv;
   int r;

FD_ZERO (&fds);
   FD_SET (fd, &fds);

/* Timeout. */
   tv.tv_sec = 3;
   tv.tv_usec = 0;

r = select (fd + 1, &fds, NULL, NULL, &tv);

if (-1 == r) {
    if (EINTR == errno)
     continue;
    printf ("select");
                        }

if (0 == r) {
    fprintf (stderr, "select timeout\n");
    exit (EXIT_FAILURE);
                        }

if (read (fd, buffers[0].start, buffers[0].length))
   break;
}

fwrite(buffers[0].start, buffers[0].length, 1, file_fd);

free (buffers[0].start);
close (fd);
fclose (file_fd);
exit (EXIT_SUCCESS);
return 0;
}

11、V4L2摄像头获取单幅图片测试程序的更多相关文章

  1. 10、V4L2摄像头获取单幅图片测试程序(MMAP模式)

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

  2. ios获取相册图片 压缩图片

    从摄像头/相册获取图片 刚刚在上面的知识中提到从摄像头/相册获取图片是面向终端用户的,由用户去浏览并选择图片为程序使用.在这里,我们需要过UIImagePickerController类来和用户交互. ...

  3. Linux 下V4l2摄像头采集图片,实现yuyv转RGB,RGB转BMP,RGB伸缩,jpeglib 库实现压缩RGB到内存中,JPEG经UDP发送功(转)

    ./configure CC=arm-linux-gnueabihf-gcc LD=arm-linux-gnueabihf-ld --host=arm-linux --prefix=/usr/loca ...

  4. 利用opencv从USB摄像头获取图片

    由于opencv自带的VideoCapture函数直接从usb摄像头获取视频数据,所以用这个来作为实时的图像来源用于实体检测识别是很方便的. 1. 安装opencv 安装的步骤可以按照之前这个文章操作 ...

  5. WebRTC从摄像头获取图片传入canvas

    WebRTC从摄像头获取图片传入canvas 前面我们已经能够利用WebRTC的功能,通过浏览器打开摄像头,并把预览的图像显示在video元素中. 接下来我们尝试从视频中截取某一帧,显示在界面上. h ...

  6. python从网络摄像头获取rstp视频流并截取图片保存

    import cv2 def get_img_from_camera_net(folder_path):     cap = cv2.VideoCapture("rtsp://admin:a ...

  7. C语言高级应用---操作linux下V4L2摄像头应用程序

    我们都知道,想要驱动linux下的摄像头,其实很简单,照着V4L2的手册一步步来写,很快就可以写出来,但是在写之前我们要注意改变系统的一些配置,使系统支持framebuffer,在dev下产生fb0这 ...

  8. C语言高级应用---操作linux下V4L2摄像头应用程序【转】

    转自:http://blog.csdn.net/morixinguan/article/details/51001713 版权声明:本文为博主原创文章,如有需要,请注明转载地址:http://blog ...

  9. PHP高效获取远程图片尺寸和大小(转)

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

随机推荐

  1. Stacked Autoencoders

    转自:http://www.cnblogs.com/tornadomeet/archive/2013/03/25/2980357.html 如果使用多层神经网络的话,那么将可以得到对输入更复杂的函数表 ...

  2. 2017国家集训队作业[agc008f]Black Radius

    2017国家集训队作业[agc008f]Black Radius 时隔4个月,经历了省赛打酱油和中考各种被吊打后,我终于回想起了我博客园的密码= = 题意: ​ 给你一棵树,树上有若干个关键点.选中某 ...

  3. js常用代码示例及解决跨域的几种方法

    1.阻止默认行为 // 原生js document.getElementById('btn').addEventListener('click', function (event) { event = ...

  4. VMware Vsphere 6.0安装部署 总体部署架构

    (一)总体部署架构 本教程用于学习目的,力求详尽的介绍安装部署过程和各组件之间的关系,部署过程从最简单的模型开始,系列文章按时间顺序依次展开,每篇介绍一个组件. 开始阶段,按照一台物理服务器,部署所有 ...

  5. terminfo 数据库?

    什么是 terminfo 数据库? UNIX 系统上的 terminfo 数据库用于定义终端和打印机的属性及功能,包括各设备(例如,终端和打印机)的行数和列数以及要发送至该设备的文本的属性.UNIX ...

  6. Python day2 知识回顾

    标准库一般放在lib,site-packages放自己下载的:起的py文件名称不要和模块名字相同:import sys#print(sys.path)#打印去哪里寻找这些模块的路径#print(sys ...

  7. 今日题解------uvalive 2689

    今天学到了代码以外的东西,就是你在vj上挂了content ,然后你想更新它,你就要刷新一下,不然你提交的那题可能提交到别的地方. 好了回到重点,本题的题意是: #include<bits/st ...

  8. PLSQL乱码&TNS-12557: protocol adapter not loadable解决

    PLSQL乱码&TNS-12557: protocol adapter not loadable解决 PLSQL乱码&TNS-12557: protocol adapter not l ...

  9. 每位iOS开发者不容错过的10大有用工具

    内容简单介绍 1.iOS简单介绍 2.iOS开发十大有用工具之开发环境 3.iOS开发十大有用工具之图标设计 4.iOS开发十大有用工具之原型设计 5.iOS开发十大有用工具之演示工具 6.iOS开发 ...

  10. VS:&quot;64位调试操作花费的时间比预期要长&quot;的一解决途径

    解决的方法之中的一个: 在命令提示符那里打入例如以下命令: netsh winsock reset catalog netsh int ip reset reset.log hit 重新启动电脑后,就 ...