+/**
+ * Attempts to get an image from a camera
+ * @param num - Camera id
+ * @param width - Width to force
+ * @param height - Height to force
+ * @param image - Pointer to CvMat* to set with result
+ * @returns true on success, false on error
+ */
+ bool Camera_GetImage(int num, int width, int height, IplImage ** frame)
+ {
+ Log(LOGDEBUG, "Called with arguments num=%d width=%d height=%d frame=%p", num,width,height, frame);
+ 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;
+
+ if( g_capture == NULL)
+ {
+ g_capture = cvCreateCameraCapture(num);
+ g_captureID = num;
+ }
+ else if( num != g_captureID)
+ {
+ cvReleaseCapture(&g_capture);
+ g_capture = cvCreateCameraCapture(num);
+ g_captureID = num;
+ }
+
+ if (g_capture != NULL)
+ {
+
+ cvSetCaptureProperty(g_capture, CV_CAP_PROP_FRAME_WIDTH, width);
+ cvSetCaptureProperty(g_capture, CV_CAP_PROP_FRAME_HEIGHT, height);
+
+ *frame = cvQueryFrame(g_capture);
+ result = (*frame != NULL);
+
+ //cvShowImage("display", *image);
+ //cvWaitKey(0);
+ //cvSaveImage("test.jpg",*image,0);
+
+ Log(LOGDEBUG, "At end of mutex");
+ }
+
+ pthread_mutex_unlock(&mutex); //Close the mutex
+
+ //NOTE: Never have a "return" statement before the mutex is unlocked; it causes deadlocks!
+ return result;