Windows Phone 8.1 RT的Zxing.net实现:CapturePhotoToStreamAsync问题

3

我正在使用ZXing.net为Windows Phone 8.1 RT应用程序创建一个用户控件,使用相机扫描条形码。

条形码已经被解码,但是当调用CapturePhotoToStreamAsync方法时,即使已经使用await等待,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);

我该怎样防止用户界面冻结?
2个回答

1
幸运的是,在Windows Phone 8.1 Runtime上解码QRCode / Barcode时,您不必捕获照片。这实际上是一个相当新的解决方案,但它确实有效:https://github.com/mmaitre314/VideoEffect#realtime-video-analysis-and-qr-code-detection。 安装nuget软件包后,您可以轻松实时解码条形码,而无需调用CapturePhotoToStreamAsync。唯一的缺点是您只能针对ARM进行目标设置。您可以在该网站上找到示例代码。或者您可以联系我,我可以向您发送我使用此功能的项目部分。

这似乎是一个不错的解决方案。我添加了一个按钮来拍照然后进行分析,而不是实时扫描。但我会尝试你的解决方案。不过,SDK只适用于微软/诺基亚手机还是其他品牌(宏碁、阿尔卡特一触即发等)? - Gouar
目前它只支持微软/诺基亚手机。但我认为这并不是问题。截至2013年,诺基亚占据了Windows Phone市场的90%(http://www.theinquirer.net/inquirer/news/2309660/nokia-accounts-for-90-percent-of-the-windows-phone-market)。而且我认为自那时以来并没有发生太大变化。因此,您仍将支持大多数设备。 - WPMed
1
不是这样的。Lumia Imaging SDK 在任何 Windows Phone(8.0、8.1)、任何 Windows RT 平板电脑或 Windows PC(8.1 通用应用程序目标)上都可以正常工作。这只是品牌问题。 - CÅdahl
换句话说,ARM/x86/x64都得到支持。如果你创建了一个通用应用程序(你应该这样做),你可以轻松地覆盖Windows市场。 - CÅdahl
这个能否垂直排列? - garenyondem
显示剩余2条评论

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;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接