由于毕设中用到了摄像头拍照功能,然后网上查了一下,最后自己稍微修改,以当前时间保存为.jpeg格式的图片,最后稍微添加了自己的一些小理解,与大家分享一下。
-----------------------------------------------------------------------------------------------------------------------------------
用的摄像头是ZC301P,已经加载进内核,输出格式是JPEG,大小是640*480,所以后面的图片大小计算的时候是640*480*3。
cam.c:
#include <fcntl.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <linux/videodev2.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
int main(){
//add by liuzj
time_t t;
struct tm *tmp;
t = time(NULL);
tmp = localtime(&t);
char buffer1[30];
char path[30];
/end
int fd = open("/dev/video0",O_RDWR);
printf("lzj------->>>fd is %d\n",fd);
//
struct v4l2_capability cap;
ioctl(fd,VIDIOC_QUERYCAP,&cap);
printf("lzj---------->>>>>Driver Name:%s\nCard Name:%s\nBus info:%s\n",cap.driver,cap.card,cap.bus_info);
//
struct v4l2_fmtdesc fmtdesc;
fmtdesc.index = 0;
fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
while(ioctl(fd,VIDIOC_ENUM_FMT,&fmtdesc) != -1){
printf("lzj-------->>>>>fmtdesc.description is %s\n",fmtdesc.description);
fmtdesc.index ++;
}
//
struct v4l2_format fmt;
fmt.type = V4L2_BUF_TYPE_VIDEO