⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testcamera.c

📁 linux下cmos摄像头驱动模块
💻 C
📖 第 1 页 / 共 2 页
字号:
}#ifdef	YCbCr_TO_RGB_TEST#include "422jpeg.h"static void test_ycbcr_to_rgb(void *scr){	__u8 *buffer_y, *buffer_cb, *buffer_cr;		buffer_y  = (__u8 *)c422jpeg;	buffer_cb = buffer_y  + 240 * 320;	buffer_cr = buffer_cb + 240 * 320 / 2;		image_width = 240;	image_height = 320;		printf("Test YCbCr422 to RGB...\n");	show_cam_img(scr, buffer_y, buffer_cb, buffer_cr);	printf("Press enter key to continue...\n");	getchar();		image_width = 0;	image_height = 0;}#elsestatic void test_ycbcr_to_rgb(void *scr) {}#endif/*************************************************************/static void usage(void){	int i;	printf("testcamera [-x/y/p/m/o/f/h options]\n");	printf("-w width   (set output image width, must be 4 alignment)\n");	printf("-h height  (set output image height, must be 4 alignment)\n");	printf("-p palette (set output image color space)\n");	printf("	optional image:\n");	for(i=0; i<MAX_IMAGE_FORMAT; i++)		printf("	[%2d] : %s\n", i, optional_image_format[i].name);	printf("-m         (use mmap method for camera)\n");	printf("-o number  (set save jpeg file optimization, must be 0~100)\n");	printf("-f name    (set save jpeg file prefix name)\n");	printf("-h show help\n");}static int getoptions(int argc, char *argv[]){	int ret = 0;	char c;	opterr = 0;	//don't print error message	while ((c = getopt (argc, argv, "x:y:p:o:f:u:mvh")) != (char)EOF)	/* Scan the command line for options */	switch (c) {	case 'x':		image_width = atoi(optarg);		if((image_width%4)||(image_width>FIXED_SOURCE_WIDTH))			ret = -1;		break;	case 'y':		image_height = atoi(optarg);		if((image_height%4)||(image_height>FIXED_SOURCE_HEIGHT))			ret = -1;		break;	case 'p':		image_format = atoi(optarg);		if(image_format>=MAX_IMAGE_FORMAT)			ret = -1;		break;	case 'o':		optimization = atoi(optarg);		if(optimization>100)			ret = -1;		break;	case 'f':		image_file = optarg;		break;	case 'm':		mmap_camera = 1;		break;	case 'h':		//ret = -1;		//break;	default:		ret = -1;		break;	}	return ret;}/*************************************************************/int main(int argc, char *argv[]){	int i, fd, fbfd;	struct fb_var_screeninfo vinfo;	struct fb_fix_screeninfo finfo;	__u8 *fb_buf;	__u32 screensize;	struct video_capability vc;	struct video_window vw;	struct video_capture vcp;	struct video_picture vp;	struct video_mbuf vm;	__u8 *buf = NULL;	int show, cnt;	fd_set rfds;	struct timeval tv;	struct termios tio;	int c_lflag;	fbfd = open("/dev/fb0", O_RDWR);	if (fbfd < 0) {		fbfd = open("/dev/fb/0", O_RDWR);		if(fbfd<0) {			printf("Error: cannot open framebuffer device.\n");		        return -1;		}	}	// Get fixed screen information	if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {				printf("Error reading fixed information.\n");		close(fbfd);        	return -1;	}	// Get variable screen information	if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {		printf("Error reading variable information.\n");		close(fbfd);		return -1;	}	printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel );	fb_xres = vinfo.xres;	fb_yres = vinfo.yres;	fb_bpp  = vinfo.bits_per_pixel;	screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;	fb_buf = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,				fbfd, 0);	if ((int)fb_buf == -1) {		printf("Error: failed to map framebuffer device to memory.\n");		close(fbfd);        	return -1;	}	test_ycbcr_to_rgb(fb_buf);	if(getoptions(argc, argv)<0) {		close(fbfd);		usage();		return -1;	}	fd = open("/dev/v4l/video0", O_RDONLY);	//rd&wr	if(fd<0) {		fprintf(stderr, "Open camera fail!\n");		close(fbfd);		return -1;	} else		fprintf(stdout, "Open camera success\n");	if(ioctl(fd, VIDIOCGCAP, &vc)<0)		printf("VIDIOCGCAP fail\n");	else		printf("max width %d, height %d\nmin width %d, height %d\n",			vc.maxwidth, vc.maxheight, vc.minwidth, vc.minheight);	vw.width  = FIXED_SOURCE_WIDTH;		//fixed in this application	vw.height = FIXED_SOURCE_HEIGHT;	//fixed in this application	if(ioctl(fd, VIDIOCSWIN, &vw)<0)		printf("VIDIOCSWIN fail\n");	if(ioctl(fd, VIDIOCGWIN, &vw)<0)		printf("VIDIOCGWIN fail\n");	else		printf("current width %d, height %d\n",	vw.width, vw.height);	if(!image_width||!image_height) {		image_width  = fb_xres;//vw.width;		image_height = fb_yres;//vw.height;	}	vcp.width  = image_width;	vcp.height = image_height;	if(ioctl(fd, VIDIOCSCAPTURE, &vcp)<0)		printf("VIDIOCSCAPTURE fail\n");	else {		if(ioctl(fd, VIDIOCGCAPTURE, &vcp)<0)			printf("VIDIOCGCAPTURE fail\n");		else			printf("capture width %d, height %d\n", vcp.width, vcp.height);	}	vp.palette = optional_image_format[image_format].palette;	if(ioctl(fd, VIDIOCSPICT, &vp)<0)		printf("VIDIOCSPICT fail\n");	if(ioctl(fd, VIDIOCGPICT, &vp)<0)		printf("VIDIOCGPICT fail\n");	else		printf("current palette %d\n",	vp.palette);	if(mmap_camera) {		if(ioctl(fd, VIDIOCGMBUF, &vm)<0) {			printf("VIDIOCGMBUF fail\n");			mmap_camera = 0;		} else {			printf("current camera buffer size %d, total frames %d\n",				vm.size, vm.frames);					buf = (__u8 *)mmap(0, vm.size, PROT_READ, MAP_SHARED, fd, 0);			if((int)buf==-1) {				printf("mmap camera fail!\n");				mmap_camera = 0;			} else				puts("mmap camera ok.\n");		}	}	if(!mmap_camera) {		puts("allocate memory for camera buffer.\n");		buf = malloc(image_width*image_height*2);		if(!buf) {			printf("fail to allocate memory for camera!\n");			close(fd);			munmap(fb_buf, screensize);			close(fbfd);			return -1;		}	}	printf("buffer at 0x%08x\n", (int)buf);	if(tcgetattr(0, &tio)<0) {		printf("tcgetattr fail!\n");		goto fail;	} else {		c_lflag = tio.c_lflag;		tio.c_lflag &= ~(ICANON | ECHO);		if(tcsetattr(0, TCSAFLUSH, &tio)<0) {			printf("tcsetattr fail!\n");			goto fail;		}	}		printf("now start capture...\n");	printf("press Esc key to exit, 'c' to save picture, 'd' to enable/disable display\n");	if(ioctl(fd, VIDIOCCAPTURE, VIDEO_START)<0) {		printf("VIDIOCCAPTURE fail\n");		goto fail;	}		FD_ZERO(&rfds);		show = 1;	cnt  = 0;	while(1) {		//FD_ZERO(&rfds);		FD_SET(0, &rfds);		FD_SET(fd, &rfds);		tv.tv_sec = 3;		tv.tv_usec = 0;		select(fd+1, &rfds, NULL,  NULL, &tv);		if(FD_ISSET(0, &rfds)) {			char cmd;			//printf("stdin input\n");			if(read(0, &cmd, 1)<0) {				printf("read stdin fail!\n");				break;			}			if(cmd=='d'||cmd=='D') {				show = !show;				printf("%s display\n", show?"enable":"disable");			} else if(cmd=='c'||cmd=='C') {				printf("save picture...\n");				save_picture(buf);			} else if(cmd==0x1b)				break;					} else if(FD_ISSET(fd, &rfds)) {			if(mmap_camera) {				//use read to clear ready flag in driver				i = read(fd, buf, 0);	//mmap camera for read, so read 0 byte to buf				if(i<0) {					printf("read fail!%d\n", i);					break;				}				//printf("read %d bytes\n", i);			} else {								i = read(fd, buf, image_width*image_height*2);				if(i<0) {					fprintf(stderr, "read fail! %d\n", i);					break;				}				//printf("read %d bytes\n", i);				cnt++;			}		} else {			printf("wait timeout, break...\n");			break;		}		if(show)			show_cam_img(fb_buf, buf,					buf+image_width*image_height,					buf+(image_width*image_height*3)/2);	}	tio.c_lflag = c_lflag;	tcsetattr(0, TCSAFLUSH, &tio);fail:	if(mmap_camera)		munmap(buf, vm.size);	else		free(buf);	close(fd);	munmap(fb_buf, screensize);	close(fbfd);	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -