0% found this document useful (0 votes)
8 views3 pages

IoT Spy Camera Streaming Code

The document contains C++ code for a simple IoT spy camera application using the Mongoose web server and OpenCV. It streams video from the camera over HTTP in MJPEG format, allowing users to view the stream in a web browser. The application listens on port 8080 and serves a basic HTML page with the video stream embedded.

Uploaded by

blessm7740
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

IoT Spy Camera Streaming Code

The document contains C++ code for a simple IoT spy camera application using the Mongoose web server and OpenCV. It streams video from the camera over HTTP in MJPEG format, allowing users to view the stream in a web browser. The application listens on port 8080 and serves a basic HTML page with the video stream embedded.

Uploaded by

blessm7740
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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;

You might also like