I'm new in Windows Phone 8.1 development. I'm doing a simple app to scan barcode using ZXing.Net and Windows Phone 8.1 SDK.
When I call the Decode method I got an exception with text of "Index was outside the bounds of the array"
Here is a portion of code:
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";
}
I think the problem is with the size of the WritableImage, because when I run the app on the emulator (and definitely there is no barcode in it), the decoder decodes and returns no value (and that's ok), but when I run it on my WP8.1 device, I got an exception with a text of: "Index was outside the bounds of the array"
I tried to resize the writable image with no results! but maybe I'm resizing a bad way or values. Any help with that? Thanks.
You will need to emulate this action:
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);
first pull the image out of the stream then do it again with the PixelWidth found on the actual image.