Page 17 - MSDN Magazine, June 2017
P. 17
Figure 1 A Preconfigured Azure IoT Suite Solution Portal and the Remote Universal Windows Platform App, Which Acquires and Processes the Video Stream
declare the corresponding device capability in the Package.appxmanifest.
To initialize the MediaCapture class, you invoke the InitializeAsync method. Eventually, you can pass to that method an instance of the MediaCaptureInitializationSettings class, which lets you specify capture options. You can choose between streaming video or audio and select the capture hardware. Here, I acquire only video from the default webcam (see Figure 2).
Next, using the Source property of the CaptureElement class instance, I wire this object with the MediaCapture control to display video stream. I also invoke helper method GetVideoProperties, which reads and then stores the size of a video frame. I use this information later to get the preview frame for processing. Finally, to actually start
and stopping camera preview, displaying the video stream and pre- senting image brightness (a label and a progress bar). The second tab contains two buttons, which you can use to connect a device to the cloud and to register a device with the IoT portal. The Cloud tab also has a checkbox that lets you enable streaming telemetry data.
Note that the RemoteCamera app is universal, so it can be deployed without any changes to your development PC, a smartphone, a tablet or to the Raspberry Pi.
Most of the logic associated with the UI is implemented within the RemoteCameraViewModel class (see the ViewModels sub- folder of the RemoteCamera project). This class, apart from implementing a few properties that are bound to the UI, handles the video acquisition, image processing and cloud interaction. Those sub-functionalities are implemented in separate classes: CameraCapture, ImageProcessor and CloudHelper, respectively. I’ll discuss CameraCapture and ImageProcessor shortly, while CloudHelper and related helper classes will be described later in the context of the preconfigured Azure IoT solution.
Camera Capture
Camera capture (see CameraCapture.cs under the Helpers folder) is built on top of two elements: Windows.Media.Capture.Media- Capture class and Windows.UI.Xaml.Controls.CaptureElement. The former is used to acquire video, while the latter displays the acquired video stream. I acquire video with a webcam, so I have to msdnmagazine.com
and stop video acquisition, I invoke StartPreviewAsync and Stop- PreviewAsync of the MediaCapture class. In the CameraCapture I wrapped those methods with additional logic, verifying initial- ization and the preview state:
public async Task Start() {
if (IsInitialized) {
if (!IsPreviewActive) {
await MediaCapture.StartPreviewAsync();
IsPreviewActive = true; }
} }
When you run the app, you can press the Start preview button, which configures camera acquisition, and you’ll see the camera
Figure 2 Camera Capture Initialization
public MediaCapture { get; private set; } = new MediaCapture(); public bool IsInitialized { get; private set; } = false;
public async Task Initialize(CaptureElement captureElement) {
if (!IsInitialized) {
var settings = new MediaCaptureInitializationSettings() {
StreamingCaptureMode = StreamingCaptureMode.Video };
try {
await MediaCapture.InitializeAsync(settings); GetVideoProperties();
captureElement.Source = MediaCapture;
IsInitialized = true; }
catch (Exception ex) {
Debug.WriteLine(ex.Message);
IsInitialized = false; }
} }
June 2017 13