8 static CvCapture * g_capture = NULL;
9 static int g_captureID = -1;
12 * Image stream handler. Returns an image to the user.
13 * @param context The context to work in
14 * @param params User specified parameters
16 void Image_Handler(FCGIContext * context, char * params)
19 int num = 0, width = 1600, height = 1200; // Set Default values
21 {"num", &num, FCGI_INT_T},
22 {"width", &width, FCGI_INT_T},
23 {"height", &height, FCGI_INT_T}
25 if (!FCGI_ParseRequest(context, params, val, 3)) // Populate val
27 // 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
28 else if (num < 0 || num > 1) {
29 FCGI_RejectJSON(context, "Invalid capture number");
31 // Ensure valid widths
32 } else if (width <= 0 || height <= 0) {
33 FCGI_RejectJSON(context, "Invalid width/height");
37 IplImage * src = NULL; // Source Image
38 CvMat * encoded = NULL; // Encoded Image
40 Camera_GetImage( num, width, height ,&src);
42 Log(LOGDEBUG, "About to encode");
43 encoded = cvEncodeImage(".jpg",src,0);
44 Log(LOGDEBUG, "Encoded");
46 Log(LOGNOTE, "Sending image!");
47 FCGI_PrintRaw("Content-type: image/jpg\r\n");
48 FCGI_PrintRaw("Cache-Control: no-cache, no-store, must-revalidate\r\n\r\n");
49 //FCGI_PrintRaw("Content-Length: %d", g_encoded->rows*g_encoded->cols);
50 FCGI_WriteBinary(encoded->data.ptr,1,encoded->rows*encoded->cols);
52 cvReleaseMat(&encoded);
53 cvReleaseImageHeader(&src);
57 * Attempts to get an image from a camera
58 * @param num - Camera id
59 * @param width - Width to force
60 * @param height - Height to force
61 * @param frame - Pointer to IplImage* to set with result
62 * @returns true on success, false on error
64 bool Camera_GetImage(int num, int width, int height, IplImage ** frame)
66 Log(LOGDEBUG, "Called with arguments num=%d width=%d height=%d frame=%p", num,width,height, frame);
67 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // Need to use a mutex to ensure 2 captures are not open at once
68 pthread_mutex_lock(&mutex);
71 if( g_capture == NULL)
73 g_capture = cvCreateCameraCapture(num);
76 else if( num != g_captureID)
78 cvReleaseCapture(&g_capture);
79 g_capture = cvCreateCameraCapture(num);
83 if (g_capture != NULL)
86 cvSetCaptureProperty(g_capture, CV_CAP_PROP_FRAME_WIDTH, width);
87 cvSetCaptureProperty(g_capture, CV_CAP_PROP_FRAME_HEIGHT, height);
89 *frame = cvQueryFrame(g_capture);
90 result = (*frame != NULL);
92 //cvShowImage("display", *image);
94 //cvSaveImage("test.jpg",*image,0);
96 Log(LOGDEBUG, "At end of mutex");
99 pthread_mutex_unlock(&mutex); //Close the mutex
101 //NOTE: Never have a "return" statement before the mutex is unlocked; it causes deadlocks!
106 * Executed on cleanup. Releases the OpenCV Capture structs.
110 // Release the capture and IplImage pointers
111 //cvReleaseImageHeader(&g_frame);
112 cvReleaseCapture(&g_capture);