我正在使用ZXing.net创建一个UserControl,用于使用相机将条形码扫描到Windows Phone 8.1 RT应用程序中。
条形码解码得很好,但是当调用CapturePhotoToStreamAsync方法时,我已经冻结了UI,即使是等待它。 执行大约需要600毫秒。
我正在将应用程序测试到模拟器中。
以下代码以异步方式执行:
// Preview of the camera
await _mediaCapture.InitializeAsync(settings);
VideoCapture.Source = _mediaCapture;
VideoCapture.FlowDirection = Windows.UI.Xaml.FlowDirection.LeftToRight;
await _mediaCapture.StartPreviewAsync();
VideoEncodingProperties res = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
ImageEncodingProperties iep = ImageEncodingProperties.CreateBmp();
iep.Height = res.Height;
iep.Width = res.Width;
var barcodeReader = new BarcodeReader
{
TryHarder = true,
AutoRotate = true
};
WriteableBitmap wB = new WriteableBitmap((int)res.Width, (int)res.Height);
while (_result == null)
{
using (var stream = new InMemoryRandomAccessStream())
{
await _mediaCapture.CapturePhotoToStreamAsync(iep, stream);
stream.Seek(0);
await wB.SetSourceAsync(stream);
_result = barcodeReader.Decode(wB);
}
}
await _mediaCapture.StopPreviewAsync();
//callback to handle result
ScanCallback(_result.Text);
如何防止用户界面冻结?
答案 0 :(得分:1)
幸运的是,您无需在Windows Phone 8.1 Runtime上捕获照片来解码QRCode /条形码。实际上这是一个非常新的解决方案,但它有效:https://github.com/mmaitre314/VideoEffect#realtime-video-analysis-and-qr-code-detection 在使用nuget包后,您可以轻松实时解码条形码,而无需调用CapturePhotoToStreamAsync。唯一的缺点是你只能针对ARM。您可以在网站上找到示例代码。或者您可以联系我,我可以将项目的一部分发送给您。
答案 1 :(得分:0)
当我使用相机首先拍摄照片(让您将焦点放在条形码所在的正确位置)时,我总能得到更好的效果,然后发送照片进行条形码识别。
导致口吃是因为您试图继续检查CPU上的条形码的实时馈送(特别是对于ARM设备)
var dialog = new CameraCaptureUI();
StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
var stream = await file.OpenReadAsync();
// initialize with 1,1 to get the current size of the image
var writeableBmp = new WriteableBitmap(1, 1);
writeableBmp.SetSource(stream);
// and create it again because otherwise the WB isn't fully initialized and decoding
// results in a IndexOutOfRange
writeableBmp = new WriteableBitmap(writeableBmp.PixelWidth, writeableBmp.PixelHeight);
stream.Seek(0);
writeableBmp.SetSource(stream);
var result = ScanBitmap(writeableBmp);
string barcode = "";
if (result != null)
{
barcode = result.Text;
}
这里是ScanBitmap方法:
private Result ScanBitmap(WriteableBitmap writeableBmp)
{
var barcodeReader = new BarcodeReader
{
Options = new ZXing.Common.DecodingOptions()
{
TryHarder = true
},
AutoRotate = true
};
var result = barcodeReader.Decode(writeableBmp);
if (result != null)
{
CaptureImage.Source = writeableBmp;
}
return result;
}