这一节主要介绍如何去设置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. redis知识积累

    redis 安装包:redis-3.0.6.tar.gz 打开 redis: cd /media/deepl/文档/ubuntutools/redis-3.0.6/src/ ./redis-serve ...

  2. oracle数据库的迁移(从一台服务器到另一个台服务器,从oracle 10g到oracle 11g)

    这个过程呢,还是蛮艰难的.... 一.最初我使用的是Navicat中的数据传输来迁移的,虽说整个数据库的迁移没有成功,但传输指定的对象时还是传输成功了.所以还是记录一下吧. 1.前提连接好数据库.在指 ...

  3. 本地导入/导出远程oracle数据库

    1.导出数据库 exp 用户名/密码@远程服务器IP:数据端口号/实例名 file=存储dmp文件的路径 full=y; 2.导入数据库 imp 用户名/密码@远程服务器IP:数据库端口号/实例名 f ...

  4. 15-BOM

    BOM的介绍 JavaScript基础分为三个部分: ECMAScript:JavaScript的语法标准.包括变量.表达式.运算符.函数.if语句.for语句等. DOM:文档对象模型,操作网页上的 ...

  5. Jmeter-连接 MySQL数据库

    一.下载mysql驱动包,mysql各个版本驱动包如下: http://central.maven.org/maven2/mysql/mysql-connector-java/ (上面链接的信息来源于 ...

  6. excel函数累加求和与累计百分比应用

    申明:为了方便记忆,该笔记内容纯属拷贝,如与原创雷同,请加我为火山小视频好友:345270311,必将献上好段子已表感谢~ 正传 以下表为例,求公里数的累加求和以及累计百分比. 在D2单元格输入=su ...

  7. android Activity中设置setResult 没有触发onActivityResult

    昨天修改<manifest 文件中activity 的 模式为单例模式:android:launchMode="singleTask" ,发现我的onActivityResu ...

  8. Hdu4135 Co-prime 2017-06-27 16:03 25人阅读 评论(0) 收藏

    Co-prime Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Subm ...

  9. nginx实现unigui群集

    nginx实现unigui群集 在笔者写此文的时候,UNIGUI1.50.x的版本已经发布,其提供的HyperServer已经支持群集. 有网友还专门为此做了群集方面的测试: 从上图可以看出:群集总共 ...

  10. [leet code 4] Median of Two Sorted Arrays

    1 题目 There are two sorted arrays A and B of size m and n respectively. Find the median of the two so ...