TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How to create Camera Profile in UWP App
Muthuramalingam Duraipandi
Aug 09
2016
Code
2.1
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
camera.ZIP
// MainPage1
using
System;
using
System.Collections.Generic;
using
Windows.UI.Xaml.Controls;
using
CameraProfile;
namespace
SDKTemplate
{
public
partial
class
MainPage : Page
{
public
const
string
FEATURE_NAME =
"CameraProfile"
;
List<Scenario> scenarios =
new
List<Scenario>
{
new
Scenario() { Title=
"Locate Record Specific Profile"
, ClassType=
typeof
(Scenario1_SetRecordProfile)},
new
Scenario() { Title=
"Query Profile for Concurrency"
, ClassType=
typeof
(Scenario2_ConcurrentProfile)},
new
Scenario() { Title=
"Query Profile for HDR Support"
, ClassType=
typeof
(Scenario3_EnableHdrProfile)}
};
}
public
class
Scenario
{
public
string
Title {
get
;
set
; }
public
Type ClassType {
get
;
set
; }
}
}
//Scenario1.cs
namespace
CameraProfile
{
using
System;
using
System.Collections.Generic;
using
System.Globalization;
using
System.Linq;
using
System.Threading.Tasks;
using
Windows.Devices.Enumeration;
using
Windows.Media.Capture;
using
Windows.UI.Xaml;
using
Windows.UI.Xaml.Controls;
using
Windows.UI.Xaml.Navigation;
using
SDKTemplate;
public
sealed
partial
class
Scenario1_SetRecordProfile : Page
{
private
MainPage rootPage;
public
Scenario1_SetRecordProfile()
{
this
.InitializeComponent();
}
protected
override
void
OnNavigatedTo(NavigationEventArgs e)
{
rootPage = MainPage.Current;
}
private
async
void
InitRecordProfileBtn_Click(
object
sender, RoutedEventArgs e)
{
MediaCapture mediaCapture =
new
MediaCapture();
MediaCaptureInitializationSettings mediaInitSettings =
new
MediaCaptureInitializationSettings();
string
videoDeviceId =
string
.Empty;
videoDeviceId = await GetVideoProfileSupportedDeviceIdAsync(Windows.Devices.Enumeration.Panel.Back);
if
(
string
.IsNullOrEmpty(videoDeviceId))
{
await LogStatus(
"ERROR: No Video Device Id found, verify your device supports profiles"
, NotifyType.ErrorMessage);
return
;
}
await LogStatusToOutputBox(
string
.Format(CultureInfo.InvariantCulture,
"Found video capture device that supports Video Profile, Device Id:\r\n {0}"
, videoDeviceId));
await LogStatusToOutputBox(
"Retrieving all Video Profiles"
);
IReadOnlyList<MediaCaptureVideoProfile> profiles = MediaCapture.FindAllVideoProfiles(videoDeviceId);
await LogStatusToOutputBox(
string
.Format(CultureInfo.InvariantCulture,
"Number of Video Profiles found: {0}"
, profiles.Count));
foreach
(var profile
in
profiles)
{
var description = profile.SupportedRecordMediaDescription;
foreach
(var desc
in
description)
{
await LogStatusToOutputBox(
string
.Format(CultureInfo.InvariantCulture,
"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()));
}
}
await LogStatusToOutputBox(
"Querying for matching WVGA 30FPS Profile"
);
var match = (from profile
in
profiles
from desc
in
profile.SupportedRecordMediaDescription
where desc.Width == 640 && desc.Height == 480 && Math.Round(desc.FrameRate) == 30
select
new
{ profile, desc }).FirstOrDefault();
if
(match !=
null
)
{
mediaInitSettings.VideoProfile = match.profile;
mediaInitSettings.RecordMediaDescription = match.desc;
await LogStatus(
string
.Format(CultureInfo.InvariantCulture,
"Matching WVGA 30FPS Video Profile found: \r\n Profile Id: {0}\r\n Width: {1} Height: {2} FrameRate: {3}"
,
mediaInitSettings.VideoProfile.Id, mediaInitSettings.RecordMediaDescription.Width,
mediaInitSettings.RecordMediaDescription.Height, Math.Round(mediaInitSettings.RecordMediaDescription.FrameRate).ToString()), NotifyType.StatusMessage);
}
else
{
mediaInitSettings.VideoProfile = profiles[0];
await LogStatus(
"Could not locate a matching profile, setting to default recording profile"
, NotifyType.ErrorMessage);
}
await mediaCapture.InitializeAsync(mediaInitSettings);
await LogStatusToOutputBox(
"Media Capture settings initialized to selected profile"
);
}
private
async
void
InitCustomProfileBtn_Click(
object
sender, RoutedEventArgs e)
{
MediaCapture mediaCapture =
new
MediaCapture();
MediaCaptureInitializationSettings mediaInitSettings =
new
MediaCaptureInitializationSettings();
string
videoDeviceId =
string
.Empty;
string
customProfileId =
"{A0E517E8-8F8C-4F6F-9A57-46FC2F647EC0},0"
;
videoDeviceId = await GetVideoProfileSupportedDeviceIdAsync(Windows.Devices.Enumeration.Panel.Back);
if
(
string
.IsNullOrEmpty(videoDeviceId))
{
await LogStatus(
"ERROR: No Video Device Id found, verify your device supports profiles"
, NotifyType.ErrorMessage);
return
;
}
await LogStatusToOutputBox(
string
.Format(CultureInfo.InvariantCulture,
"Found video capture device that supports Video Profile, Device Id:\r\n {0}"
, videoDeviceId));
await LogStatusToOutputBox(
"Querying device for custom profile support"
);
mediaInitSettings.VideoProfile = (from profile
in
MediaCapture.FindAllVideoProfiles(videoDeviceId)
where profile.Id == customProfileId
select profile).FirstOrDefault();
if
(mediaInitSettings.VideoProfile ==
null
)
{
await LogStatus(
"Could not locate a matching profile, setting to default recording profile"
, NotifyType.ErrorMessage);
mediaInitSettings.VideoProfile = MediaCapture.FindAllVideoProfiles(videoDeviceId).FirstOrDefault();
}
await LogStatus(
string
.Format(CultureInfo.InvariantCulture,
"Custom recording profile located: {0}"
, customProfileId), NotifyType.StatusMessage);
await mediaCapture.InitializeAsync(mediaInitSettings);
}
public
async Task<
string
> GetVideoProfileSupportedDeviceIdAsync(Windows.Devices.Enumeration.Panel panel)
{
string
deviceId =
string
.Empty;
await LogStatusToOutputBox(
"Looking for all video capture devices"
);
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
await LogStatusToOutputBox(
string
.Format(CultureInfo.InvariantCulture,
"Number of video capture devices found: {0}"
, devices.Count.ToString()));
foreach
(var device
in
devices)
{
if
(MediaCapture.IsVideoProfileSupported(device.Id) && device.EnclosureLocation.Panel == panel)
{
deviceId = device.Id;
break
;
}
}
return
deviceId;
}
private
async Task LogStatusToOutputBox(
string
message)
{
await
this
.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
outputBox.Text += message +
"\r\n"
;
outputScrollViewer.ChangeView(0, outputBox.ActualHeight, 1);
});
}
private
async Task LogStatus(
string
message, NotifyType type)
{
await
this
.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
rootPage.NotifyUser(message, type);
});
await LogStatusToOutputBox(message);
}
}
}
//Scenario2.cs
namespace
CameraProfile
{
using
System;
using
System.Globalization;
using
System.Linq;
using
System.Threading.Tasks;
using
Windows.Devices.Enumeration;
using
Windows.Media.Capture;
using
Windows.UI.Xaml.Controls;
using
Windows.UI.Xaml.Navigation;
using
SDKTemplate;
public
sealed
partial
class
Scenario2_ConcurrentProfile : Page
{
private
MainPage rootPage;
public
Scenario2_ConcurrentProfile()
{
this
.InitializeComponent();
}
protected
override
void
OnNavigatedTo(NavigationEventArgs e)
{
rootPage = MainPage.Current;
}
private
async
void
CheckConcurrentProfileBtn_Click(
object
sender, Windows.UI.Xaml.RoutedEventArgs e)
{
bool
concurrencySupported =
false
;
string
frontVideoDeviceId =
string
.Empty;
string
backVideoDeviceId =
string
.Empty;
MediaCapture mediaCaptureFront =
new
MediaCapture();
MediaCapture mediaCaptureBack =
new
MediaCapture();
await LogStatusToOutputBox(
"Looking for all video capture devices on front panel"
);
frontVideoDeviceId = await GetVideoProfileSupportedDeviceIdAsync(Windows.Devices.Enumeration.Panel.Front);
await LogStatusToOutputBox(
"Looking for all video capture devices on back panel"
);
backVideoDeviceId = await GetVideoProfileSupportedDeviceIdAsync(Windows.Devices.Enumeration.Panel.Back);
if
(
string
.IsNullOrEmpty(frontVideoDeviceId) ||
string
.IsNullOrEmpty(backVideoDeviceId))
{
await LogStatus(
"ERROR: A capture device doesn't support Video Profile"
, NotifyType.ErrorMessage);
return
;
}
await LogStatusToOutputBox(
"Video Profiles are supported on both cameras"
);
MediaCaptureInitializationSettings mediaInitSettingsFront =
new
MediaCaptureInitializationSettings();
MediaCaptureInitializationSettings mediaInitSettingsBack =
new
MediaCaptureInitializationSettings();
mediaInitSettingsFront.VideoDeviceId = frontVideoDeviceId;
mediaInitSettingsBack.VideoDeviceId = backVideoDeviceId;
await LogStatusToOutputBox(
string
.Format(CultureInfo.InvariantCulture,
"Front Device Id located: {0}"
, frontVideoDeviceId.ToString()));
await LogStatusToOutputBox(
string
.Format(CultureInfo.InvariantCulture,
"Back Device Id located: {0}"
, backVideoDeviceId.ToString()));
await LogStatusToOutputBox(
"Querying for concurrent profile"
);
var concurrentProfile = (from frontProfile
in
MediaCapture.FindConcurrentProfiles(frontVideoDeviceId)
from backProfile
in
MediaCapture.FindConcurrentProfiles(backVideoDeviceId)
where frontProfile.Id == backProfile.Id
select
new
{ frontProfile, backProfile }).FirstOrDefault();
if
(concurrentProfile !=
null
)
{
mediaInitSettingsFront.VideoProfile = concurrentProfile.frontProfile;
mediaInitSettingsBack.VideoProfile = concurrentProfile.backProfile;
concurrencySupported =
true
;
await LogStatus(
"Concurrent profile located, device supports concurrency"
, NotifyType.StatusMessage);
}
else
{
mediaInitSettingsFront.VideoProfile =
null
;
mediaInitSettingsBack.VideoProfile =
null
;
await LogStatus(
"No Concurrent profiles located, device does not support concurrency"
, NotifyType.ErrorMessage);
}
await LogStatusToOutputBox(
"Initializing Front camera settings"
);
await mediaCaptureFront.InitializeAsync(mediaInitSettingsFront);
if
(concurrencySupported)
{
await LogStatusToOutputBox(
"Device supports concurrency, initializing Back camera settings"
);
await mediaCaptureBack.InitializeAsync(mediaInitSettingsBack);
}
}
public
async Task<
string
> GetVideoProfileSupportedDeviceIdAsync(Windows.Devices.Enumeration.Panel panel)
{
string
deviceId =
string
.Empty;
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
await LogStatusToOutputBox(
string
.Format(CultureInfo.InvariantCulture,
"Number of video capture devices found: {0}"
, devices.Count.ToString()));
foreach
(var device
in
devices)
{
if
(MediaCapture.IsVideoProfileSupported(device.Id) && device.EnclosureLocation.Panel == panel)
{
deviceId = device.Id;
break
;
}
}
return
deviceId;
}
private
async Task LogStatusToOutputBox(
string
message)
{
await
this
.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
outputBox.Text += message +
"\r\n"
;
outputScrollViewer.ChangeView(0, outputBox.ActualHeight, 1);
});
}
private
async Task LogStatus(
string
message, NotifyType type)
{
await
this
.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
rootPage.NotifyUser(message, type);
});
await LogStatusToOutputBox(message);
}
}
}
//Scenario3.cs
namespace
CameraProfile
{
using
System;
using
System.Collections.Generic;
using
System.Globalization;
using
Windows.Devices.Enumeration;
using
Windows.Media.Capture;
using
Windows.UI.Xaml.Controls;
using
Windows.UI.Xaml.Navigation;
using
SDKTemplate;
using
System.Threading.Tasks;
public
sealed
partial
class
Scenario3_EnableHdrProfile : Page
{
private
MainPage rootPage;
public
Scenario3_EnableHdrProfile()
{
this
.InitializeComponent();
}
protected
override
void
OnNavigatedTo(NavigationEventArgs e)
{
rootPage = MainPage.Current;
}
private
async
void
CheckHdrSupportBtn_Click(
object
sender, Windows.UI.Xaml.RoutedEventArgs e)
{
string
videoDeviceId =
string
.Empty;
bool
HdrVideoSupported =
false
;
MediaCapture mediaCapture =
new
MediaCapture();
MediaCaptureInitializationSettings mediaCaptureInitSetttings =
new
MediaCaptureInitializationSettings();
await LogStatusToOutputBox(
"Querying for video capture device on back of the device that supports Video Profile"
);
videoDeviceId = await GetVideoProfileSupportedDeviceIdAsync(Windows.Devices.Enumeration.Panel.Back);
if
(
string
.IsNullOrEmpty(videoDeviceId))
{
await LogStatus(
"ERROR: No Video Device Id found, verify your device supports profiles"
, NotifyType.ErrorMessage);
return
;
}
await LogStatusToOutputBox(
string
.Format(CultureInfo.InvariantCulture,
"Found device that supports Video Profile, Device Id:\r\n {0}"
, videoDeviceId));
IReadOnlyList<MediaCaptureVideoProfile> profiles = MediaCapture.FindKnownVideoProfiles(videoDeviceId, KnownVideoProfile.VideoRecording);
foreach
(MediaCaptureVideoProfile profile
in
profiles)
{
IReadOnlyList<MediaCaptureVideoProfileMediaDescription> recordMediaDescription = profile.SupportedRecordMediaDescription;
foreach
(MediaCaptureVideoProfileMediaDescription videoProfileMediaDescription
in
recordMediaDescription)
{
if
(videoProfileMediaDescription.IsHdrVideoSupported)
{
mediaCaptureInitSetttings.VideoProfile = profile;
mediaCaptureInitSetttings.RecordMediaDescription = videoProfileMediaDescription;
HdrVideoSupported =
true
;
await LogStatus(
"HDR supported video profile found, set video profile to current HDR supported profile"
, NotifyType.StatusMessage);
break
;
}
}
if
(HdrVideoSupported)
{
break
;
}
}
await LogStatusToOutputBox(
"Initializing Media settings to HDR Supported video profile"
);
await mediaCapture.InitializeAsync(mediaCaptureInitSetttings);
if
(HdrVideoSupported)
{
await LogStatusToOutputBox(
"Initializing HDR Video Mode to Auto"
);
mediaCapture.VideoDeviceController.HdrVideoControl.Mode = Windows.Media.Devices.HdrVideoMode.Auto;
}
}
public
async Task<
string
> GetVideoProfileSupportedDeviceIdAsync(Windows.Devices.Enumeration.Panel panel)
{
string
deviceId =
string
.Empty;
await LogStatusToOutputBox(
"Looking for all video capture devices"
);
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
await LogStatusToOutputBox(
string
.Format(CultureInfo.InvariantCulture,
"Number of video capture devices found: {0}"
, devices.Count.ToString()));
foreach
(var device
in
devices)
{
if
(MediaCapture.IsVideoProfileSupported(device.Id) && device.EnclosureLocation.Panel == panel)
{
deviceId = device.Id;
break
;
}
}
return
deviceId;
}
private
async Task LogStatusToOutputBox(
string
message)
{
await
this
.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
outputBox.Text += message +
"\r\n"
;
outputScrollViewer.ChangeView(0, outputBox.ActualHeight, 1);
});
}
private
async Task LogStatus(
string
message, NotifyType type)
{
await
this
.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
rootPage.NotifyUser(message, type);
});
await LogStatusToOutputBox(message);
}
}
}
camera
profile
UWP
Windows