如何使用owin / katana流式传输jpeg?

时间:2015-10-22 17:52:27

标签: c# .net-4.5 owin katana

我在Windows服务中自我托管OWIN / KATANA。现在我已经实现了一种从相机中抓取单个图像的方法。我想从相机中抓取多个帧并将它们流回到html页面上的img标签。这可能与OWIN / KATANA有关吗?

1 个答案:

答案 0 :(得分:0)

app.Map("/Camera/Video", a =>
{
    a.Run(context =>
    {
        string connectionid = CurrentDevice.Value.ToString();
        object ret = DeviceManager.Instance.SendMessageToDevice(connectionid, "startmovie");
        context.Response.Headers.Add("Content-Type", new string[] { "multipart/x-mixed-replace; boundary=--jpgboundary" });

        bool con = true;
        StreamWriter writer = new StreamWriter(context.Response.Body);
        while (con)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                Image img = (Image)DeviceManager.Instance.SendMessageToDevice(connectionid, "capturestill");
                img.Save(ms, ImageFormat.Jpeg);
                byte[] buffer = ms.GetBuffer();

                writer.WriteLine("--jpgboundary");
                writer.WriteLine("Content-Type: image/jpeg");
                writer.WriteLine(string.Format("Content-length: {0}", buffer.Length));
                writer.WriteLine();
                context.Response.Write(buffer);
                //writer.WriteLine(Convert.ToBase64String(buffer));
                writer.Flush();
            }
            Thread.Sleep(200);
        }
        DeviceManager.Instance.SendMessageToDevice(connectionid, "stopmovie");
        return context.Response.WriteAsync("");
    });
});

我弄清楚我的问题是什么。我正在使用WriteAsync,我只需要使用Write。以上工作很棒。我只需要弄清楚如何阻止它。