📄 ci-capture.c
字号:
/* * stillyuv.c * * Copyright (C) 2003, Intel Corporation * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * This is a sample code of camera application. * */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <linux/fb.h>#include <sys/mman.h>#include <errno.h>#include <linux/videodev.h>#include <linux/pxa_camera.h>#include <getopt.h>#include <sys/ioctl.h>#include <fcntl.h>#include <unistd.h>#include <linux/types.h>#include <sys/time.h>/* * [in] dev, bpp, format, xpos, ypos, xres, yres * [out] map, yoff, ylen, cboff, cblen, croff, crlen , pitch * * format: 0x2 = YCbCr 444 Planar, 0x3 = YCbCr 422 Planar, 0x4 = YCbCr 420 Planar * pitch : Actual pixels per line. * * if successed, file descriptor returned, * otherwise, -1 returned. */int overlay2_open(char *dev, int bpp, int format, int xpos, int ypos, int xres, int yres, unsigned char **map, int *yoff, int *ylen, int *cboff, int *cblen, int *croff, int *crlen, int *pitch){ struct fb_var_screeninfo var; struct fb_fix_screeninfo fix; int fd,err; if ( (!map) || (!yoff) || (!ylen) || (!cboff) || (!cblen) || (!croff) || (!crlen) ) return -1; fd = open(dev, O_RDWR); if (fd < 0) return -1; var.xres = xres; var.yres = yres; var.bits_per_pixel = bpp; /* nonstd for FOR/XPOS/YPOS */ var.nonstd = (format <<20) | (ypos << 10) | xpos; /* set "var" screeninfo */ err = ioctl(fd, FBIOPUT_VSCREENINFO, &var); if (err) { close(fd); return -1; } /* get updated "fix" screeninfo */ err = ioctl(fd, FBIOGET_FSCREENINFO, &fix); if (err) { close(fd); return -1; } /* get updated "var" screeninfo */ err = ioctl(fd, FBIOGET_VSCREENINFO, &var); if (err) { close(fd); return -1; } *map = (unsigned char*)mmap(0, fix.smem_len, PROT_READ | PROT_WRITE , MAP_SHARED, fd, 0); if(*map == MAP_FAILED) return -1; *yoff = var.red.offset; *ylen = var.red.length; *cboff = var.green.offset; *cblen = var.green.length; *croff = var.blue.offset; *crlen = var.blue.length; *pitch = fix.line_length; return fd;}/* * release overlay fd */int overlay2_close(int fd){ if (fd >= 0) close(fd); /* FIXME : we need mumap */ return 0;}#define DEFAULT_FB_DEVICE "/dev/fb2"void udelay(int usec){ fd_set rfds; struct timeval tv; FD_ZERO(&rfds); FD_SET(0, &rfds); tv.tv_sec = 0; tv.tv_usec = usec; select(1, &rfds, NULL, NULL, &tv);}#define FRAME_NUM 100int main(int argc, char *argv[]){ int fd; int frame; unsigned char *map; int ylen, yoff, cblen, cboff, crlen, croff, pitch; int dev; unsigned char* buff = NULL; struct video_window vw; struct video_mbuf vm; int frame_size; int arg; struct timeval start, end; struct timezone tz; struct ioctl_data { int val1; int val2; }; struct ioctl_data io_data;#ifdef DUMP_VIDEO FILE* ff; ff=fopen("cap.yuv422","wb");#endif /* ------------------- argument process ----------------------- */ if (argc < 2) { printf("Usage: %s win_width win_height \n", argv[0]); return -1; } vw.width = atoi(argv[1]); vw.height = atoi(argv[2]); /* ------------------- end of argument process ---------------- */ dev = open("/dev/video0", O_RDONLY); if (dev < 0) { printf("Error: cannot open /dev/video0.\n"); exit(1); } if (ioctl(dev, VIDIOCSWIN, &vw)) { printf("Error setting camera information.\n"); exit(2); } else { printf("Set size. width = %d, height = %d.\n",vw.width, vw.height); } if (ioctl(dev, VIDIOCGWIN, &vw)) { printf("Error reading camera information.\n"); exit(2); } else { printf("Get size. width = %d, height = %d.\n",vw.width, vw.height); } io_data.val1 = CAMERA_IMAGE_FORMAT_YCBCR422_PLANAR; io_data.val2 = CAMERA_IMAGE_FORMAT_YCBCR422_PLANAR; if (ioctl(dev, WCAM_VIDIOCSINFOR, &io_data)) { printf("Error setting camera format.\n"); exit(2); } fd = overlay2_open(DEFAULT_FB_DEVICE, 24, 0x3, 50, 100, vw.width, vw.height, &map, &yoff, &ylen, &cboff, &cblen, &croff, &crlen, &pitch); if ( fd < 0) { printf("Failed to open:%s\n", DEFAULT_FB_DEVICE); exit(-1); } if (ioctl (dev, VIDIOCGMBUF, &vm) == -1) { printf ("ioctl (VIDIOCGMBUF) failed\n"); exit(-1); } buff = (unsigned char*)malloc(vw.width * vw.height * 2); if (!buff) { printf("Cann't allocate buffer.\n"); exit(3); } memset(buff,0,vw.width*vw.height*2); memcpy(map+yoff, buff, vw.width*vw.height); memcpy(map+cboff, buff+vw.width*vw.height, vw.width*vw.height/2); memcpy(map+croff, buff+vw.width*vw.height+vw.width*vw.height/2, vw.width*vw.height/2);/* preview */ arg = VIDEO_START; ioctl(dev, VIDIOCCAPTURE, arg); frame_size = vw.width * vw.height * 2; frame = 0; while(1) { int len; len = read (dev, buff, frame_size); if (len <= 0 ) { printf("Capture error.\n"); exit(6); } else { // printf("Capture successfully.\n"); } memcpy(map+yoff, buff, vw.width*vw.height); memcpy(map+cboff, buff + vw.width*vw.height, vw.width*vw.height/2); memcpy(map+croff, buff + vw.width*vw.height+vw.width*vw.height/2, vw.width*vw.height/2); frame ++; if (frame == 100) break; } gettimeofday(&end, &tz); arg = VIDEO_STOP; ioctl(dev, VIDIOCCAPTURE, arg);/* Stop preview */ printf ("preview stop \n"); udelay (1000000); printf ("begin to capture \n"); arg = STILL_IMAGE; ioctl(dev, VIDIOCCAPTURE, arg); frame_size = vw.width * vw.height * 2; frame = 0; gettimeofday(&start, &tz); int len; len = read (dev, buff, frame_size);{/* pass the first few frames for just power on of the sensor */ int i; for (i = 0; i < 3; i ++) { ioctl(dev, VIDIOCCAPTURE, arg); len = read (dev, buff, frame_size);// printf ("capture one frame \n"); }} if (len <= 0 ) { printf("Capture error.\n"); exit(6); } else { printf("Capture successfully.\n"); frame++; } memcpy(map+yoff, buff, vw.width*vw.height); memcpy(map+cboff, buff + vw.width*vw.height, vw.width*vw.height/2); memcpy(map+croff, buff + vw.width*vw.height+vw.width*vw.height/2, vw.width*vw.height/2); #ifdef DUMP_VIDEO if(ff) { fwrite(buff,1,vw.width*vw.height*2,ff); }#endif #ifdef DUMP_VIDEO if(ff) fclose(ff);#endif free(buff); printf ("---------- Still Image Capture Completed, pls press any key to terminate this demo -------- \n"); getchar(); close(dev); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -