使用了是中星微的Z301B的USB摄像头,在天嵌的板子上运行的,在网络上搜了好久的资料,自己也做了好就,可是还是不行啊,我是没有ARM基础的,接触linux也就一两个星期多,现在真的是没有一点头绪了,记下来吧,计算下来以后慢慢品尝了~~
一下的程序可以实现图像的采集,可是只能采集一张图片,然后保存为JPG格式的,就算我选择的视频格式为UYVY格式,保存为YUV格式,但是最终文件内部格式还是JPG格式的,因为利用YUV解码器无法查看文件,将后缀改为JPG后可以用图像查看器查看··········现在我知道要在程序中提取出图像的RGB信息,然后进行转换,转换为YUV格式的,但是找了好久,都没有找到相关资料啊··········真的挺郁闷的 ╮(╯▽╰)╭
下面是提取一张图片的程序源代码,已经实现了,但是程序是别人的,我想改改,将图片保存为YUV格式的,或者视频也可以,但是真不知道如何下手了···经过了两个星期的爬滚,还是一无所获~~~~悲剧啊·························
/*arm-linux-gcc -o usbcamera usbcamera.c ; then download the file and put in the /sbin .*/ #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 CAPTURE_FILE "/media/frame.yuv" #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; static unsigned int n_buffers = 0; FILE *file_fd; static unsigned long file_length; static unsigned char *file_name; static int read_frame (void) { struct v4l2_buffer buf; unsigned int i; CLEAR (buf); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; int ff = ioctl (fd, VIDIOC_DQBUF, &buf); if(ff<0) printf("failture\n"); assert (buf.index < n_buffers); printf ("buf.index dq is %d,\n",buf.index); fwrite(buffers[buf.index].start, buffers[buf.index].length, 1, file_fd); /////////////////////// FILE *fp = fopen(CAPTURE_FILE, "w+"); if (fp < 0) { printf("open frame data file failed\n"); return -1; } fwrite(buffers[buf.index].start, 1, buffers[buf.index].length, fp);
fclose(fp); printf("Capture one frame saved in %s\n", CAPTURE_FILE); //////////////////// ff=ioctl (fd, VIDIOC_QBUF, &buf); if(ff<0) printf("failture VIDIOC_QBUF\n"); return 1; } int main (int argc,char ** argv) { struct v4l2_capability cap; struct v4l2_format fmt; unsigned int i; enum v4l2_buf_type type; file_fd = fopen("test-mmap.jpg", "w"); fd = open (dev_name, O_RDWR | O_NONBLOCK, 0); int ff=ioctl (fd, VIDIOC_QUERYCAP, &cap); if(ff<0) printf("failture VIDIOC_QUERYCAP\n"); // Print capability infomations printf("Capability Informations:\n"); printf(" driver: %s\n", cap.driver); printf(" card: %s\n", cap.card); printf(" bus_info: %s\n", cap.bus_info); printf(" version: %08X\n", cap.version); printf(" capabilities: %08X\n", cap.capabilities); struct v4l2_fmtdesc fmt1; int ret; memset(&fmt1, 0, sizeof(fmt1)); fmt1.index = 0; fmt1.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; while ((ret = ioctl(fd, VIDIOC_ENUM_FMT, &fmt1)) == 0) { fmt1.index++; printf("{ pixelformat = '%c%c%c%c', description = '%s' }\n",fmt1.pixelformat & 0xFF, (fmt1.pixelformat >> 8) & 0xFF,(fmt1.pixelformat >> 16) & 0xFF, (fmt1.pixelformat >> 24) & 0xFF,fmt1.description); } 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_UYVY;//V4L2_PIX_FMT_JPEG; fmt.fmt.pix.field = V4L2_FIELD_INTERLACED; ff = ioctl (fd, VIDIOC_S_FMT, &fmt); if(ff<0) printf("failture VIDIOC_S_FMT\n"); file_length = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height; struct v4l2_requestbuffers req; CLEAR (req); req.count = 4;//1 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; req.memory = V4L2_MEMORY_MMAP; ff = ioctl (fd, VIDIOC_REQBUFS, &req); if(ff<0) printf("failture VIDIOC_REQBUFS\n"); if (req.count < 1) printf("Insufficient buffer memory\n"); buffers = (struct buffer*)calloc (req.count, sizeof (*buffers)); for (n_buffers = 0; n_buffers < req.count; ++n_buffers) { struct v4l2_buffer buf; CLEAR (buf); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; buf.index = n_buffers; if (-1 == ioctl (fd, VIDIOC_QUERYBUF, &buf)) printf ("VIDIOC_QUERYBUF error\n"); buffers[n_buffers].length = buf.length; buffers[n_buffers].start = mmap (NULL ,buf.length,PROT_READ | PROT_WRITE ,MAP_SHARED ,fd, buf.m.offset); if (MAP_FAILED == buffers[n_buffers].start) printf ("mmap failed\n"); } for (i = 0; i < n_buffers; ++i) { struct v4l2_buffer buf; CLEAR (buf); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; buf.index = i; if (-1 == ioctl (fd, VIDIOC_QBUF, &buf)) printf ("VIDIOC_QBUF failed\n"); } type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (-1 == ioctl (fd, VIDIOC_STREAMON, &type)) printf ("VIDIOC_STREAMON failed\n"); for (;;) { fd_set fds; struct timeval tv; int r; FD_ZERO (&fds); FD_SET (fd, &fds); tv.tv_sec = 2; tv.tv_usec = 0; r = select (fd + 1, &fds, NULL, NULL, &tv); if (-1 == r) { if (EINTR == errno) continue; printf ("select err\n"); } if (0 == r) { fprintf (stderr, "select timeout\n"); exit (EXIT_FAILURE); } if (read_frame ()) break; } unmap: for (i = 0; i < n_buffers; ++i) if (-1 == munmap (buffers->start, buffers->length)) printf ("munmap error"); type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (-1 == ioctl(fd, VIDIOC_STREAMOFF, &type)) printf("VIDIOC_STREAMOFF"); close (fd); fclose (file_fd); exit (EXIT_SUCCESS); return 0; }
相关推荐
linux下基于V4L2/Qt的usb摄像头采集显示程序 Qt工程, 测试使用的是Qt5.6.0 可以正常使用, 采集usb摄像头的图像并显示, 可以正常配置摄像头像素
源码注释很多很清晰哦!!! 使用Qt和V4L2连接USB摄像头,采集图片的方式为V4L2_PIX_FMT_MJPEG,也可修改代码切换到V4L2_PIX_FMT_YUYV,带yuyv转rgb888函数。亲测可用哦。
使用原始的RGB数据构造Opencv中的Mat对象。该资源使用Linux系统中的V4L2接口读取摄像头MJPEG图像数据,解码成RGB数据,再转换为Opencv中的Mat对象所使用的BGR格式
在tda4vm evm上基于v4l2的usb摄像头图像采集的自定义kernel源码 使用ti自带工具生成kernel的源码 在kernel的源码中,将capture的各个函数分配到相应的模块
V4l2读取USB摄像头MJPG和yuyv数据,并实时显示摄像头的数据
因为RK3399pro+fadora系统的/dev/video0口被占用,使用python3+opencv无法读取摄像头数据,所以使用过 v4l2调用USB摄像头
该文件是基于v4l2接口编程的从USB摄像头采集图像信息,并在LCD实时显示的代码,里面是纯c写的,由Makefile编译
linux下基于QT和v4l2驱动的USB摄像头视频采集与显示例子,请参看其中README文件,本例子在ubuntu12.04LTS下编译通过,采用的通用的免驱摄像头,可以方便的移植到嵌入式linux中
linux 下应用SDL1.2和V4L2 获取双目摄像头数据并一起显示
v4l2基本框架,以及将linux下摄像头采集数据YUYV转换为JPEG格式
Linux下通过v4l2技术采集USB摄像头的视频,然后通过x264编码器编码成视频储存起来
通过熟悉V4L2的编程框架,应用在具体的摄像头图像采集程序中,采用MMAP方式,读取一帧图像并保存为jpg格式图片,对用V4L2API开发视频设备的读者有参考作用。
采用v4l2+usb摄像头 进行视频采集
Native层打开 V4L2 video0摄像头-V4L2_MEMORY_USERPTR-YUYV格式-1plane.cpp 对就文章:《Native层打开 V4L2 video0摄像头-V4L2_MEMORY_USERPTR-YUYV格式-1plane》 ...
使用video4linux2编程接口,获得笔记本摄像头影像后,用SDL显示在X Window下。 关键点有二:其一,从video4linux2的编程接口笔者了解到其笔记本摄像头支持YUYV视频帧格式,显示在 SDL上需要将YUYV格式转换成SDL支持...
利用V4L2格式获取Linux摄像头数据,自己写的,肯定是可以运行的。