【问题标题】:windows phone 8.1 RT Zxing.net implementation : issue with CapturePhotoToStreamAsyncwindows phone 8.1 RT Zxing.net 实现:CapturePhotoToStreamAsync 问题
【发布时间】:2015-03-25 23:48:23
【问题描述】:

我正在使用 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 冻结?

【问题讨论】:

    标签: c# windows-runtime windows-phone-8.1 zxing


    【解决方案1】:

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

    【讨论】:

    • 这似乎是一个不错的解决方案。我添加了一个按钮来拍照,然后进行分析而不是实时扫描。但我会尝试你的解决方案。但是,SDK 是否仅适用于 MS/Nokia 手机或其他品牌(Acer、Alcatel One Touche 等...)?
    • 目前只支持MS/诺基亚手机。但目前我认为这不是问题。截至 2013 年,诺基亚占据了 Windows Phone 市场的 90% (theinquirer.net/inquirer/news/2309660/…)。而且我认为从那以后它没有太大变化。所以你仍然会支持大多数设备。
    • 不正确。 Lumia Imaging SDK 在任何 Windows Phone(8.0、8.1)和任何 Windows RT 平板电脑或 Windows PC(8.1 通用应用程序目标)上都能正常工作。这只是品牌问题。
    • 也就是说,ARM/x86/x64都支持。如果您创建一个通用应用程序(您应该这样做),您也可以轻松覆盖 Windows 市场。
    • 好吧,不是 Lumia Imaging SDK 将我的 Windows Phone 项目限制在 ARM 上,但是由于我安装了 Video Effect SDK(它依赖于 Lumia Imaging SDK),所以我只能针对 ARM。
    【解决方案2】:

    当我先使用相机拍照(让您专注于条形码所在的正确位置)然后发送图片以进行条形码识别时,我总是能获得更好的效果。

    卡顿是因为您尝试不断检查实时提要中的条形码,这对 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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      • 2016-08-22
      相关资源
      最近更新 更多