首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ZXing.net解码抛出IndexOutOfRangeException

ZXing.net解码抛出IndexOutOfRangeException
EN

Stack Overflow用户
提问于 2015-04-13 15:47:30
回答 1查看 1.1K关注 0票数 1

我正在构建一个Windows应用程序(使用Windows,它是一个通用应用程序),需要能够扫描QR代码。我用的是ZXing.NET。我遇到的问题如下:当相机开始拍摄时,IndexOutOfRangeException会被ZXing抛出:

代码语言:javascript
复制
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()

我使用的代码是:

代码语言:javascript
复制
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函数看起来像:

代码语言:javascript
复制
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。我还找到了另一个使用的示例,但这对我也不起作用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-13 18:14:52

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

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29609616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档