【问题标题】:ZXing.net decoding throws IndexOutOfRangeExceptionZXing.net解码抛出IndexOutOfRangeException
【发布时间】:2015-04-14 02:14:52
【问题描述】:

我正在构建一个需要能够扫描 QR 码的 Windows Phone 应用(使用 Windows 运行时,它是一个通用应用)。我正在为此使用 ZXing.NET。我遇到的问题如下:当相机开始捕捉时,ZXing 抛出 IndexOutOfRangeException:

A first chance exception of type 'System.IndexOutOfRangeException' occurred in ZXing.winmd
   at ZXing.BitmapLuminanceSource..ctor(WriteableBitmap writeableBitmap)
   at ZXing.BarcodeReader.<.cctor>b__4(WriteableBitmap bitmap)
   at ZXing.BarcodeReader.Decode(WriteableBitmap barcodeBitmap)
   at xxx.Views.Scanner2.ScanBitmap(WriteableBitmap writeableBmp)
   at xxx.Views.Scanner2.<OnNavigatedTo>d__5.MoveNext()

我使用的代码是:

while (_result == null)
{
    using (var stream = new InMemoryRandomAccessStream())
    {
        await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);   

        stream.Seek(0);

        var writeableBitmap = new WriteableBitmap(1, 1);
        await writeableBitmap.SetSourceAsync(stream);

        _result = ScanBitmap(writeableBitmap);
    }
}

ScanBitmap 函数看起来像:

private Result ScanBitmap(WriteableBitmap writeableBmp)
{
    var barcodeReader = new BarcodeReader
    {
        Options = new DecodingOptions
        {
            PossibleFormats = new[] { BarcodeFormat.QR_CODE },
            TryHarder = true
        },
        AutoRotate = true
    };
    var result = barcodeReader.Decode(writeableBmp);

    if (result != null)
    {
        CaptureImage.Source = writeableBmp;
    }

    return result;
}

完整的源代码可以在这里找到:http://pastebin.com/w90w0b3z

当我将图像捕获到文件系统然后读取它(而不是将图像捕获到内存流)时,我没有这个问题,但这非常慢,并且使用户界面在几秒钟内完全没有响应。它还需要不必要的权限才能访问相册。

有谁知道我怎样才能让它工作?我找到了这个线程,但我不明白解决方案:http://zxingnet.codeplex.com/discussions/570173。我还发现了另一个使用 Nokia Imaging SDK 的示例,但这对我也不起作用。

【问题讨论】:

    标签: windows-runtime windows-phone-8.1 ocr qr-code zxing


    【解决方案1】:

    所以,感谢http://www.soulier.ch/?p=2464&lang=en,我找到了一个可行的解决方案。事实证明,进入_mediaCapture.CapturePhotoToStreamAsyncImageEncodingProperties 需要一个宽度和一个高度。我将宽度设置为 400,将高度设置为 600。我还为 WriteableBitmap 使用了相同的宽度 + 高度。现在工作正常! (虽然还是有点慢)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多