From 101de2b07aa2feefa74f7e73876fd5cc8fc696cf Mon Sep 17 00:00:00 2001
From: AlexeyAB <alexeyab84@gmail.com>
Date: Tue, 20 Mar 2018 21:40:01 +0000
Subject: [PATCH] More stable web-cam

---
 src/http_stream.cpp |   39 ++++++++++++++++++++++++++++++++++++---
 1 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/src/http_stream.cpp b/src/http_stream.cpp
index 4392a27..ca57728 100644
--- a/src/http_stream.cpp
+++ b/src/http_stream.cpp
@@ -37,6 +37,7 @@
 #define SOCKET_ERROR   -1
 #endif /* _WIN32 */
 
+#include <cstdio>
 #include <vector>
 #include <iostream>
 using std::cerr;
@@ -56,7 +57,7 @@
 	int timeout; // master sock timeout, shutdown after timeout millis.
 	int quality; // jpeg compression [1..100]
 
-	int _write(int sock, char *s, int len)
+	int _write(int sock, char const*const s, int len)
 	{
 		if (len < 1) { len = strlen(s); }
 		return ::send(sock, s, len, 0);
@@ -128,7 +129,7 @@
 		params.push_back(IMWRITE_JPEG_QUALITY);
 		params.push_back(quality);
 		cv::imencode(".jpg", frame, outbuf, params);
-		unsigned int outlen = outbuf.size();
+		size_t outlen = outbuf.size();
 
 #ifdef _WIN32 
 		for (unsigned i = 0; i<rread.fd_count; i++)
@@ -169,7 +170,7 @@
 			else // existing client, just stream pix
 			{
 				char head[400];
-				sprintf(head, "--mjpegstream\r\nContent-Type: image/jpeg\r\nContent-Length: %lu\r\n\r\n", outlen);
+				sprintf(head, "--mjpegstream\r\nContent-Type: image/jpeg\r\nContent-Length: %zu\r\n\r\n", outlen);
 				_write(s, head, 0);
 				int n = _write(s, (char*)(&outbuf[0]), outlen);
 				//cerr << "known client " << s << " " << n << endl;
@@ -194,4 +195,36 @@
 	std::cout << " MJPEG-stream sent. \n";
 }
 
+
+CvCapture* get_capture_webcam(int index) {
+	CvCapture* cap = NULL;
+	try {
+		cap = (CvCapture*)new cv::VideoCapture(index);
+		//((cv::VideoCapture*)cap)->set(CV_CAP_PROP_FRAME_WIDTH, 1280);
+		//((cv::VideoCapture*)cap)->set(CV_CAP_PROP_FRAME_HEIGHT, 960);
+	}
+	catch (...) {
+		std::cout << " Error: Web-camera " << index << " can't be opened! \n";
+	}
+	return cap;
+}
+
+IplImage* get_webcam_frame(CvCapture *cap) {
+	IplImage* src = NULL;
+	try {
+		cv::VideoCapture &cpp_cap = *(cv::VideoCapture *)cap;
+		cv::Mat frame;
+		if (cpp_cap.isOpened()) {
+			cpp_cap >> frame;
+			src = cvCreateImage(cvSize(frame.cols, frame.rows), 8, frame.channels());
+			*src = frame;
+		}
+	}
+	catch (...) {
+		std::cout << " Web-camera stoped! \n";
+	}
+	return src;
+}
+
+
 #endif	// OPENCV
\ No newline at end of file

--
Gitblit v1.10.0