📄 cam2fb_file.c
字号:
/* * CAMIF test * */#include <stdio.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>#include <unistd.h>#include <ctype.h>#include <errno.h>#include <sys/mman.h>#include <sys/time.h>#include <sys/ioctl.h>#include <linux/videodev.h>#define CAM_WIDTH 240#define CAM_HEIGHT 240//#define CAM_WIDTH 640//#define CAM_HEIGHT 480#define CAM_TO_FILEstatic int cam_fp = -1;static char *cam_mmap = NULL;static inline int camera_init(void){ int dev_fp = -1; dev_fp = open("/dev/video0",O_RDWR); if (dev_fp < 0) { printf("open /dev/video0 error\n"); return -1; } printf("video device opened (%d)\n",dev_fp); return dev_fp;}int main(int argc, char *argv[]){ int frame = 0; int ret; int i; FILE *filedata; struct video_capture v_capture; struct video_mbuf v_mbuf; struct video_mmap v_mmap; struct video_capability cap; struct video_picture pict; // write to file filedata = fopen("still.YUV", "wb+"); if(!filedata){ printf("picmmap.yuv open error\n"); return 0; } printf("file.yuv opened \n"); if ((cam_fp = camera_init()) < 0) //open goto err;/// ret = ioctl(cam_fp,VIDIOCGCAP,&cap); if(1){ printf("cap:ch=%d,max=%d:%d,min=%d:%d\n", cap.channels, cap.maxwidth, cap.maxheight, cap.minwidth, cap.minheight); printf("cap:[%s]\n",cap.name); printf("cap:0x%X\n",cap.type); }/// ret = ioctl(cam_fp,VIDIOCGCAPTURE,&v_capture); if(1){ printf("capture:offset:%d:%d.\n",v_capture.x, v_capture.y); printf("w=%d,h=%d\n",v_capture.width, v_capture.height); printf("decimation=0x%x\n",v_capture.decimation); printf("flags=%d\n",v_capture.flags); } v_capture.x = 0; v_capture.y = 0; v_capture.width = CAM_WIDTH; v_capture.height = CAM_HEIGHT; v_capture.flags = VIDEO_CAPTURE_ODD; v_capture.decimation = VIDEO_CAPTURE_ODD; ret = ioctl(cam_fp,VIDIOCSCAPTURE,&v_capture); ret = ioctl(cam_fp,VIDIOCGCAPTURE,&v_capture); if(1){ printf("capture:offset:%d:%d.\n",v_capture.x, v_capture.y); printf("w=%d,h=%d\n",v_capture.width, v_capture.height); printf("decimation=0x%x\n",v_capture.decimation); printf("flags=%d\n",v_capture.flags); }/// ret = ioctl(cam_fp,VIDIOCGPICT,&pict); printf("pict:brightness=%d:hue=%d.\n",pict.brightness,pict.hue); printf(":color=%d,contrast=%d\n",pict.colour,pict.contrast); printf(":whiteness=%d,depth=%d\n",pict.whiteness,pict.depth); printf(":palette=0x%X\n",pict.palette); pict.palette = VIDEO_PALETTE_YUV420; ret = ioctl(cam_fp,VIDIOCSPICT,&pict); ret = ioctl(cam_fp,VIDIOCGPICT,&pict); printf("pict:brightness=%d:hue=%d.\n",pict.brightness,pict.hue); printf(":color=%d,contrast=%d\n",pict.colour,pict.contrast); printf(":whiteness=%d,depth=%d\n",pict.whiteness,pict.depth); printf(":palette=0x%X\n",pict.palette); // get mbuf attribute if ((ret = ioctl(cam_fp,VIDIOCGMBUF,&v_mbuf)) < 0) { printf("get mbuf attribute error, ret(%d)\n",ret); goto err; } printf("video_mbuf:size=%d frames=%d\n",v_mbuf.size,v_mbuf.frames);//mmap cam_mmap = (char*)mmap(0, v_mbuf.size, PROT_READ | PROT_WRITE, MAP_SHARED, cam_fp, 0); if(cam_mmap < 0){ printf("mmap() video error\n"); return -1; } i = 3; while(i){ //v_mmap.frame = 0; v_mmap.frame = frame; v_mmap.width = CAM_WIDTH; v_mmap.height = CAM_HEIGHT; v_mmap.format = VIDEO_PALETTE_YUV420; printf("i=%d\n",i); if ((ret =ioctl(cam_fp,VIDIOCMCAPTURE,&v_mmap)) < 0) { printf("get capture error, ret(%d)\n",ret); goto err; } while(ioctl(cam_fp, VIDIOCSYNC, &frame) < 0){ sleep(1); printf("sleep 1s\n"); } if(i == 1){ //fwrite(cam_mmap, 1, v_capture.width*v_capture.height*3/2, filedata); fwrite(cam_mmap, 1, 240*360, filedata); printf("a picture ok\n"); } frame = (frame == 0)?1:0; //next frame i--; } err: fclose(filedata); if(cam_mmap) munmap(cam_mmap, v_mbuf.size); if(cam_fp) close(cam_fp); printf("done.\n"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -