Created
July 10, 2020 06:38
-
-
Save yushulx/2bfddc9559f6e0d5780c8e7c2547cb3c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Text; | |
using System.Net; | |
using System.Threading.Tasks; | |
using OpenCvSharp; | |
namespace Web | |
{ | |
class Program | |
{ | |
public static HttpListener listener; | |
public static string url = "http://localhost:2020/"; | |
public static string pageData = | |
"<!DOCTYPE>" + | |
"<html>" + | |
" <head>" + | |
" <title>HttpListener Example</title>" + | |
" </head>" + | |
" <body>" + | |
"<img id=\"image\"/>"+ | |
" <script type=\"text/javascript\">var image = document.getElementById('image');function refresh() {image.src = \"/image?\" + new Date().getTime();image.onload= function(){setTimeout(refresh, 30);}}refresh();</script> "+ | |
" </body>" + | |
"</html>"; | |
public static VideoCapture capture = new VideoCapture(0); | |
public static async Task HandleIncomingConnections() | |
{ | |
while (true) | |
{ | |
HttpListenerContext ctx = await listener.GetContextAsync(); | |
HttpListenerRequest req = ctx.Request; | |
HttpListenerResponse resp = ctx.Response; | |
if ((req.HttpMethod == "GET") && (req.Url.AbsolutePath.StartsWith("/image"))) { | |
resp.ContentType = "image/jpeg"; | |
using (Mat image = new Mat()) | |
{ | |
capture.Read(image); | |
Cv2.ImEncode(".jpg", image, out var imageData); | |
await resp.OutputStream.WriteAsync(imageData, 0, imageData.Length); | |
resp.Close(); | |
} | |
} | |
else { | |
// Write the response info | |
byte[] data = Encoding.UTF8.GetBytes(pageData); | |
resp.ContentType = "text/html"; | |
resp.ContentEncoding = Encoding.UTF8; | |
resp.ContentLength64 = data.LongLength; | |
// Write out to the response stream (asynchronously), then close it | |
await resp.OutputStream.WriteAsync(data, 0, data.Length); | |
resp.Close(); | |
} | |
} | |
} | |
static void Main(string[] args) | |
{ | |
// Create a Http server and start listening for incoming connections | |
listener = new HttpListener(); | |
listener.Prefixes.Add(url); | |
listener.Start(); | |
Console.WriteLine("Listening for connections on {0}", url); | |
// Handle requests | |
Task listenTask = HandleIncomingConnections(); | |
listenTask.GetAwaiter().GetResult(); | |
// Close the listener | |
listener.Close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment