ZXing.net 解码抛出 IndexOutOfRangeException

ZXing.net decoding throws IndexOutOfRangeException

我正在构建一个需要能够扫描二维码的 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 的示例,但这对我也不起作用。

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