#根据网上常见的一个测试程序修改而来 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. C++中的纯虚函数

    ---恢复内容开始--- 在C++中的一种函数申明被称之为:纯虚函数(pure virtual function).它的申明格式如下 class CShape { public: ; }; 在什么情况 ...

  2. String类型转Long类型需要注意的问题

    转自:https://blog.csdn.net/m819177045/article/details/52669785/

  3. HttpUtility.UrlEncode,Server.UrlEncode 的区别

    引用: 1.HttpUtility.UrlEncode,HttpUtility.UrlDecode是静态方法,而Server.UrlEncode,Server.UrlDecode是实例方法. 2.Se ...

  4. BZOJ3192: [JLOI2013]删除物品(splay)

    Description   箱子再分配问题需要解决如下问题:  (1)一共有N个物品,堆成M堆.  (2)所有物品都是一样的,但是它们有不同的优先级.  (3)你只能够移动某堆中位于顶端的物品.  ( ...

  5. Redis学习总结和相关资料

    因为别人都在用Redis,所以我不得不用Redis.  听起来感觉我很菜的样子,事实上和菜没有关系.  一是由于别人都用,作为后来者,没有"先发"优势,只能顺着别人的思路来做.当前 ...

  6. 代码生成器实现的Entity,Dao,Service,Controller,JSP神器(含代码附件)

    package com.flong.codegenerator; import java.sql.Connection; import java.sql.DatabaseMetaData; impor ...

  7. color-在framwork中添加属性变量

    1.今天在修改framwork中的代码的时候,需要把自己在代码中写的一个#ffffff,变成在xml中引用的变量.具体操作方法如下 1)在 frameworks/base/core/res/res/v ...

  8. 104.tcp多线程读写实现群聊

    客户端: #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include <w ...

  9. 15个常用的javaScript正则表达式--来自于javascript公众号

    摘要 收集整理了15个常用的javaScript正则表达式,其中包括用户名.密码强度.整数.数字.电子邮件地址(Email).手机号码.身份证号.URL地址. IPv4地址. 十六进制颜色. 日期. ...

  10. nodeJS+socket.io传递消息

    服务器端 安装express,socket.io npm install express --save-dev npm install socket.io --save app.js const ex ...