Changed capture initilisation
[matches/MCTX3420.git] / server / image.c
1 #include "cv.h"
2 #include "highgui_c.h"
3 #include "image.h"
4 #include <string.h>
5 #include <stdio.h>
6 #include <pthread.h>
7
8 CvCapture *capture;
9 IplImage *frame;
10 int captureID = -1;
11
12 void Image_Handler(FCGIContext * context, char * params)
13 {
14         int num = 0, width = 1600, height = 1200;       // Set Default values
15         FCGIValue val[] = {
16                 {"num", &num, FCGI_INT_T},
17                 {"width", &width, FCGI_INT_T},
18                 {"height", &height, FCGI_INT_T}
19         };
20         if (!FCGI_ParseRequest(context, params, val, 3))        // Populate val
21                 return;
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");
25                 return;
26         // Ensure valid widths
27         } else if (width <= 0 || height <= 0) {
28                 FCGI_RejectJSON(context, "Invalid width/height");
29                 return;
30         }
31         
32         CvMat * g_src = NULL;   // Source Image
33         CvMat * g_encoded;      // Encoded Image
34
35         result = Camera_GetImage( num, width, height ,g_src); 
36         g_encoded = cvEncodeImage("test_encode.jpg",g_src,0);
37
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);
43         
44         cvReleaseMat(&g_encoded);
45         cvReleaseMat(&g_src);
46 }
47         
48  bool Camera_GetImage(int num, int width, int height,  CvMat * image)
49  {
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);
52         bool result = false;
53
54         if( capture == NULL)
55         {
56                 capture = cvCreateCameraCapture(num);
57                 captureID = num;
58         }
59         else if( num != captureID)
60         {
61                 cvReleaseCapture(&capture);
62                 capture = cvCreateCameraCapture(num);   
63                 captureID = num;
64         }
65
66         cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, width);
67         cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, height);
68
69         frame = cvQueryFrame(capture);
70         if( frame == NULL)
71                 return result;
72
73         // Convert the IplImage pointer to CvMat
74         CvMat stub;
75         image = cvGetMat(frame, &stub, 0, 0);
76         if( image == NULL)
77                 return result;
78
79         pthread_mutex_unlock(&mutex);   //Close the mutex
80         return true;
81 }
82
83 void Image_Cleanup()
84 {
85         // Release the capture and IplImage pointers
86         cvReleaseImageHeader(&frame);
87         cvReleaseCapture(&capture);
88 }
89

UCC git Repository :: git.ucc.asn.au