Web API 从缓冲区流式传输图像
Web API streaming images from buffer
出于某种原因,我无法使下面的代码正常工作。我正在尝试通过 HTTP 将图像流式传输到浏览器。
[Route("api/v1/camera/live/stream")]
[HttpGet]
public HttpResponseMessage GetStream()
{
logger.Trace($"GET api/v1/camera/live/stream called");
var response = Request.CreateResponse();
response.Content = new PushStreamContent(new Action<Stream, HttpContent, TransportContext>(
async (stream, content, tansportContext) =>
{
try
{
StreamWriter writer = new StreamWriter(stream);
while (HttpContext.Current.Response.IsClientConnected)
{
var image = databuffer.Get<CameraFrame>().LastOrDefault();
if (image != null)
{
MemoryStream ms = new MemoryStream();
ms.SetLength(0);
ms.WriteTo(BytesOf(databuffer.Get<CameraFrame>().Last().Image));
writer.WriteLine("--boundary");
writer.WriteLine("Content-Type: image/jpeg");
writer.WriteLine($"Content-length: {ms.Length}");
writer.WriteLine();
writer.Write(ms.GetBuffer());
writer.Flush();
}
await Task.Delay(33);
}
}
catch (Exception ex)
{
logger.Error($"Streaming error", ex);
throw;
}
logger.Trace("streaming ended");
}));
response.Content.Headers.ContentType =
MediaTypeHeaderValue.Parse("multipart/x-mixed-replace; boundary=--boundary");
return response;
}
private MemoryStream BytesOf(Bitmap image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms;
}
databuffer.Get() 不断地被相机填充位图帧。
当我 运行 上面的代码都可以正常编译。但是当我使用 chrome 导航到 URL 它不显示图像。
好的,我已经解决了。主要是我没有正确地做流。
[Route("api/v1/camera/live/stream")]
[HttpGet]
public HttpResponseMessage GetStream()
{
logger.Trace($"GET api/v1/camera/live/stream called");
var response = Request.CreateResponse(HttpStatusCode.PartialContent);
response.Content = new PushStreamContent(new Action<Stream, HttpContent, TransportContext>(async (stream, content, tansportContext) =>
{
EventHandler<AcquiredDataEventArgs<CameraFrame>> handler = (_, __) => { };
try
{
MjpegWriter writer = new MjpegWriter(stream);
handler = (s, e) => writer.Write(e.Data.Image.GetJpegStream());
camera.DataAcquired += handler;
while (HttpContext.Current.Response.IsClientConnected)
{
await Task.Delay(100);
}
camera.DataAcquired -= handler;
}
catch (Exception ex)
{
logger.Error($"Streaming error", ex);
throw;
}
logger.Trace("streaming ended");
}));
response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/x-mixed-replace; boundary=--boundary");
return response;
}
出于某种原因,我无法使下面的代码正常工作。我正在尝试通过 HTTP 将图像流式传输到浏览器。
[Route("api/v1/camera/live/stream")]
[HttpGet]
public HttpResponseMessage GetStream()
{
logger.Trace($"GET api/v1/camera/live/stream called");
var response = Request.CreateResponse();
response.Content = new PushStreamContent(new Action<Stream, HttpContent, TransportContext>(
async (stream, content, tansportContext) =>
{
try
{
StreamWriter writer = new StreamWriter(stream);
while (HttpContext.Current.Response.IsClientConnected)
{
var image = databuffer.Get<CameraFrame>().LastOrDefault();
if (image != null)
{
MemoryStream ms = new MemoryStream();
ms.SetLength(0);
ms.WriteTo(BytesOf(databuffer.Get<CameraFrame>().Last().Image));
writer.WriteLine("--boundary");
writer.WriteLine("Content-Type: image/jpeg");
writer.WriteLine($"Content-length: {ms.Length}");
writer.WriteLine();
writer.Write(ms.GetBuffer());
writer.Flush();
}
await Task.Delay(33);
}
}
catch (Exception ex)
{
logger.Error($"Streaming error", ex);
throw;
}
logger.Trace("streaming ended");
}));
response.Content.Headers.ContentType =
MediaTypeHeaderValue.Parse("multipart/x-mixed-replace; boundary=--boundary");
return response;
}
private MemoryStream BytesOf(Bitmap image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms;
}
databuffer.Get() 不断地被相机填充位图帧。
当我 运行 上面的代码都可以正常编译。但是当我使用 chrome 导航到 URL 它不显示图像。
好的,我已经解决了。主要是我没有正确地做流。
[Route("api/v1/camera/live/stream")]
[HttpGet]
public HttpResponseMessage GetStream()
{
logger.Trace($"GET api/v1/camera/live/stream called");
var response = Request.CreateResponse(HttpStatusCode.PartialContent);
response.Content = new PushStreamContent(new Action<Stream, HttpContent, TransportContext>(async (stream, content, tansportContext) =>
{
EventHandler<AcquiredDataEventArgs<CameraFrame>> handler = (_, __) => { };
try
{
MjpegWriter writer = new MjpegWriter(stream);
handler = (s, e) => writer.Write(e.Data.Image.GetJpegStream());
camera.DataAcquired += handler;
while (HttpContext.Current.Response.IsClientConnected)
{
await Task.Delay(100);
}
camera.DataAcquired -= handler;
}
catch (Exception ex)
{
logger.Error($"Streaming error", ex);
throw;
}
logger.Trace("streaming ended");
}));
response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/x-mixed-replace; boundary=--boundary");
return response;
}