MY PROJECT CODE
// File: spy_camera.cpp
#include "mongoose.h"
#include <opencv2/[Link]>
#include <iostream>
#include <vector>
using namespace cv;
using namespace std;
static const char *s_listen_on = "[Link]
static void handle_stream(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
if (mg_http_match_uri(hm, "/stream")) {
mg_printf(c,
"HTTP/1.1 200 OK\r\n"
"Cache-Control: no-cache\r\n"
"Pragma: no-cache\r\n"
"Content-Type: multipart/x-mixed-replace; boundary=frame\r\n\r\n");
VideoCapture cap(0);
if (![Link]()) {
cerr << "Error opening camera.\n";
return;
Mat frame;
vector<uchar> buf;
vector<int> param = { IMWRITE_JPEG_QUALITY, 80 };
while (true) {
cap >> frame;
if ([Link]()) break;
imencode(".jpg", frame, buf, param);
mg_printf(c, "--frame\r\nContent-Type: image/jpeg\r\nContent-Length: %lu\r\n\r\n",
[Link]());
mg_send(c, [Link](), [Link]());
mg_printf(c, "\r\n");
mg_mgr_poll((mg_mgr *)fn_data, 50); // Keep connection alive
} else {
mg_http_reply(c, 200, "Content-Type: text/html\r\n",
"<html><body><h1>IoT Spy Camera</h1><img src='/stream'
/></body></html>");
}
}
int main() {
struct mg_mgr mgr;
mg_mgr_init(&mgr);
cout << "Starting MJPEG stream at [Link] << endl;
mg_http_listen(&mgr, s_listen_on, handle_stream, &mgr);
for (;;) {
mg_mgr_poll(&mgr, 1000);
mg_mgr_free(&mgr);
return 0;