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

📄 camera.cpp

📁 linux和arm下usb摄像头图像的获取
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "camera.h"

//Construct a camera, get init camera parameter
Camera::Camera(char *deviceIn, int widthIn, int heightIn, int formatIn, int grabmethodIn)
{
	memset (&videoIn, 0, sizeof (struct vdIn));
	memset(videodevice, 0, sizeof(videodevice));
	
	if(NULL == deviceIn)
	{
		printf("No device specifyed, using default device:%s\n", defaultDev);
		memcpy(videodevice, defaultDev, strlen(defaultDev));		
	}
	else if(strlen(deviceIn) > sizeof(videodevice))
	{
		printf("The video device name is too long, using default device:%s\n", defaultDev);
		memcpy(videodevice, defaultDev, strlen(defaultDev));
	}
	else
	{
		memcpy(videodevice, deviceIn, strlen(deviceIn));
	}
		//for(;;);
	printf("videodevice=%s\n", videodevice);


	if(widthIn == 320 || widthIn || 640)
	{
		width = widthIn;
	}
	else
	{
		printf("With parameter error, support width is 320 and 640, Using default wotdh: 320\n");
		width = 320;
	}

	if(heightIn == 240 || heightIn == 480)
	{
		height =heightIn;
	}
	else
	{
		printf("Helght parameter error, support height is 240 and 480, Using default height: 240\n");
		height = 240;
	}

	if(formatIn != VIDEO_PALETTE_JPEG)
	{
		printf("Format error, suppot format is: VIDEO_PALETTE_JPEG=21, using default format: VIDEO_PALETTE_JPEG\n");
		format = VIDEO_PALETTE_JPEG;
	}
	else
	{
		format = formatIn;
	}
		

	if(grabmethodIn== methodRead || grabmethodIn== methodMmap)
	{
		grabmethod = grabmethodIn;
	}
	else
	{
		printf("Grab image method error, support method is methodRead =0 and methodMmap = 1, using default method: methodRead\n");
		grabmethod = methodRead;
	}

	initBlist();
}
//overload construct function for convenience 
/*Camera::Camera()
{	
	memcpy(videodevice, "/dev/v4l/video0", strlen("/dev/v4l/video0"));
	format = VIDEO_PALETTE_JPEG;
	grabmethod = methodRead;
	height = 240;
	width = 320;		
}*/
//destroy a camera, just close it
Camera::~Camera()
{
	close_v4l (&videoIn);
}

void Camera::initBlist()
{

	for(int i = 0; i < 21; i++)
	{
		memset(Blist[i].name, 0, sizeof(Blist[i].name));
	}
	
	memcpy(Blist[0].name, "SPCA505", strlen("SPCA505"));
	Blist[0].num = BRIDGE_SPCA505 ;
	
	memcpy(Blist[1].name, "SPCA506", strlen("SPCA506"));
	Blist[1].num = BRIDGE_SPCA506;
	
	memcpy(Blist[2].name, "SPCA501", strlen("SPCA501"));
	Blist[2].num = BRIDGE_SPCA501;
	
	memcpy(Blist[3].name, "SPCA508", strlen("SPCA508"));
	Blist[3].num = BRIDGE_SPCA508;
	
	memcpy(Blist[4].name, "SPCA504", strlen("SPCA504"));
	Blist[4].num = BRIDGE_SPCA504;
	
	memcpy(Blist[5].name, "SPCA500", strlen("SPCA500"));
	Blist[5].num = BRIDGE_SPCA500;
	memcpy(Blist[0].name, "SPCA504B", strlen("SPCA504B"));
	Blist[0].num = BRIDGE_SPCA504B;
	
	memcpy(Blist[6].name, "SPCA533", strlen("SPCA533"));
	Blist[6].num = BRIDGE_SPCA533;
	
	memcpy(Blist[7].name, "SPCA504C", strlen("SPCA504C"));
	Blist[7].num = BRIDGE_SPCA504C;
	
	memcpy(Blist[8].name, "SPCA561", strlen("SPCA561"));
	Blist[8].num = BRIDGE_SPCA561;
	
	memcpy(Blist[9].name, "SPCA536", strlen("SPCA536"));
	Blist[9].num = BRIDGE_SPCA536;
	
	memcpy(Blist[10].name, "SN9C102", strlen("SN9C102"));
	Blist[10].num = BRIDGE_SONIX;
	
	memcpy(Blist[11].name, "ZR364XX", strlen("ZR364XX"));
	Blist[11].num = BRIDGE_ZR364XX;
	
	memcpy(Blist[12].name, "ZC301-2", strlen("ZC301-2"));
	Blist[12].num = BRIDGE_ZC3XX;
	
	memcpy(Blist[13].name, "CX11646", strlen("CX11646"));
	Blist[13].num = BRIDGE_CX11646;
	
	memcpy(Blist[14].name, "TV8532", strlen("TV8532"));
	Blist[14].num = BRIDGE_TV8532;
	
	memcpy(Blist[15].name, "ET61XX51", strlen("ET61XX51"));
	Blist[15].num = BRIDGE_ETOMS;
	
	memcpy(Blist[16].name, "SN9CXXX", strlen("SN9CXXX"));
	Blist[16].num = BRIDGE_SN9CXXX;
	
	memcpy(Blist[17].name, "MR97311", strlen("MR97311"));
	Blist[17].num = BRIDGE_MR97311;
	
	memcpy(Blist[18].name, "UNKNOW", strlen("UNKNOW"));
	Blist[18].num = BRIDGE_UNKNOW;
	
	Blist[19].num = -1;
}
//Init the camera, and create a thread to grab image
void Camera::openCamera()
{
	pthread_attr_t attr;

	 pthread_attr_init(&attr); 
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 
	init_videoIn(&videoIn, videodevice, width, height, format,grabmethod);	
	pthread_create (&grabImgThrdId, NULL, grab, (void*)&videoIn);
	//pthread_create (&grabImgThrdId, &attr, Camera::grab, NULL);
	//pthread_create (&grabImgThrdId, NULL, test, (void*)&tmp);
	 signal(SIGPIPE, SIG_IGN);	/* Ignore sigpipe */

	sigchld_handler();
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = SA_RESTART;
}

void Camera::sigchld_handler()
{
	videoIn.signalquit = 0;
}

//close the cameara
void Camera::closeCamera()
{
	close_v4l (&videoIn);
}

int Camera::readFrame(char *buf)
{
	//int ret;
	int frameout = 1;
	//unsigned char wakeup = 0;
	struct frame_t *headerframe;
	 
	while ((frameout == videoIn.frame_cour) && videoIn.signalquit) 
	{
		printf("Image is not ready, waiting 1 ms\n");
	   	usleep(1000);		
	}	
   
	videoIn.framelock[frameout]++;
	headerframe = (struct frame_t *) videoIn.ptframe[frameout];	
	memcpy(buf, (unsigned char*)(videoIn.ptframe[frameout]+sizeof(struct frame_t)), headerframe->size);
	videoIn.framelock[frameout]--;
	frameout = (frameout+1)%4;     
	return headerframe->size;     
}

void Camera::upbright()
{
	unsigned short bright =0;
	
	bright = videoIn.videopict.brightness;
	
	if(( bright+0x200) < 0xffff)
	{
		bright += 0x200;
		videoIn.videopict.brightness = bright;
		if (SetVideoPict (&videoIn) < 0)
    		{
      			printf("Upbright bright failed\n");
		}
	}
}

void Camera::downbright()
{
	unsigned short bright =0;
	
	bright = videoIn.videopict.brightness;
	
	if(( bright-0x200) > 0)
	{
		bright -= 0x200;
		videoIn.videopict.brightness = bright;
		if (SetVideoPict (&videoIn) < 0)
    		{
      			printf("Down bright failed\n");
		}
	}
}

void Camera::upcontrast()
{
	unsigned short contrast =0;
	
	contrast = videoIn.videopict.contrast;
	
	if((contrast+0x200) < 0xffff)
	{
		contrast += 0x200;
		videoIn.videopict.contrast = contrast;
		
		if (SetVideoPict (&videoIn) < 0)
    		{
      			printf("Up contrast  failed\n");
    		}	
	}
}

void Camera::downcontrast()
{
	unsigned short contrast =0;
	
	contrast = videoIn.videopict.contrast;
	
	if((contrast-0x200) > 0)
	{
		contrast -= 0x200;
		videoIn.videopict.contrast = contrast;
		
		if (SetVideoPict (&videoIn) < 0)
    		{
      			printf("Down contrast  failed\n");
    		}	
	}
}


int Camera::init_videoIn (struct vdIn *vd, char *device, int width, int height, int format, int grabmethod)
{
	int err = -1;
	int i;
	
	if (vd == NULL || device == NULL)
		return -1;
	
	if (width == 0 || height == 0)
		return -1;
	
	if(grabmethod < 0 || grabmethod > 1)		
		grabmethod = 1; //read by default;
		
	// check format 
	vd->videodevice = NULL;
	vd->cameraname = NULL;
	vd->videodevice = NULL;
	vd->videodevice = (char *) realloc (vd->videodevice, 16);
	vd->cameraname = (char *) realloc (vd->cameraname, 32);
	snprintf (vd->videodevice, 12, "%s", device);
	
	if(debug) 
		printf("video %s \n",vd->videodevice);
	
	memset (vd->cameraname, 0, sizeof (vd->cameraname));
	memset(vd->bridge, 0, sizeof(vd->bridge));
	vd->signalquit = 1;
	vd->hdrwidth = width;
	vd->hdrheight = height;
  	/*compute the max frame size   */
	vd->formatIn = format; 
	vd->bppIn = GetDepth (vd->formatIn);
	vd->grabMethod = grabmethod;		//mmap or read 
	vd->pFramebuffer = NULL;
	/* init and check all setting */
	err = init_v4l (vd);
	/* allocate the 4 frames output buffer */
 
	for (i = 0; i < OUTFRMNUMB; i++)
	{
		vd->ptframe[i] = NULL;
		vd->ptframe[i] = (unsigned char *) realloc (vd->ptframe[i], sizeof(struct frame_t) + (size_t) vd->framesizeIn );
		vd->framelock[i] = 0;
	}
 
	vd->frame_cour = 0;  
	pthread_mutex_init (&vd->grabmutex, NULL);
	return err;
}

int Camera::GetDepth (int format)
{
	int depth;
	switch (format)
	{   
		case VIDEO_PALETTE_JPEG:
		{
			depth = 8;
			 break;
		}  
		
		case VIDEO_PALETTE_RAW:
		{
			depth = 8;
			 break;
		}
     
		case VIDEO_PALETTE_YUV420P:
		{
			depth = (8 * 3) >> 1;
			 break;
		}

		case VIDEO_PALETTE_RGB565:
      		{
			depth = 16;
			break;
		}
		
		case VIDEO_PALETTE_RGB24:
		{
			depth = 24;
			break;
		}
		
		case VIDEO_PALETTE_RGB32:
		{
			depth = 32;
			break;
		}
      
		default:
		{
			depth = -1;
			break;
		}
	}
	
	return depth;
}

int Camera::init_v4l (struct vdIn *vd)
{
	int f;
	int erreur = 0;
	
	if ((vd->fd = open (vd->videodevice, O_RDWR)) == -1)
		exit_fatal ("ERROR opening V4L interface");

	if (ioctl (vd->fd, VIDIOCGCAP, &(vd->videocap)) == -1)
		exit_fatal ("Couldn't get videodevice capability");

	if(debug) 
		printf ("Camera found: %s \n", vd->videocap.name);
		snprintf (vd->cameraname, 32, "%s", vd->videocap.name);

	erreur = GetVideoPict (vd);
	if (ioctl (vd->fd, VIDIOCGCHAN, &vd->videochan) == -1)
	{
		if(debug) printf ("Hmm did not support Video_channel\n");
			vd->cameratype = UNOW;
	}
	else
	{
		if (vd->videochan.name)
		{
			if(debug) printf ("Bridge found: %s \n", vd->videochan.name);
				snprintf (vd->bridge, 9, "%s", vd->videochan.name);
			vd->cameratype = GetStreamId (vd->videochan.name);
			spcaPrintParam (vd->fd,&vd->videoparam);
		}
		else
		{
			if(debug) 
				printf ("Bridge not found not a spca5xx Webcam Probing the hardware !!\n");
			vd->cameratype = UNOW;
		}
	}

	/* Only jpeg webcam allowed */
	if(vd->cameratype != JPEG)
	{
		exit_fatal ("Not a JPEG webcam sorry Abort !");
	}
	
	if(debug)
		printf ("StreamId: %d  Camera\n", vd->cameratype);
	
	vd->videopict.palette = vd->formatIn;
	vd->videopict.depth = GetDepth (vd->formatIn);
	vd->bppIn = GetDepth (vd->formatIn);
  
    
	vd->framesizeIn = (vd->hdrwidth * vd->hdrheight >> 2 );
	printf("vd->formatIn=%d\n", vd->formatIn);
	  printf("vd->videopict.depth=%d\n", vd->videopict.depth);
	    printf("vd->bppIn=%d\n", vd->bppIn);
		printf("vd->hdrwidth=%d, vd->hdrheight=%d, vd->framesizeIn=%d\n", vd->hdrwidth, vd->hdrheight,  vd->framesizeIn);
printf("vd->fd=%d\n", vd->fd);
	erreur = SetVideoPict (vd);
	erreur = GetVideoPict (vd);
	
	if (vd->formatIn != vd->videopict.palette || vd->bppIn != vd->videopict.depth)
		exit_fatal ("could't set video palette Abort !");
	
	if (erreur < 0)

⌨️ 快捷键说明

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