Windows phone 8.1 在ZXing.Net解码方法中,“索引超出了数组的界限”

Windows phone 8.1 在ZXing.Net解码方法中,“索引超出了数组的界限”,windows-phone-8.1,barcode,qr-code,zxing,Windows Phone 8.1,Barcode,Qr Code,Zxing,我是Windows Phone 8.1开发的新手。 我正在使用ZXing.Net和WindowsPhone8.1SDK做一个简单的应用程序来扫描条形码 当我调用Decode方法时,我得到一个异常,索引的文本在数组的边界之外 以下是代码的一部分: InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream(); await Camera.CapturePhotoToStreamAsync(ImageEncodingPrope

我是Windows Phone 8.1开发的新手。 我正在使用ZXing.Net和WindowsPhone8.1SDK做一个简单的应用程序来扫描条形码

当我调用Decode方法时,我得到一个异常,索引的文本在数组的边界之外

以下是代码的一部分:

InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
await Camera.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);

stream.Seek(0);

var properties = Camera.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);
var videoEncodingProperties = properties as VideoEncodingProperties;

WriteableBitmap writableImg = new WriteableBitmap((int)videoEncodingProperties.Width, (int)videoEncodingProperties.Height);

await writableImg.SetSourceAsync(stream);

result = barcode.Decode(writableImg); // The exception is here

if (result != null)
{
    debug.Text = result.Text;
}
else
{
    debug.Text = "No results";
}
我认为问题在于可写图像的大小,因为当我在emulator上运行应用程序时,其中肯定没有条形码,解码器解码并返回任何值,这没关系,但当我在我的WP8.1设备上运行它时,我得到了一个异常,文本为: 索引超出了数组的边界

我试图调整可写图像的大小,但没有结果!但也许我调整尺寸的方式或价值观不好。
有什么帮助吗?谢谢。

您需要模拟此操作:

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

首先将图像从流中拉出,然后使用实际图像上的像素宽度再次执行此操作。

感谢@Lyle作为二维码阅读器,该代码是否具有可接受的性能和资源行为?例如,高内存使用率?我的项目主要针对我的业务代码39,我还没有让zxing读取代码39条形码我尝试过此策略,但writeableBmp.PixelWidth、writeableBmp.PixelHeight始终为1,因此,我总是得到索引外部错误…一旦您第一次读取位图流,第二次读取时,它将自动知道值是什么,如果您得到1,则表示您没有从流中读取