📄 docapture.c
字号:
/* IM8803 Camera Capture Program * HHTech * 2003.11 *///#define MYDEBUG#include "myhead.h"#include "img.h"#include <sys/time.h>/*****************Conditional Compile Macros*************//***************Application Macro Definitions********************/#define CAP_DELAY 50000#define VF_WIDTH 240#define VF_HEIGHT 320#define VF_BYTES_PER_PIXEL 2#define VF_IMAGE_SIZE VF_WIDTH*VF_HEIGHT*VF_BYTES_PER_PIXEL#define FB_DEVICE1 "/dev/fb0"#define IOCTL_CSI_INIT 1#define IOCTL_I2C_WRITE 2#define IOCTL_I2C_READ 3#define IOCTL_SUBSAMPLE 4#define IOCTL_SET_GAIN 5#define IOCTL_SET_SCREEN_WIDTH 6#define IOCTL_SET_INT_TIME 7#define IOCTL_DMA_CAPTURE 8#define IOCTL_STOP_CAPTURE 13#define IOCTL_INC_FRM 20 // increment frame rate meter#define IOCTL_QVGA 22#define IOCTL_VGA 23#define IOCTL_SXGA 24#define QUIT_CMD "quit"#define TYPE_CMD "type"#define CAPT_CMD "capt"static int fd_fb1;static U8 *fb1;static U8 vf_buff[VF_IMAGE_SIZE];static U8 fb_buff[VF_IMAGE_SIZE];static U8 filename[30];static int fd_csi2c;static U8 quit = 0;static unsigned int capframe = 0;FILE *bmpFile;/***************************Implementation **************************/void clearScreen(U8 * fb, U32 size){ U32 i; U8 *p = fb; for (i = 0; i < size; i++) *(p++) = 0;}void viewfinderCapture(){ int i; PLINE; if (!read(fd_csi2c, vf_buff, VF_IMAGE_SIZE)) { } else { PDEBUG("*** CSI data read failed ! ***\n"); return; } PLINE; return;}/* Display capture image int 240x320 LCD */void displayViewfinderImage(U8 * fb, U8 * display_buf, int screen_height,int screen_width, float screen_bytes_per_pixel){ int i,j; for(i=0;i<320;i++) for(j=0;j<240;j++) *((U16 *)fb_buff+240*i+j)=*((U16 *)display_buf+320*j+i); memcpy(fb, fb_buff, screen_height * screen_width * screen_bytes_per_pixel);}const char bmp_head[] = { 0x42,0x4d,0x42,0x58,0x02,0x00,0x00,0x00,0x00,0x00, 0x42,0x00,0x00,0x00,0x28,0x00,0x00,0x00,0x40,0x01, 0x00,0x00,0xf0,0x00,0x00,0x00,0x01,0x00,0x10,0x00, 0x03,0x00,0x00,0x00,0x00,0x58,0x02,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0xf8,0x00,0x00,0xe0,0x07, 0x00,0x00,0x1f,0x00,0x00,0x00};void writeImageToFile(){ capframe++; sprintf(filename,"/tmp/0%d.bmp",capframe); bmpFile=fopen(filename, "w+"); fwrite(bmp_head,1,66,bmpFile); fwrite(vf_buff,1,240*320*2,bmpFile); fclose(bmpFile);}static inline void print_fps(struct timeval *s, struct timeval *e){ unsigned long time; unsigned long sec; unsigned long usec; int fps = 0; sec = e->tv_sec - s->tv_sec; if (e->tv_usec > s->tv_usec) usec = e->tv_usec - s->tv_usec; else { usec = e->tv_usec + 1000000 - s->tv_usec; sec--; } time = sec * 1000 + (usec+1) / 1000; fps = 1000 / (time / 30); printf("%d fps\n", fps);}int main(int argc, char *argv[]){ int i; unsigned int frames=0; struct timeval start_tv, end_tv; struct timezone tz; printf("CSI test: version 0.2 compiled at " __DATE__ " / " __TIME__ "\n"); if ((fd_csi2c = open("/dev/csi2c", O_RDWR)) < 0) { printf("Device csi2c open error !\n"); exit(-1); }# if ((fd_fb1 = open(FB_DEVICE1, O_RDWR)) <= 0) { printf("Frame buffer %s open error!\n", FB_DEVICE1); close(fd_csi2c); exit(-1); } PDEBUG("Use %s as frame buffer device.\n", FB_DEVICE1); if ((fb1 = (U8 *) mmap(0, VF_IMAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb1, 0)) == 0) { printf("Error: failed to map framebuffer device %s.\n", FB_DEVICE1); close(fd_fb1); close(fd_csi2c); exit(-1); } printf("Frame buffer 1 mapping succeed.\n"); clearScreen(fb1, VF_IMAGE_SIZE); if(argc == 2) { ioctl(fd_csi2c, 18, atoi(argv[1])); } else ioctl(fd_csi2c, 18, 0); ioctl(fd_csi2c, IOCTL_DMA_CAPTURE, VF_IMAGE_SIZE); //start capturing gettimeofday(&start_tv, &tz); while ((!quit)) { viewfinderCapture(); displayViewfinderImage(fb1, vf_buff, VF_HEIGHT, VF_WIDTH,VF_BYTES_PER_PIXEL); frames ++;#if 0 if ((frames % 50) == 0) { gettimeofday(&end_tv, &tz); print_fps(&start_tv, &end_tv); gettimeofday(&start_tv, &tz); }#endif { struct timeval tv1; fd_set fds1; int fd; char cmd[256]; char cmdbuff[10]; char test[6]; fd=0; tv1.tv_sec=0; tv1.tv_usec=0; FD_ZERO(&fds1); FD_SET(fd,&fds1); select(fd+1,&fds1,NULL,NULL,&tv1); if(FD_ISSET(fd,&fds1)) { memset(cmd,0,sizeof(cmd)); read(fd,cmd,256); if(strncmp(cmd,QUIT_CMD,4)==0){ goto end; } else if(strncmp(cmd,CAPT_CMD,4)==0){ writeImageToFile(); } else if(strncmp(cmd,TYPE_CMD,4)==0){ strncpy(test,cmd,5); ioctl(fd_csi2c,18,atoi(&test[4])); } } } }end: close(fd_csi2c); printf("doCapture finished.\n"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -