• 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,用于以流的形式访问文件。

  • 相关阅读:
    解决Windows 7下IE11无法卸载、无法重新安装,提示安装了更新的IE版本
    [SQL Server] 数据库日志文件自动增长导致连接超时的分析
    DataTable转换为List<T>或者DataRow转换为T
    比较Js的substring、substr和C#的Substring
    .NET(c#)Parameters
    SheetJS保存Excel文件
    SheetJS将table转为Excel
    JS中使用let解决闭包
    Font Awesome图标的粗细
    滚动条样式修改
  • 原文地址:https://www.cnblogs.com/qixue/p/4992264.html
Copyright © 2020-2023  润新知