AdvancedCapture.js 5.4 KB

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