1//// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF 2//// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO 3//// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 4//// PARTICULAR PURPOSE. 5//// 6//// Copyright (c) Microsoft Corporation. All rights reserved 7 8(function () { 9 "use strict"; 10 11 var cameraList = null; 12 var mediaCaptureMgr = null; 13 var captureInitSettings = null; 14 15 var page = WinJS.UI.Pages.define("/html/AdvancedCapture.html", { 16 17 ready: function (element, options) { 18 scenarioInitialize(); 19 }, 20 21 unload: function (element, options) { 22 // release resources 23 releaseMediaCapture(); 24 } 25 }); 26 27 function scenarioInitialize() { 28 // Initialize the UI elements 29 id("btnStartDevice").disabled = false; 30 id("btnStartDevice").addEventListener("click", startDevice, false); 31 id("btnStartPreview").disabled = true; 32 id("videoEffect").disabled = true; 33 id("btnStartPreview").addEventListener("click", startPreview, false); 34 id("cameraSelect").addEventListener("change", onDeviceChange, false); 35 36 id("videoEffect").addEventListener('change', addEffectToImageStream, false); 37 38 enumerateCameras(); 39 } 40 41 function initCameraSettings() { 42 captureInitSettings = new Windows.Media.Capture.MediaCaptureInitializationSettings(); 43 captureInitSettings.streamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.video 44 45 // If the user chose another capture device, use it by default 46 var selectedIndex = id("cameraSelect").selectedIndex; 47 var deviceInfo = cameraList[selectedIndex]; 48 captureInitSettings.videoDeviceId = deviceInfo.id; 49 } 50 51 // this function takes care of releasing the resources associated with media capturing 52 function releaseMediaCapture() { 53 if (mediaCaptureMgr) { 54 mediaCaptureMgr.close(); 55 mediaCaptureMgr = null; 56 } 57 } 58 59 //Initialize media capture with the current settings 60 function startDevice() { 61 displayStatus("Starting device"); 62 releaseMediaCapture(); 63 initCameraSettings(); 64 65 mediaCaptureMgr = new Windows.Media.Capture.MediaCapture(); 66 mediaCaptureMgr.initializeAsync(captureInitSettings).done(function (result) { 67 // Update the UI 68 id("btnStartPreview").disabled = false; 69 id("btnStartDevice").disabled = true; 70 displayStatus("Device started"); 71 }); 72 } 73 74 function startPreview() { 75 displayStatus("Starting preview"); 76 id("btnStartPreview").disabled = true; 77 id("videoEffect").disabled = false; 78 var video = id("previewVideo"); 79 video.src = URL.createObjectURL(mediaCaptureMgr, { oneTimeOnly: true }); 80 video.play(); 81 displayStatus("Preview started"); 82 } 83 84 function addEffectToImageStream() { 85 var effectId = id("videoEffect").selectedIndex; 86 var props = new Windows.Foundation.Collections.PropertySet(); 87 props.insert("{698649BE-8EAE-4551-A4CB-3EC98FBD3D86}", effectId); 88 89 mediaCaptureMgr.clearEffectsAsync(Windows.Media.Capture.MediaStreamType.videoPreview).then(function () { 90 return mediaCaptureMgr.addEffectAsync(Windows.Media.Capture.MediaStreamType.videoPreview, 'OcvTransform.OcvImageManipulations', props); 91 }).then(function () { 92 displayStatus('Effect has been successfully added'); 93 }, errorHandler); 94 } 95 96 function enumerateCameras() { 97 displayStatus("Enumerating capture devices"); 98 var cameraSelect = id("cameraSelect"); 99 cameraList = null; 100 cameraList = new Array(); 101 102 // Clear the previous list of capture devices if any 103 while (cameraSelect.length > 0) { 104 cameraSelect.remove(0); 105 } 106 107 // Enumerate cameras and add them to the list 108 var deviceInfo = Windows.Devices.Enumeration.DeviceInformation; 109 deviceInfo.findAllAsync(Windows.Devices.Enumeration.DeviceClass.videoCapture).done(function (cameras) { 110 if (cameras.length === 0) { 111 cameraSelect.disabled = true; 112 displayError("No camera was found"); 113 id("btnStartDevice").disabled = true; 114 cameraSelect.add(new Option("No cameras available")); 115 } else { 116 cameras.forEach(function (camera) { 117 cameraList.push(camera); 118 cameraSelect.add(new Option(camera.name)); 119 }); 120 } 121 }, errorHandler); 122 } 123 124 function onDeviceChange() { 125 releaseMediaCapture(); 126 id("btnStartDevice").disabled = false; 127 id("btnStartPreview").disabled = true; 128 id("videoEffect").disabled = true; 129 displayStatus(""); 130 } 131 132 function suspendingHandler(suspendArg) { 133 displayStatus("Suspended"); 134 releaseMediaCapture(); 135 } 136 137 function resumingHandler(resumeArg) { 138 displayStatus("Resumed"); 139 scenarioInitialize(); 140 } 141 142 function errorHandler(err) { 143 displayError(err.message); 144 } 145 146 function failedEventHandler(e) { 147 displayError("Fatal error", e.message); 148 } 149 150 function displayStatus(statusText) { 151 SdkSample.displayStatus(statusText); 152 } 153 154 function displayError(error) { 155 SdkSample.displayError(error); 156 } 157 158 function id(elementId) { 159 return document.getElementById(elementId); 160 } 161})(); 162