#include <pthread.h>
CvCapture *capture;
+IplImage *frame;
int captureID = -1;
void Image_Handler(FCGIContext * context, char * params)
cvReleaseMat(&g_encoded);
cvReleaseMat(&g_src);
}
-
+
bool Camera_GetImage(int num, int width, int height, CvMat * image)
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // Need to use a mutex to ensure 2 captures are not open at once
pthread_mutex_lock(&mutex);
bool result = false;
- capture = cvCreateCameraCapture(num);
+ if( capture == NULL)
+ {
+ capture = cvCreateCameraCapture(num);
+ captureID = num;
+ }
+ else if( num != captureID)
+ {
+ cvReleaseCapture(&capture);
+ capture = cvCreateCameraCapture(num);
+ captureID = num;
+ }
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, width);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, height);
- IplImage * frame = cvQueryFrame(capture);
+ frame = cvQueryFrame(capture);
if( frame == NULL)
return result;
if( image == NULL)
return result;
+ pthread_mutex_unlock(&mutex); //Close the mutex
+ return true;
+}
+
+void Image_Cleanup()
+{
// Release the capture and IplImage pointers
cvReleaseImageHeader(&frame);
cvReleaseCapture(&capture);
-
- pthread_mutex_unlock(&mutex); //Close the mutex
- return true;
}