How to create Camera Profile in UWP App

  1. // MainPage1  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using Windows.UI.Xaml.Controls;  
  5. using CameraProfile;  
  6. namespace SDKTemplate  
  7. {  
  8. public partial class MainPage : Page  
  9. {  
  10. public const string FEATURE_NAME = "CameraProfile";  
  11. List<Scenario> scenarios = new List<Scenario>  
  12. {  
  13. new Scenario() { Title="Locate Record Specific Profile", ClassType=typeof(Scenario1_SetRecordProfile)},  
  14. new Scenario() { Title="Query Profile for Concurrency", ClassType=typeof(Scenario2_ConcurrentProfile)},  
  15. new Scenario() { Title="Query Profile for HDR Support", ClassType=typeof(Scenario3_EnableHdrProfile)}  
  16. };  
  17. }  
  18. public class Scenario  
  19. {  
  20. public string Title { getset; }  
  21. public Type ClassType { getset; }  
  22. }  
  23. }  
  24. //Scenario1.cs  
  25. namespace CameraProfile  
  26. {  
  27. using System;  
  28. using System.Collections.Generic;  
  29. using System.Globalization;  
  30. using System.Linq;  
  31. using System.Threading.Tasks;  
  32. using Windows.Devices.Enumeration;  
  33. using Windows.Media.Capture;  
  34. using Windows.UI.Xaml;  
  35. using Windows.UI.Xaml.Controls;  
  36. using Windows.UI.Xaml.Navigation;  
  37. using SDKTemplate;  
  38. public sealed partial class Scenario1_SetRecordProfile : Page  
  39. {  
  40. private MainPage rootPage;  
  41. public Scenario1_SetRecordProfile()  
  42. {  
  43. this.InitializeComponent();  
  44. }  
  45. protected override void OnNavigatedTo(NavigationEventArgs e)  
  46. {  
  47. rootPage = MainPage.Current;  
  48. }  
  49. private async void InitRecordProfileBtn_Click(object sender, RoutedEventArgs e)  
  50. {  
  51. MediaCapture mediaCapture = new MediaCapture();  
  52. MediaCaptureInitializationSettings mediaInitSettings = new MediaCaptureInitializationSettings();  
  53. string videoDeviceId = string.Empty;  
  54. videoDeviceId = await GetVideoProfileSupportedDeviceIdAsync(Windows.Devices.Enumeration.Panel.Back);  
  55. if (string.IsNullOrEmpty(videoDeviceId))  
  56. {  
  57. await LogStatus("ERROR: No Video Device Id found, verify your device supports profiles", NotifyType.ErrorMessage);  
  58. return;  
  59. }  
  60. await LogStatusToOutputBox(string.Format(CultureInfo.InvariantCulture, "Found video capture device that supports Video Profile, Device Id:\r\n {0}", videoDeviceId));  
  61. await LogStatusToOutputBox("Retrieving all Video Profiles");  
  62. IReadOnlyList<MediaCaptureVideoProfile> profiles = MediaCapture.FindAllVideoProfiles(videoDeviceId);  
  63. await LogStatusToOutputBox(string.Format(CultureInfo.InvariantCulture, "Number of Video Profiles found: {0}", profiles.Count));  
  64. foreach (var profile in profiles)  
  65. {  
  66. var description = profile.SupportedRecordMediaDescription;  
  67. foreach (var desc in description)  
  68. {  
  69. await LogStatusToOutputBox(string.Format(CultureInfo.InvariantCulture,  
  70. "Video Profile Found: Profile Id: {0}\r\n Width: {1} Height: {2} FrameRate: {3}", profile.Id, desc.Width, desc.Height, Math.Round(desc.FrameRate).ToString()));  
  71. }  
  72. }  
  73. await LogStatusToOutputBox("Querying for matching WVGA 30FPS Profile");  
  74. var match = (from profile in profiles  
  75. from desc in profile.SupportedRecordMediaDescription  
  76. where desc.Width == 640 && desc.Height == 480 && Math.Round(desc.FrameRate) == 30  
  77. select new { profile, desc }).FirstOrDefault();  
  78. if (match != null)  
  79. {  
  80. mediaInitSettings.VideoProfile = match.profile;  
  81. mediaInitSettings.RecordMediaDescription = match.desc;  
  82. await LogStatus(string.Format(CultureInfo.InvariantCulture,  
  83. "Matching WVGA 30FPS Video Profile found: \r\n Profile Id: {0}\r\n Width: {1} Height: {2} FrameRate: {3}",  
  84. mediaInitSettings.VideoProfile.Id, mediaInitSettings.RecordMediaDescription.Width,  
  85. mediaInitSettings.RecordMediaDescription.Height, Math.Round(mediaInitSettings.RecordMediaDescription.FrameRate).ToString()), NotifyType.StatusMessage);  
  86. }  
  87. else  
  88. {  
  89. mediaInitSettings.VideoProfile = profiles[0];  
  90. await LogStatus("Could not locate a matching profile, setting to default recording profile", NotifyType.ErrorMessage);  
  91. }  
  92. await mediaCapture.InitializeAsync(mediaInitSettings);  
  93. await LogStatusToOutputBox("Media Capture settings initialized to selected profile");  
  94. }  
  95. private async void InitCustomProfileBtn_Click(object sender, RoutedEventArgs e)  
  96. {  
  97. MediaCapture mediaCapture = new MediaCapture();  
  98. MediaCaptureInitializationSettings mediaInitSettings = new MediaCaptureInitializationSettings();  
  99. string videoDeviceId = string.Empty;  
  100. string customProfileId = "{A0E517E8-8F8C-4F6F-9A57-46FC2F647EC0},0";  
  101. videoDeviceId = await GetVideoProfileSupportedDeviceIdAsync(Windows.Devices.Enumeration.Panel.Back);  
  102. if (string.IsNullOrEmpty(videoDeviceId))  
  103. {  
  104. await LogStatus("ERROR: No Video Device Id found, verify your device supports profiles", NotifyType.ErrorMessage);  
  105. return;  
  106. }  
  107. await LogStatusToOutputBox(string.Format(CultureInfo.InvariantCulture, "Found video capture device that supports Video Profile, Device Id:\r\n {0}", videoDeviceId));  
  108. await LogStatusToOutputBox("Querying device for custom profile support");  
  109. mediaInitSettings.VideoProfile = (from profile in MediaCapture.FindAllVideoProfiles(videoDeviceId)  
  110. where profile.Id == customProfileId  
  111. select profile).FirstOrDefault();  
  112. if (mediaInitSettings.VideoProfile == null)  
  113. {  
  114. await LogStatus("Could not locate a matching profile, setting to default recording profile", NotifyType.ErrorMessage);  
  115. mediaInitSettings.VideoProfile = MediaCapture.FindAllVideoProfiles(videoDeviceId).FirstOrDefault();  
  116. }  
  117. await LogStatus(string.Format(CultureInfo.InvariantCulture, "Custom recording profile located: {0}", customProfileId), NotifyType.StatusMessage);  
  118. await mediaCapture.InitializeAsync(mediaInitSettings);  
  119. }  
  120. public async Task<string> GetVideoProfileSupportedDeviceIdAsync(Windows.Devices.Enumeration.Panel panel)  
  121. {  
  122. string deviceId = string.Empty;  
  123. await LogStatusToOutputBox("Looking for all video capture devices");  
  124. DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);  
  125. await LogStatusToOutputBox(string.Format(CultureInfo.InvariantCulture, "Number of video capture devices found: {0}", devices.Count.ToString()));  
  126. foreach (var device in devices)  
  127. {  
  128. if (MediaCapture.IsVideoProfileSupported(device.Id) && device.EnclosureLocation.Panel == panel)  
  129. {  
  130. deviceId = device.Id;  
  131. break;  
  132. }  
  133. }  
  134. return deviceId;  
  135. }  
  136. private async Task LogStatusToOutputBox(string message)  
  137. {  
  138. await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>  
  139. {  
  140. outputBox.Text += message + "\r\n";  
  141. outputScrollViewer.ChangeView(0, outputBox.ActualHeight, 1);  
  142. });  
  143. }  
  144. private async Task LogStatus(string message, NotifyType type)  
  145. {  
  146. await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>  
  147. {  
  148. rootPage.NotifyUser(message, type);  
  149. });  
  150. await LogStatusToOutputBox(message);  
  151. }  
  152. }  
  153. }  
  154. //Scenario2.cs  
  155. namespace CameraProfile  
  156. {  
  157. using System;  
  158. using System.Globalization;  
  159. using System.Linq;  
  160. using System.Threading.Tasks;  
  161. using Windows.Devices.Enumeration;  
  162. using Windows.Media.Capture;  
  163. using Windows.UI.Xaml.Controls;  
  164. using Windows.UI.Xaml.Navigation;  
  165. using SDKTemplate;  
  166. public sealed partial class Scenario2_ConcurrentProfile : Page  
  167. {  
  168. private MainPage rootPage;  
  169. public Scenario2_ConcurrentProfile()  
  170. {  
  171. this.InitializeComponent();  
  172. }  
  173. protected override void OnNavigatedTo(NavigationEventArgs e)  
  174. {  
  175. rootPage = MainPage.Current;  
  176. }  
  177. private async void CheckConcurrentProfileBtn_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)  
  178. {  
  179. bool concurrencySupported = false;  
  180. string frontVideoDeviceId = string.Empty;  
  181. string backVideoDeviceId = string.Empty;  
  182. MediaCapture mediaCaptureFront = new MediaCapture();  
  183. MediaCapture mediaCaptureBack = new MediaCapture();  
  184. await LogStatusToOutputBox("Looking for all video capture devices on front panel");  
  185. frontVideoDeviceId = await GetVideoProfileSupportedDeviceIdAsync(Windows.Devices.Enumeration.Panel.Front);  
  186. await LogStatusToOutputBox("Looking for all video capture devices on back panel");  
  187. backVideoDeviceId = await GetVideoProfileSupportedDeviceIdAsync(Windows.Devices.Enumeration.Panel.Back);  
  188. if (string.IsNullOrEmpty(frontVideoDeviceId) || string.IsNullOrEmpty(backVideoDeviceId))  
  189. {  
  190. await LogStatus("ERROR: A capture device doesn't support Video Profile", NotifyType.ErrorMessage);  
  191. return;  
  192. }  
  193. await LogStatusToOutputBox("Video Profiles are supported on both cameras");  
  194. MediaCaptureInitializationSettings mediaInitSettingsFront = new MediaCaptureInitializationSettings();  
  195. MediaCaptureInitializationSettings mediaInitSettingsBack = new MediaCaptureInitializationSettings();  
  196. mediaInitSettingsFront.VideoDeviceId = frontVideoDeviceId;  
  197. mediaInitSettingsBack.VideoDeviceId = backVideoDeviceId;  
  198. await LogStatusToOutputBox(string.Format(CultureInfo.InvariantCulture, "Front Device Id located: {0}", frontVideoDeviceId.ToString()));  
  199. await LogStatusToOutputBox(string.Format(CultureInfo.InvariantCulture, "Back Device Id located: {0}", backVideoDeviceId.ToString()));  
  200. await LogStatusToOutputBox("Querying for concurrent profile");  
  201. var concurrentProfile = (from frontProfile in MediaCapture.FindConcurrentProfiles(frontVideoDeviceId)  
  202. from backProfile in MediaCapture.FindConcurrentProfiles(backVideoDeviceId)  
  203. where frontProfile.Id == backProfile.Id  
  204. select new { frontProfile, backProfile }).FirstOrDefault();  
  205. if (concurrentProfile != null)  
  206. {  
  207. mediaInitSettingsFront.VideoProfile = concurrentProfile.frontProfile;  
  208. mediaInitSettingsBack.VideoProfile = concurrentProfile.backProfile;  
  209. concurrencySupported = true;  
  210. await LogStatus("Concurrent profile located, device supports concurrency", NotifyType.StatusMessage);  
  211. }  
  212. else  
  213. {  
  214. mediaInitSettingsFront.VideoProfile = null;  
  215. mediaInitSettingsBack.VideoProfile = null;  
  216. await LogStatus("No Concurrent profiles located, device does not support concurrency", NotifyType.ErrorMessage);  
  217. }  
  218. await LogStatusToOutputBox("Initializing Front camera settings");  
  219. await mediaCaptureFront.InitializeAsync(mediaInitSettingsFront);  
  220. if (concurrencySupported)  
  221. {  
  222. await LogStatusToOutputBox("Device supports concurrency, initializing Back camera settings");  
  223. await mediaCaptureBack.InitializeAsync(mediaInitSettingsBack);  
  224. }  
  225. }  
  226. public async Task<string> GetVideoProfileSupportedDeviceIdAsync(Windows.Devices.Enumeration.Panel panel)  
  227. {  
  228. string deviceId = string.Empty;  
  229. DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);  
  230. await LogStatusToOutputBox(string.Format(CultureInfo.InvariantCulture, "Number of video capture devices found: {0}", devices.Count.ToString()));  
  231. foreach (var device in devices)  
  232. {  
  233. if (MediaCapture.IsVideoProfileSupported(device.Id) && device.EnclosureLocation.Panel == panel)  
  234. {  
  235. deviceId = device.Id;  
  236. break;  
  237. }  
  238. }  
  239. return deviceId;  
  240. }  
  241. private async Task LogStatusToOutputBox(string message)  
  242. {  
  243. await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>  
  244. {  
  245. outputBox.Text += message + "\r\n";  
  246. outputScrollViewer.ChangeView(0, outputBox.ActualHeight, 1);  
  247. });  
  248. }  
  249. private async Task LogStatus(string message, NotifyType type)  
  250. {  
  251. await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>  
  252. {  
  253. rootPage.NotifyUser(message, type);  
  254. });  
  255. await LogStatusToOutputBox(message);  
  256. }  
  257. }  
  258. }  
  259. //Scenario3.cs  
  260. namespace CameraProfile  
  261. {  
  262. using System;  
  263. using System.Collections.Generic;  
  264. using System.Globalization;  
  265. using Windows.Devices.Enumeration;  
  266. using Windows.Media.Capture;  
  267. using Windows.UI.Xaml.Controls;  
  268. using Windows.UI.Xaml.Navigation;  
  269. using SDKTemplate;  
  270. using System.Threading.Tasks;  
  271. public sealed partial class Scenario3_EnableHdrProfile : Page  
  272. {  
  273. private MainPage rootPage;  
  274. public Scenario3_EnableHdrProfile()  
  275. {  
  276. this.InitializeComponent();  
  277. }  
  278. protected override void OnNavigatedTo(NavigationEventArgs e)  
  279. {  
  280. rootPage = MainPage.Current;  
  281. }  
  282. private async void CheckHdrSupportBtn_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)  
  283. {  
  284. string videoDeviceId = string.Empty;  
  285. bool HdrVideoSupported = false;  
  286. MediaCapture mediaCapture = new MediaCapture();  
  287. MediaCaptureInitializationSettings mediaCaptureInitSetttings = new MediaCaptureInitializationSettings();  
  288. await LogStatusToOutputBox("Querying for video capture device on back of the device that supports Video Profile");  
  289. videoDeviceId = await GetVideoProfileSupportedDeviceIdAsync(Windows.Devices.Enumeration.Panel.Back);  
  290. if (string.IsNullOrEmpty(videoDeviceId))  
  291. {  
  292. await LogStatus("ERROR: No Video Device Id found, verify your device supports profiles", NotifyType.ErrorMessage);  
  293. return;  
  294. }  
  295. await LogStatusToOutputBox(string.Format(CultureInfo.InvariantCulture,  
  296. "Found device that supports Video Profile, Device Id:\r\n {0}", videoDeviceId));  
  297. IReadOnlyList<MediaCaptureVideoProfile> profiles = MediaCapture.FindKnownVideoProfiles(videoDeviceId, KnownVideoProfile.VideoRecording);  
  298. foreach (MediaCaptureVideoProfile profile in profiles)  
  299. {  
  300. IReadOnlyList<MediaCaptureVideoProfileMediaDescription> recordMediaDescription = profile.SupportedRecordMediaDescription;  
  301. foreach (MediaCaptureVideoProfileMediaDescription videoProfileMediaDescription in recordMediaDescription)  
  302. {  
  303. if (videoProfileMediaDescription.IsHdrVideoSupported)  
  304. {  
  305. mediaCaptureInitSetttings.VideoProfile = profile;  
  306. mediaCaptureInitSetttings.RecordMediaDescription = videoProfileMediaDescription;  
  307. HdrVideoSupported = true;  
  308. await LogStatus("HDR supported video profile found, set video profile to current HDR supported profile", NotifyType.StatusMessage);  
  309. break;  
  310. }  
  311. }  
  312. if (HdrVideoSupported)  
  313. {  
  314. break;  
  315. }  
  316. }  
  317. await LogStatusToOutputBox("Initializing Media settings to HDR Supported video profile");  
  318. await mediaCapture.InitializeAsync(mediaCaptureInitSetttings);  
  319. if (HdrVideoSupported)  
  320. {  
  321. await LogStatusToOutputBox("Initializing HDR Video Mode to Auto");  
  322. mediaCapture.VideoDeviceController.HdrVideoControl.Mode = Windows.Media.Devices.HdrVideoMode.Auto;  
  323. }  
  324. }  
  325. public async Task<string> GetVideoProfileSupportedDeviceIdAsync(Windows.Devices.Enumeration.Panel panel)  
  326. {  
  327. string deviceId = string.Empty;  
  328. await LogStatusToOutputBox("Looking for all video capture devices");  
  329. DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);  
  330. await LogStatusToOutputBox(string.Format(CultureInfo.InvariantCulture, "Number of video capture devices found: {0}", devices.Count.ToString()));  
  331. foreach (var device in devices)  
  332. {  
  333. if (MediaCapture.IsVideoProfileSupported(device.Id) && device.EnclosureLocation.Panel == panel)  
  334. {  
  335. deviceId = device.Id;  
  336. break;  
  337. }  
  338. }  
  339. return deviceId;  
  340. }  
  341. private async Task LogStatusToOutputBox(string message)  
  342. {  
  343. await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>  
  344. {  
  345. outputBox.Text += message + "\r\n";  
  346. outputScrollViewer.ChangeView(0, outputBox.ActualHeight, 1);  
  347. });  
  348. }  
  349. private async Task LogStatus(string message, NotifyType type)  
  350. {  
  351. await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>  
  352. {  
  353. rootPage.NotifyUser(message, type);  
  354. });  
  355. await LogStatusToOutputBox(message);  
  356. }  
  357. }  
  358. }