windows phone 8.1 RT Zxing.net 实施:CapturePhotoToStreamAsync 问题
windows phone 8.1 RT Zxing.net implementation : issue with CapturePhotoToStreamAsync
我正在使用 ZXing.net 创建一个用户控件,用于使用相机将条形码扫描到 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);
如何防止 UI 冻结?
当我先用相机拍照(让你对焦在条形码所在的正确位置)然后发送图片进行条形码识别时,我总是能得到更好的结果。
卡顿是因为您尝试不断检查实时提要中的条形码,这在 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;
}
幸运的是,您无需拍摄照片即可在 Windows Phone 8.1 运行时解码 QRCode/Barcode。这实际上是一个全新的解决方案,但它确实有效:https://github.com/mmaitre314/VideoEffect#realtime-video-analysis-and-qr-code-detection
安装 nuget 包后,您可以轻松实时解码条形码,无需调用 CapturePhotoToStreamAsync。唯一的缺点是您只能针对 ARM。您可以在网站上找到示例代码。或者你可以联系我,我可以把我使用它的项目部分发给你。
我正在使用 ZXing.net 创建一个用户控件,用于使用相机将条形码扫描到 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);
如何防止 UI 冻结?
当我先用相机拍照(让你对焦在条形码所在的正确位置)然后发送图片进行条形码识别时,我总是能得到更好的结果。
卡顿是因为您尝试不断检查实时提要中的条形码,这在 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;
}
幸运的是,您无需拍摄照片即可在 Windows Phone 8.1 运行时解码 QRCode/Barcode。这实际上是一个全新的解决方案,但它确实有效:https://github.com/mmaitre314/VideoEffect#realtime-video-analysis-and-qr-code-detection 安装 nuget 包后,您可以轻松实时解码条形码,无需调用 CapturePhotoToStreamAsync。唯一的缺点是您只能针对 ARM。您可以在网站上找到示例代码。或者你可以联系我,我可以把我使用它的项目部分发给你。