Merge branch 'master' of https://github.com/szmoore/MCTX3420
[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
7 CvCapture *captures[2] = {0};
8
9 void Image_Handler(FCGIContext * context, char * params)
10 {
11         int num = 0, width = 800, height = 600;
12         FCGIValue val[] = {
13                 {"num", &num, FCGI_INT_T},
14                 {"width", &width, FCGI_INT_T},
15                 {"height", &height, FCGI_INT_T}
16         };
17         if (!FCGI_ParseRequest(context, params, val, 3))
18                 return;
19         else if (num < 0 || num > 1) {
20                 FCGI_RejectJSON(context, "Invalid capture number");
21                 return;
22         } else if (width <= 0 || height <= 0) {
23                 FCGI_RejectJSON(context, "Invalid width/height");
24                 return;
25         }
26
27         CvCapture *capture = captures[num];
28         if (capture == NULL) {
29                 capture = cvCreateCameraCapture(num);
30                 captures[num] = capture;
31         }
32
33         cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, width);
34         cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, height);
35
36         static int p[] = {CV_IMWRITE_JPEG_QUALITY, 100, 0};
37
38         IplImage * frame = cvQueryFrame(capture);
39         assert(frame != NULL);
40
41 //        CvMat stub;
42  //       CvMat * background = cvGetMat(frame, &stub, 0, 0);
43
44 //      CvMat *cv8u = cvCreateMat(frame->width, frame->height, CV_8U);
45 //      double min, max;
46 //      CvPoint a,b;    
47 //      cvMinMaxLoc(background, &min, &max, &a, &b, 0);
48         
49 //      double ccscale = 255.0/(max-min);
50 //      double ccshift = -min;
51         //cvCvtScale(frame, cv8u, ccscale, ccshift);
52         CvMat * jpg = cvEncodeImage(".jpg", frame, p);
53
54         // Will this work?
55         Log(LOGNOTE, "Sending image!");
56         FCGI_PrintRaw("Content-type: image/jpg\r\n");
57         FCGI_PrintRaw("Cache-Control: no-cache, no-store, must-revalidate\r\n\r\n");
58         //FCGI_PrintRaw("Content-Length: %d", jpg->rows*jpg->cols);
59         FCGI_WriteBinary(jpg->data.ptr,1,jpg->rows*jpg->cols);
60         
61         cvReleaseMat(&jpg);
62         cvReleaseImageHeader(&frame);
63 }

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