您的位置:首页 > 其它

How To Scan QRCode For UWP (3)

2017-08-02 11:47 357 查看
这一节主要介绍如何去设置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 = 1920;
uint desiredHeight = 1080;

private async Task<uint[]> SetResolution(MediaStreamType streamType)
{
//Get the supported encoding properties.
var mediaStreamProperties = mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(streamType);
if (mediaStreamProperties == null || mediaStreamProperties.Count == 0)
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 != 0) ? (width / (double)heiht) : double.NaN, 2);
}


另外我决定采用的 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[0];
imageEnCodingProperties.Height = resolution[1];
}
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来实现扫描二维码的功能。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: