M6: 使用摄像头(CameraCaptureUI)

本小节介绍UWP中摄像头的使用,使用CameraCaptureUI来拍照,不仅能够获得图像,还能够对图像进行剪裁 (目前Mobile设备还上不支持)。 在本例中, 单击Camera按钮调用摄像头来拍摄图像, 并使用CameraCaptureUI.PhotoSettings.CroppedSizeInPixels对图像进行剪裁。

MainPage.xaml页面,定位到abtnOpenCamera控件,定义单击事件为Camera_Click

<AppBarButton x:Name="abtnOpenCamera" Icon="Camera" Label="Camera" Click="Camera_Click"/>

然后,用鼠标在Camera_Click中任何位置单击,在键盘上按下F12键,进入Code Behind代码。

使用关键字async将Camera_Click修改为异步方法。

private async void Camera_Click(object sender, RoutedEventArgs e)
{
}

定义CameraCaptureUI对象,设置图像格式为CameraCaptureUIPhotoFormat.Jpeg,设置剪裁大小为400X400,代码如下:

var cameraCaptureUI = new CameraCaptureUI();
cameraCaptureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
cameraCaptureUI.PhotoSettings.AllowCropping = true;
cameraCaptureUI.PhotoSettings.CroppedSizeInPixels = new Size(400, 400);

StorageFile photo = await cameraCaptureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);

然后,通过文件流方式将StorageFile传给BitmapImage,再将BitmapImage传给ImageBrush,进而设置为gridMsg的背景。

if (photo == null)
    return;

BitmapImage bitmapImage = new BitmapImage();
using (IRandomAccessStream stream =await photo.OpenAsync(FileAccessMode.Read))
{
    bitmapImage.SetSource(stream);
}

ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = bitmapImage;
gridMsg.Background = imageBrush;

其中, IRandomAccessStream来自于Windows.Storage.Streams,用于以流的形式访问文件。

原文地址:https://www.cnblogs.com/qixue/p/4992264.html