Reputation: 12476
I'm building a Windows Phone app (using Windows Runtime, it's a universal app) that needs to be able to scan QR codes. I'm using ZXing.NET for that. The issue I have is as follows: when the camera starts capturing, an IndexOutOfRangeException is thrown by ZXing:
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()
The code I'm using is:
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);
}
}
And the ScanBitmap function looks like:
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;
}
Full source code can be found here: http://pastebin.com/w90w0b3z
I don't have this issue when I capture the image to the file system and then read it (instead of capturing the image to a memory stream), but that is extremely slow and makes the user interface completely unresponsive for seconds. It also requires an unnecessary permission to access the photo albums.
Does anyone know how I can get this working? I found this thread, but I don't understand the solution: http://zxingnet.codeplex.com/discussions/570173. I also found another sample that uses the Nokia Imaging SDK, but that doesn't work for me either.
Upvotes: 1
Views: 1204
Reputation: 12476
So, I found a working solution thanks to http://www.soulier.ch/?p=2464&lang=en. It turned out that the ImageEncodingProperties
that goes into _mediaCapture.CapturePhotoToStreamAsync
needed a width and a heigth. I set the width to 400 and the height to 600. I also used the same width + height for the WriteableBitmap
. It's working fine now! (albeit still a little bit slow)
Upvotes: 5