12 void Image_Handler(FCGIContext * context, char * params)
14 int num = 0, width = 1600, height = 1200; // Set Default values
16 {"num", &num, FCGI_INT_T},
17 {"width", &width, FCGI_INT_T},
18 {"height", &height, FCGI_INT_T}
20 if (!FCGI_ParseRequest(context, params, val, 3)) // Populate val
22 // 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
23 else if (num < 0 || num > 1) {
24 FCGI_RejectJSON(context, "Invalid capture number");
26 // Ensure valid widths
27 } else if (width <= 0 || height <= 0) {
28 FCGI_RejectJSON(context, "Invalid width/height");
32 CvMat * g_src = NULL; // Source Image
33 CvMat * g_encoded; // Encoded Image
35 Camera_GetImage( num, width, height ,g_src);
36 g_encoded = cvEncodeImage("test_encode.jpg",g_src,0);
38 Log(LOGNOTE, "Sending image!");
39 FCGI_PrintRaw("Content-type: image/jpg\r\n");
40 FCGI_PrintRaw("Cache-Control: no-cache, no-store, must-revalidate\r\n\r\n");
41 //FCGI_PrintRaw("Content-Length: %d", g_encoded->rows*g_encoded->cols);
42 FCGI_WriteBinary(g_encoded->data.ptr,1,g_encoded->rows*g_encoded->cols);
44 cvReleaseMat(&g_encoded);
48 bool Camera_GetImage(int num, int width, int height, CvMat * image)
50 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // Need to use a mutex to ensure 2 captures are not open at once
51 pthread_mutex_lock(&mutex);
56 capture = cvCreateCameraCapture(num);
59 else if( num != captureID)
61 cvReleaseCapture(&capture);
62 capture = cvCreateCameraCapture(num);
66 cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, width);
67 cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, height);
69 frame = cvQueryFrame(capture);
73 // Convert the IplImage pointer to CvMat
75 image = cvGetMat(frame, &stub, 0, 0);
79 pthread_mutex_unlock(&mutex); //Close the mutex
85 // Release the capture and IplImage pointers
86 cvReleaseImageHeader(&frame);
87 cvReleaseCapture(&capture);