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

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