This project has moved. For the latest updates, please go here.

Reading QR codes / bar codes in windows 8 application using xaml C#

May 9, 2013 at 9:10 AM
Edited May 9, 2013 at 9:11 AM
Hi

I want to read qr codes in windows 8 application, i went through some discussions on different thread but got confused and still was not able to get out.

I use the following code
        var openPicker = new FileOpenPicker { ViewMode = PickerViewMode.Thumbnail, SuggestedStartLocation = PickerLocationId.PicturesLibrary };
        openPicker.FileTypeFilter.Add(".jpg");
        openPicker.FileTypeFilter.Add(".jpeg");
        openPicker.FileTypeFilter.Add(".png");

        StorageFile file = await openPicker.PickSingleFileAsync();
        if (file != null)
        {
            // Application now has read/write access to the picked file

            BitmapImage bmp = new BitmapImage();
            IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
            bmp.SetSource(stream);
            BarCodeImage.Source = bmp;

            IBarcodeReader reader = new BarcodeReader();
            WriteableBitmap barcodeBitmap = new WriteableBitmap(1,1);
            barcodeBitmap.SetSource(stream);
            var result = reader.Decode(barcodeBitmap);
        }
but while loading the result i am getting exception. Please help what should be code for it
Coordinator
May 9, 2013 at 11:11 AM
Please post the exception with stacktrace here. It makes it easier finding the error.
But I think I know what happens.
You can try the following snippet:
var openPicker = new FileOpenPicker { ViewMode = PickerViewMode.Thumbnail, SuggestedStartLocation = PickerLocationId.PicturesLibrary };
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");

StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
    // Application now has read/write access to the picked file

    BitmapImage bmp = new BitmapImage();
    IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
    bmp.SetSource(stream);
    BarCodeImage.Source = bmp;

    IBarcodeReader reader = new BarcodeReader();
    WriteableBitmap barcodeBitmap = new WriteableBitmap(1,1);
    barcodeBitmap.SetSource(stream);
    
    // and create it again because otherwise the WB isn't fully initialized and decoding
    // results in a IndexOutOfRange
    barcodeBitmap = new WriteableBitmap(barcodeBitmap.PixelWidth, barcodeBitmap.PixelHeight);
    stream.Seek(0);
    barcodeBitmap.SetSource(stream);
    
    var result = reader.Decode(barcodeBitmap);
}
May 9, 2013 at 12:19 PM
Edited May 9, 2013 at 12:22 PM
I tried the above piece of code, it throws error again

Message - Index was outside the bounds of the array.

StackTrace :-
at ZXing.BitmapLuminanceSource..ctor(WriteableBitmap writeableBitmap) in c:\Users\michael.jahn\Documents\SVN\ZXing.Net.WINMD\Source\lib\BitmapLuminanceSource.Silverlight.cs:line 56
at ZXing.BarcodeReader.<.cctor>b__3(WriteableBitmap bitmap) in c:\Users\michael.jahn\Documents\SVN\ZXing.Net.WINMD\Source\lib\BarcodeReader.WINMD.cs:line 37
at ZXing.BarcodeReader.Decode(WriteableBitmap barcodeBitmap) in c:\Users\michael.jahn\Documents\SVN\ZXing.Net.WINMD\Source\lib\BarcodeReader.WINMD.cs:line 312
at BarCode_Reader.MainPage.<ButtonBase_OnClick>d__1.MoveNext() in e:\Practice\BarCode Reader\BarCode Reader\MainPage.xaml.cs:line 71


For the previous code
Message - Value cannot be null. Parameter name: source

StackTrace :-
at System.Runtime.InteropServices.Marshal.CopyToManaged(IntPtr source, Object destination, Int32 startIndex, Int32 length)
at System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.CopyTo(IBuffer source, UInt32 sourceIndex, Byte[] destination, Int32 destinationIndex, Int32 count)
at System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer source, UInt32 sourceIndex, Int32 count)
at ZXing.BitmapLuminanceSource..ctor(WriteableBitmap writeableBitmap) in c:\Users\michael.jahn\Documents\SVN\ZXing.Net.WINMD\Source\lib\BitmapLuminanceSource.Silverlight.cs:line 50
at ZXing.BarcodeReader.<.cctor>b__3(WriteableBitmap bitmap) in c:\Users\michael.jahn\Documents\SVN\ZXing.Net.WINMD\Source\lib\BarcodeReader.WINMD.cs:line 37
at ZXing.BarcodeReader.Decode(WriteableBitmap barcodeBitmap) in c:\Users\michael.jahn\Documents\SVN\ZXing.Net.WINMD\Source\lib\BarcodeReader.WINMD.cs:line 312
at BarCode_Reader.MainPage.<ButtonBase_OnClick>d__1.MoveNext() in e:\Practice\BarCode Reader\BarCode Reader\MainPage.xaml.cs:line 64
Coordinator
May 9, 2013 at 7:10 PM
You can try two things:
May 9, 2013 at 8:55 PM
Thanks the demo made my day.