How To Scan QRCode For UWP (3)
这一节主要介绍如何去设置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)的更多相关文章
- How To Scan QRCode For UWP (4)
QR Code的全称是Quick Response Code,中文翻译为快速响应矩阵图码,有关它的简介可以查看维基百科. 我准备使用ZXing.Net来实现扫描二维码的功能,ZXing.Net在Cod ...
- How To Scan QRCode For UWP (2)
这篇随笔主要介绍照相预览功能,重要使用的是MediaCapture对象,MediaCapture对象还可以用来处理录音和录制视频,本文只讨论照相功能. 1:查找摄像头 后置摄像头优先,找不到后置摄像头 ...
- How To Scan QRCode For UWP (1)
本文将介绍实现一个类似于微信扫一扫功能的UI界面,后续会再实现具体的识别二维码的功能. 实例使用的Win10 SDK Version是Windows 10 Anniversary Edition(10 ...
- SWIFT Scan QRCode
SWIFT中扫描QRCode代码如下,照着敲一次再看下API的注释应该就没问题了. import UIKit import Foundation import AVFoundation class V ...
- Python生成二维码脚本
简单的记录下二维码生成和解析的Python代码 依赖下面三个包: PIL(图像处理包,安装:pip install PIL) qrcode(二维码生成包,安装:pip install qrcode) ...
- 初涉扫码登录:edusoho实现客户端扫码登录(简版)
一.项目简介及需求 edusoho是一套商业版的在线教育平台,项目本身基于symfony2框架开发,现在有一款自己的APP,要求在不多修改edusoho自身代码的基础上,实现客户端对PC端扫码登录.不 ...
- 一次使用Python连接数据库生成二维码并安装为windows服务的工作任务
最近有一个需求,在现有生产系统上的人员库中增加一个此人员关键信息的二维码,支持文字版和跳转版两种方式,与报表工具关联,可打印.以windows服务方式,定时检查,只要发现某人员没有此二维码信息,就生成 ...
- python库使用整理
1. 环境搭建 l Python安装包:www.python.org l Microsoft Visual C++ Compiler for Python l pip(get-pip.py):p ...
- AppCan移动应用开发平台新增9个超有用插件(内含演示样例代码)
使用AppCan平台进行移动开发.你所须要具备的是Html5+CSS +JS前端语言基础.此外.Hybrid混合模式应用还需结合原生语言对功能模块进行封装,对于没有原生基础的开发人员,怎样实现App里 ...
随机推荐
- 第11章:MongoDB-CRUD操作--文档--查询
①语法 db.collection.find(query, projection) ②参数 query :可选,使用查询操作符指定查询条件 projection :可选,使用投影操作符指定返回的键.查 ...
- 构造函数的prototype和constructor属性
Car.prototype = { name:'BMW', height:1400, long:4900 } function Car(color,owner){ this.color = color ...
- 使用jstl的Foreach 和jquery的each()的index属性
最近项目中用到隔行换色问题,使用到了jstl的foreach和jquery的each进行遍历. 首先jstl技术.除了常用的items,var外,还有一个下标属性varStatus,从0开始,使用起来 ...
- Linux学习(1)- TCP/IP网络协议基础
Linux学习(1)- TCP/IP网络协议基础 一.TCP/IP 简介 学习内容 TCP/IP(Transmission Control Protocol/Internet Protocol)是传输 ...
- 一些js在线引用文档
1.jquery在线引用: <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script& ...
- 16-margin的用法
margin塌陷问题 当时说到了盒模型,盒模型包含着margin,为什么要在这里说margin呢?因为元素和元素在垂直方向上margin里面有坑. 我们来看一个例子: html结构: <div ...
- C++调用ocx
1.保证ocx已正常注册,可以使用 2.创建一个C++的命令行程序,在主程序#import "HZ_KevinTest.ocx" no_namespace 生成一次程序,debug ...
- text 文本。
//支持转义符"\". <text/> 组件内只支持 <text/> 嵌套. //除了文本节点以外的其他节点都无法长按选中. <view clas ...
- 【python-pip】pip安装国外插件timeout解决方案
为什么会timeout呢,你懂得(不懂???!!!) 解决方案: 使用国内豆瓣镜像安装(也有其他的镜像,自己可以搜) pip install xxx -i http://pypi.douban.com ...
- list列表常用操作
1.创建列表.只要把逗号分隔的不同的数据项使用方括号括起来即可 List = ['wade','james','bosh','haslem'] 2.使用 range() 创建数字列表 numbers ...