wp8.1之拍照(获取焦点,使用后置摄像头)
wp8.1 没有像wp8一样直接用启动器开启摄像头,他要开启摄像头要借助CaptureElement呈现来自捕获设备(如照相机或网络摄像机)的流。今天讲讲如何打开摄像头,获取焦点,以及拍照。废话不多说,下面直接上代码。当然前提是一定要记住在appxmanifest文件Capabilities选项选择Webcam,不然会报错
首先 XAML代码:
<Grid Name="LayoutRoot"> <CaptureElement x:Name="capturePreview" Stretch="UniformToFill"/> <Image Name="ProfilePic"></Image> </Grid> <StackPanel Grid.Row="1" VerticalAlignment="Bottom"> <Slider x:Name="FocusValueSlider" Maximum="1000" Minimum="0" Grid.Row="0" Margin="12,0,15,0" Header="焦点调节:" ValueChanged="FocusValueSlider_ValueChanged" Value="500" SmallChang e="1" LargeChange="25"></Slider> <StackPanel Orientation="Horizontal"> <Button Content="启动摄像头" Click="PhotographButton_Click" ></Button> <Button Margin="50,0,0,0" Content="拍照" Click="CapturePhoto_Click"></Button> </StackPanel> </StackPanel>
然后是启动摄像头 事件:
MediaCapture captureManager = null; async private void PhotographButton_Click(object sender, RoutedEventArgs e) { if (captureManager == null) { capturePreview.Visibility = Visibility.Visible; ProfilePic.Visibility = Visibility.Collapsed; captureManager = new MediaCapture(); //选择后置摄像头 var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back); await captureManager.InitializeAsync(new MediaCaptureInitializationSettings { StreamingCaptureMode = StreamingCaptureMode.Video, PhotoCaptureSource = PhotoCaptureSource.Photo, AudioDeviceId = string.Empty, VideoDeviceId = cameraID.Id }); //摄像头旋转90度 captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees); capturePreview.Source = captureManager; await captureManager.StartPreviewAsync(); } }
获取后摄像头方法:
private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desired) { DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture)) .FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desired); if (deviceID != null) return deviceID; else throw new Exception(string.Format("Camera of type {0} doesn't exist.", desired)); }
接下来是拍照方法:
async private void CapturePhoto_Click(object sender, RoutedEventArgs e) { if (captureManager != null) { capturePreview.Visibility = Visibility.Collapsed; ProfilePic.Visibility = Visibility.Visible; //declare string for filename string captureFileName = string.Empty; //图片格式 ImageEncodingProperties format = ImageEncodingProperties.CreateJpeg(); //创建本地存储文件夹 StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync( "Photo.jpg", CreationCollisionOption.ReplaceExisting); await captureManager.CapturePhotoToStorageFileAsync(format, file); BitmapImage bmpImage = new BitmapImage(new Uri(file.Path)); ProfilePic.Source = bmpImage;
//释放摄像头资源 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 = 500; } 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 { } }
这样就完成了焦点获取。