forked from dotnet/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.cs
197 lines (178 loc) · 6.8 KB
/
Image.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Buffers;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Iot.Device.Media;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace CameraIoT.Controllers
{
/// <summary>
/// The main image controller
/// </summary>
[ApiController]
[Route("[controller]")]
public class ImageController : ControllerBase
{
private readonly ILogger<ImageController> _logger;
private readonly Camera _camera;
/// <summary>
/// Controller creation
/// </summary>
/// <param name="logger">Logger</param>
/// <param name="camera">The camera singleton</param>
public ImageController(ILogger<ImageController> logger, Camera camera)
{
_logger = logger;
_camera = camera;
}
/// <summary>
/// Get a single image http(s)://url/image
/// </summary>
/// <returns>A JPEG Image</returns>
[HttpGet]
public ActionResult Get()
{
try
{
return File(_camera.TakePicture(), "image/jpeg");
}
catch (Exception ex)
{
_logger.LogError(ex.ToString());
throw;
}
}
/// <summary>
/// Get an MJPEG stream http(s)://url/image/stream
/// </summary>
[HttpGet("stream")]
public void GetStream()
{
var bufferingFeature = HttpContext.Response.HttpContext.Features.Get<IHttpResponseBodyFeature>();
bufferingFeature?.DisableBuffering();
HttpContext.Response.StatusCode = 200;
HttpContext.Response.ContentType = "multipart/x-mixed-replace; boundary=--frame";
HttpContext.Response.Headers.Add("Connection", "Keep-Alive");
HttpContext.Response.Headers.Add("CacheControl", "no-cache");
_camera.NewImageReady += WriteBufferBody;
try
{
_logger.LogWarning($"Entering streaming loop");
_camera.StartCapture();
while (!HttpContext.RequestAborted.IsCancellationRequested)
{
}
}
catch (Exception ex)
{
_logger.LogError($"Exception in streaming: {ex}");
}
finally
{
HttpContext.Response.Body.Close();
_logger.LogInformation("End of streaming");
}
_camera.NewImageReady -= WriteBufferBody;
_camera.StopCapture();
}
private async void WriteBufferBody(object sender, NewImageBufferReadyEventArgs e)
{
try
{
await HttpContext.Response.BodyWriter.WriteAsync(CreateHeader(e.Length));
await HttpContext.Response.BodyWriter.WriteAsync(e.ImageBuffer.AsMemory().Slice(0, e.Length));
await HttpContext.Response.BodyWriter.WriteAsync(CreateFooter());
}
catch (ObjectDisposedException)
{
// ignore this as its thrown when the stream is stopped
}
ArrayPool<byte>.Shared.Return(e.ImageBuffer);
}
/// <summary>
/// Get an modified MJPEG stream http(s)://url/image/modified
/// </summary>
[HttpGet("modified")]
public void GetModifiedStream()
{
var bufferingFeature = HttpContext.Response.HttpContext.Features.Get<IHttpResponseBodyFeature>();
bufferingFeature?.DisableBuffering();
HttpContext.Response.StatusCode = 200;
HttpContext.Response.ContentType = "multipart/x-mixed-replace; boundary=--frame";
HttpContext.Response.Headers.Add("Connection", "Keep-Alive");
HttpContext.Response.Headers.Add("CacheControl", "no-cache");
_camera.NewImageReady += WriteModifiedBufferBody;
try
{
_logger.LogWarning($"Entering streaming loop");
_camera.StartCapture();
while (!HttpContext.RequestAborted.IsCancellationRequested)
{
}
}
catch (Exception ex)
{
_logger.LogError($"Exception in streaming: {ex}");
}
finally
{
HttpContext.Response.Body.Close();
_logger.LogInformation("End of streaming");
}
_camera.NewImageReady -= WriteModifiedBufferBody;
_camera.StopCapture();
}
private async void WriteModifiedBufferBody(object sender, NewImageBufferReadyEventArgs e)
{
try
{
// using System.Drawing has serious performance implications in the context of video streaming from low powered devices,
// here is a 'simple' example of modifying the image, which will not be fast enough in most use cases.
using var stream = new MemoryStream(e.ImageBuffer.AsMemory().Slice(0, e.Length).ToArray());
Bitmap myBitmap = new Bitmap(stream);
Graphics g = Graphics.FromImage(myBitmap);
g.DrawString(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), new Font("Tahoma", 20), Brushes.White, new PointF(0, 0));
using var ms = new MemoryStream();
myBitmap.Save(ms, ImageFormat.Jpeg);
await HttpContext.Response.BodyWriter.WriteAsync(CreateHeader(e.Length));
await HttpContext.Response.BodyWriter.WriteAsync(ms.ToArray());
await HttpContext.Response.BodyWriter.WriteAsync(CreateFooter());
}
catch (ObjectDisposedException)
{
// ignore this as its thrown when the stream is stopped
}
ArrayPool<byte>.Shared.Return(e.ImageBuffer);
}
/// <summary>
/// Create a MJPEG header.
/// </summary>
/// <param name="length">The length of the data</param>
/// <returns></returns>
private byte[] CreateHeader(int length)
{
string header =
"--frame\r\n" +
"Content-Type:image/jpeg\r\n" +
"Content-Length:" + length + "\r\n\r\n";
return Encoding.ASCII.GetBytes(header);
}
/// <summary>
/// Create the MJPEG footer
/// </summary>
/// <returns></returns>
private byte[] CreateFooter()
{
return Encoding.ASCII.GetBytes("\r\n");
}
}
}