8 static CvCapture * g_capture = NULL;
9 static int g_captureID = -1;
11 void Image_Handler(FCGIContext * context, char * params)
13 int num = 0, width = 1600, height = 1200; // Set Default values
15 {"num", &num, FCGI_INT_T},
16 {"width", &width, FCGI_INT_T},
17 {"height", &height, FCGI_INT_T}
19 if (!FCGI_ParseRequest(context, params, val, 3)) // Populate val
21 // Ensure the camera id is 0 or 1. Even though we plan to only have 1 camera attached at a time, this will allow 2. increase
22 else if (num < 0 || num > 1) {
23 FCGI_RejectJSON(context, "Invalid capture number");
25 // Ensure valid widths
26 } else if (width <= 0 || height <= 0) {
27 FCGI_RejectJSON(context, "Invalid width/height");
31 IplImage * src = NULL; // Source Image
32 CvMat * encoded = NULL; // Encoded Image
34 Camera_GetImage( num, width, height ,&src);
36 Log(LOGDEBUG, "About to encode");
37 encoded = cvEncodeImage(".jpg",src,0);
38 Log(LOGDEBUG, "Encoded");
40 Log(LOGNOTE, "Sending image!");
41 FCGI_PrintRaw("Content-type: image/jpg\r\n");
42 FCGI_PrintRaw("Cache-Control: no-cache, no-store, must-revalidate\r\n\r\n");
43 //FCGI_PrintRaw("Content-Length: %d", g_encoded->rows*g_encoded->cols);
44 FCGI_WriteBinary(encoded->data.ptr,1,encoded->rows*encoded->cols);
46 cvReleaseMat(&encoded);
47 cvReleaseImageHeader(&src);
51 * Attempts to get an image from a camera
52 * @param num - Camera id
53 * @param width - Width to force
54 * @param height - Height to force
55 * @param image - Pointer to CvMat* to set with result
56 * @returns true on success, false on error
58 bool Camera_GetImage(int num, int width, int height, IplImage ** frame)
60 Log(LOGDEBUG, "Called with arguments num=%d width=%d height=%d frame=%p", num,width,height, frame);
61 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // Need to use a mutex to ensure 2 captures are not open at once
62 pthread_mutex_lock(&mutex);
65 if( g_capture == NULL)
67 g_capture = cvCreateCameraCapture(num);
70 else if( num != g_captureID)
72 cvReleaseCapture(&g_capture);
73 g_capture = cvCreateCameraCapture(num);
77 //cvSetCaptureProperty(g_capture, CV_CAP_PROP_FRAME_WIDTH, width);
78 //cvSetCaptureProperty(g_capture, CV_CAP_PROP_FRAME_HEIGHT, height);
80 *frame = cvQueryFrame(g_capture);
81 result = (*frame != NULL);
83 //cvShowImage("display", *image);
85 //cvSaveImage("test.jpg",*image,0);
87 Log(LOGDEBUG, "At end of mutex");
89 pthread_mutex_unlock(&mutex); //Close the mutex
91 //NOTE: Never have a "return" statement before the mutex is unlocked; it causes deadlocks!
97 // Release the capture and IplImage pointers
98 //cvReleaseImageHeader(&g_frame);
99 cvReleaseCapture(&g_capture);