C#如何通过httpclient发送流网络摄像头

时间:2019-01-18 06:56:42

标签: c# post httprequest

我正在尝试使用httpclient发送网络摄像头流。 但是我不知道如何像tcp套接字那样无缝地发送数据。

为了测试,我使用OpencvSharp读取VideoFrame,我认为这没关系。

我尝试过的内容:

        string ch = Console.ReadLine();

        RestApiManager manager = new RestApiManager();
        VideoCapture cap = new VideoCapture(@"Video.mp4");
        byte[] bimg;
        while (cap.IsOpened())
        {
            Mat mat = new Mat();
            if (cap.Read(mat))
            {
                Cv2.ImEncode(".jpg", mat, out bimg);
                Console.WriteLine(bimg.ToString());
                if (manager.cctvPost(ch, bimg).Result)
                {
                    Console.WriteLine("okay");
                }
                else
                    Console.WriteLine("failed");
            }
        }
    }


public class RestApiManager
{
    private const string url = @"http://localhost:5000/";
    private HttpClient client;

    public RestApiManager()
    {
        client = new HttpClient();
        client.BaseAddress = new Uri(url);
    }
    public async Task<bool> cctvPost(string ch, byte[] data)
    {
        client.DefaultRequestHeaders.Accept.Add(
            new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("image/jpeg"));

        //var item = new Product() { channel = ch, image = data };
        HttpContent contentPost = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "image/jpeg");
        var task = Task.Run(() => client.PostAsync("cctv", contentPost).Result);
        HttpResponseMessage response = await task;
        return response.IsSuccessStatusCode;
    }
}

这将发送每个从视频读取的帧并发送。

和服务器获取每个图像并发送响应(200)。

0 个答案:

没有答案