参考:wp8.1之拍照(获取焦点,使用后置摄像头)

uwp开启摄像头要借助CaptureElement呈现来自捕获设备(如照相机或网络摄像机)的流。今天讲讲如何打开摄像头,获取焦点,以及拍照。废话不多说,下面直接上代码。当然前提是一定要记住在appxmanifest文件Capabilities选项选择Webcam,不然会报错

首先 XAML代码:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<CaptureElement x:Name="capturePreview" Stretch="UniformToFill" />
<Image Name="ProfilePic" />
</Grid>
<StackPanel VerticalAlignment="Bottom">
<Slider
x:Name="FocusValueSlider"
Margin="12,0,15,0"
Header="焦点调节:"
LargeChange="25"
Maximum="1000"
Minimum="0"
SmallChange="1"
ValueChanged="FocusValueSlider_ValueChanged"
Value="500" />
<StackPanel Orientation="Horizontal">
<Button Click="PhotographButton_Click" Content="启动摄像头" />
<Button
Margin="50,0,0,0"
Click="CapturePhoto_Click"
Content="拍照" />
</StackPanel>
</StackPanel>
</Grid>
    //启动摄像头
async private void PhotographButton_Click(object sender, RoutedEventArgs e)
{
if (captureManager == null)
{
capturePreview.Visibility = Visibility.Visible;
ProfilePic.Visibility = Visibility.Collapsed;
captureManager = new MediaCapture(); //选择后置摄像头
var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Back);
if (cameraDevice == null)
{
System.Diagnostics.Debug.WriteLine("No camera device found!");
return;
}
var settings = new MediaCaptureInitializationSettings
{
StreamingCaptureMode = StreamingCaptureMode.Video,
//MediaCategory = MediaCategory.Other,
//AudioProcessing = AudioProcessing.Default,
//PhotoCaptureSource = PhotoCaptureSource.Photo,
AudioDeviceId = string.Empty,
VideoDeviceId = cameraDevice.Id
};
await captureManager.InitializeAsync(settings);
//摄像头旋转90度
//captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
capturePreview.Source = captureManager;
await captureManager.StartPreviewAsync();
}
}
private static async Task<DeviceInformation> FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel desired)
{ var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); DeviceInformation desiredDevice = allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desired); return desiredDevice ?? allVideoDevices.FirstOrDefault();
}
//拍照
async private void CapturePhoto_Click(object sender, RoutedEventArgs e)
{ if (captureManager != null)
{
capturePreview.Visibility = Visibility.Collapsed;
ProfilePic.Visibility = Visibility.Visible;
ProfilePic.Source = null;
//declare string for filename
string captureFileName = string.Empty; //图片格式
ImageEncodingProperties format = ImageEncodingProperties.CreateJpeg(); //创建本地存储文件夹
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(
"Photo"+DateTime.Now.ToString("yyMMddHHmmss")+".jpg",
CreationCollisionOption.ReplaceExisting); await captureManager.CapturePhotoToStorageFileAsync(format, file); BitmapImage bmpImage = new BitmapImage(new Uri(file.Path)); ProfilePic.Source = bmpImage;//释放摄像头资源
capturePreview.Visibility = Visibility.Visible;
ProfilePic.Visibility = Visibility.Collapsed;
//captureManager.Dispose();
//captureManager = null;
} }
private void FocusValueSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
try
{ uint focus = Convert.ToUInt32(e.NewValue);
SetFocus(focus);
}
catch
{ }
}
//设置摄像头焦点方法
private async void SetFocus(uint? focusValue = null)
{ try
{ if (!focusValue.HasValue)
{
focusValue = ;
} if (captureManager.VideoDeviceController.FocusControl.Supported)
{ captureManager.VideoDeviceController.FlashControl.AssistantLightEnabled = false; captureManager.VideoDeviceController.FocusControl.Configure(new FocusSettings() { Mode = FocusMode.Manual, Value = focusValue, DisableDriverFallback = true }); await captureManager.VideoDeviceController.FocusControl.FocusAsync();
}
}
catch { }
}
05-26 12:05