这一节主要介绍如何去设置MediaCapture拍照的分辨率。

MediaCapture 包含一个 VideoDeviceController对象,凭借它可以控制摄像头的很多设置,其中包括设置拍照的分辨率。 首先通过GetAvailableMediaStreamProperties方法来获取设备所支持的 Encoding Properties,要注意的是即使你指定了MediaStreamType为Photo,这个API也会有可能同时返回 ImageEncodingProperties /VideoEncodingProperties对象。 因此我们在比较设备支持的Encoding Properties,需要手动去将它强制转换为 ImageEncodingProperties/VideoEncodingProperties对象。 此外还需要找到宽高比非常接近我们所期望的分辨率,误差范围在0.015之内。示例代码使用的宽高比为16:9,常见的还有4:3。

比较奇怪的IMediaEncodingProperties没有声明Width/Height属性,让代码写的有点啰嗦。

实现代码如下:

  uint desiredWidth = ;
uint desiredHeight = ; private async Task<uint[]> SetResolution(MediaStreamType streamType)
{
//Get the supported encoding properties.
var mediaStreamProperties = mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(streamType);
if (mediaStreamProperties == null || mediaStreamProperties.Count == )
return null; var imageEncodingProperty = mediaStreamProperties.Select(e => e as ImageEncodingProperties)
.Where(e => e != null && e.Width <= desiredWidth
&& e.Height < desiredHeight && IsMatchingRatio(e))
.OrderByDescending(e => e.Width * e.Height)
.FirstOrDefault();
if (imageEncodingProperty != null)
{
await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(streamType, imageEncodingProperty);
return new uint[] { imageEncodingProperty.Width, imageEncodingProperty.Height };
} var videoEncodingProperty = mediaStreamProperties.Select(e => e as VideoEncodingProperties)
.Where(e => e != null && e.Width <= desiredWidth
&& e.Height < desiredHeight && IsMatchingRatio(e))
.OrderByDescending(e => e.Width * e.Height)
.FirstOrDefault();
if (videoEncodingProperty != null)
{
await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(streamType, videoEncodingProperty);
return new uint[] { videoEncodingProperty.Width, videoEncodingProperty.Height };
} return null;
} private bool IsMatchingRatio(ImageEncodingProperties e)
{
double tolerance = 0.015;
return Math.Abs(GetAspectRatio(e.Height, e.Width) - GetAspectRatio(desiredHeight, desiredWidth)) < tolerance;
} private bool IsMatchingRatio(VideoEncodingProperties e)
{
double tolerance = 0.015;
return Math.Abs(GetAspectRatio(e.Height, e.Width) - GetAspectRatio(desiredHeight, desiredWidth)) < tolerance;
} private double GetAspectRatio(uint heiht, uint width)
{
return Math.Round((heiht != ) ? (width / (double)heiht) : double.NaN, );
}

另外我决定采用的 LowLagPhotoCapture 来拍摄照片,可以调用 MediaCapture.PrepareLowLagPhotoCaptureAsync 初始化 LowLagPhotoCapture,初始化成功后就可以得到 LowLagPhotoCapture 对象。
然后使用 CaptureAsync 来捕获低快门滞后照片,拍照成功后得到一个 CapturedPhoto 对象,该对象包含两个 CapturedFrame 对象,其中一个返回的是缩略图,另外一个正是我们需要的。
最后使用 FinishAsync 释放 LowLagPhotoCapture 对象和资源,LowLagPhotoCapture 对象被释放后,再次拍照需要再次初始化。

 private LowLagPhotoCapture lowLagPhotoCapture;
...
// Initialize MediaCapture
try
{
await mediaCapture.InitializeAsync(settings);
var imageEnCodingProperties = ImageEncodingProperties.CreatePng();
var resolution = await SetResolution(MediaStreamType.Photo);
if (resolution != null)
{
imageEnCodingProperties.Width = resolution[];
imageEnCodingProperties.Height = resolution[];
}
lowLagPhotoCapture = await mediaCapture.PrepareLowLagPhotoCaptureAsync(imageEnCodingProperties);
isInitialized = true;
}
catch (UnauthorizedAccessException)
{
await ShowMessage("Denied access to the camera.");
}
catch (Exception ex)
{
await ShowMessage("Exception when init MediaCapture. " + ex.Message);
}
...

下一节将介绍采用ZXing.UWP来实现扫描二维码的功能。

How To Scan QRCode For UWP (3)的更多相关文章

  1. How To Scan QRCode For UWP (4)

    QR Code的全称是Quick Response Code,中文翻译为快速响应矩阵图码,有关它的简介可以查看维基百科. 我准备使用ZXing.Net来实现扫描二维码的功能,ZXing.Net在Cod ...

  2. How To Scan QRCode For UWP (2)

    这篇随笔主要介绍照相预览功能,重要使用的是MediaCapture对象,MediaCapture对象还可以用来处理录音和录制视频,本文只讨论照相功能. 1:查找摄像头 后置摄像头优先,找不到后置摄像头 ...

  3. How To Scan QRCode For UWP (1)

    本文将介绍实现一个类似于微信扫一扫功能的UI界面,后续会再实现具体的识别二维码的功能. 实例使用的Win10 SDK Version是Windows 10 Anniversary Edition(10 ...

  4. SWIFT Scan QRCode

    SWIFT中扫描QRCode代码如下,照着敲一次再看下API的注释应该就没问题了. import UIKit import Foundation import AVFoundation class V ...

  5. Python生成二维码脚本

    简单的记录下二维码生成和解析的Python代码 依赖下面三个包: PIL(图像处理包,安装:pip install PIL) qrcode(二维码生成包,安装:pip install qrcode) ...

  6. 初涉扫码登录:edusoho实现客户端扫码登录(简版)

    一.项目简介及需求 edusoho是一套商业版的在线教育平台,项目本身基于symfony2框架开发,现在有一款自己的APP,要求在不多修改edusoho自身代码的基础上,实现客户端对PC端扫码登录.不 ...

  7. 一次使用Python连接数据库生成二维码并安装为windows服务的工作任务

    最近有一个需求,在现有生产系统上的人员库中增加一个此人员关键信息的二维码,支持文字版和跳转版两种方式,与报表工具关联,可打印.以windows服务方式,定时检查,只要发现某人员没有此二维码信息,就生成 ...

  8. python库使用整理

    1. 环境搭建 l  Python安装包:www.python.org l  Microsoft Visual C++ Compiler for Python l  pip(get-pip.py):p ...

  9. AppCan移动应用开发平台新增9个超有用插件(内含演示样例代码)

    使用AppCan平台进行移动开发.你所须要具备的是Html5+CSS +JS前端语言基础.此外.Hybrid混合模式应用还需结合原生语言对功能模块进行封装,对于没有原生基础的开发人员,怎样实现App里 ...

随机推荐

  1. Linux查看登录到服务的用户,查看用户的操作已经剔掉干坏事的用户的命令

    在工作中,我们有时候会经常的切换用户,有时候会忘记切换到哪个用户了,我们就需要知道当前登录的用户时谁,可以使用: whoami 查看当前登录到系统中的用户有哪些: who 列表中显示,第一列是用户名, ...

  2. AngularJS的select设置默认值

    AngularJS的select设置默认值 在使用Angular时候使用select标签时会遇到绑定数据指定默认显示值可这样实现 <!DOCTYPE html> <html ng-a ...

  3. Oracle 数据库 dbConsole 配置笔记

    Oracle安装后,DBConsole有时安装会失败,这时可以通过以下命令恢复: set oracle_sid=orclemca -repos recreateemca -config dbcontr ...

  4. 为WinEdt自定义XeLatex快捷键

    没有彻底抛弃Windows很重要的一方面原因,WinEdt + Sumatra PDF对LaTeX支持的太好了(自动补全,反向搜索),而且当遇到复杂公式的时候,mathtype也能帮上大忙. 我一直用 ...

  5. Django URLs error: view must be a callable or a list/tuple in the case of include()

    Django 1.10 no longer allows you to specify views as a string (e.g. 'myapp.views.home') in your URL ...

  6. NSString copy && strong

    http://www.cocoachina.com/ios/20150512/11805.html 我们在声明一个NSString属性时,对于其内存相关特性,通常有两种选择(基于ARC环境):stro ...

  7. uva 10918 - Tri Tiling(规律)

    题目链接:uva 10918 - Tri Tiling 题目大意:给出n,计算用1*2的瓷砖有多少种方法铺满3*n的地方. 解题思路:和uva 10359 - Tiling有点相似,不过难度会比较大, ...

  8. shell 命令 -- 漂亮的资源查看命令 htop

    htop 相较top,htop更加直接和美观.

  9. AngularJS 模块及provide

    一.模块 模块是一些功能的集合,如控制器.服务.过滤器.指令等子元素组成的整体. 1.注册和使用 模块相当于是一个注册表,保存着名字和编程元素的对照表,可存入也可取出. angular.module( ...

  10. [正则表达式] PHP 中使用正则表达式收集(2016/01/08 - )

    // 1. 过滤字符串中src 属性为空的img 标签 $filterBack = preg_replace("/<img[^<>]*src\=[\'\"][\' ...