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

AccessViolationException in Zxing for Winrt

Jun 25, 2013 at 9:53 AM
Hi,
I'm using Zxing to read barcode from a bitmap image in winrt,
 Dim bitmapImage As New WriteableBitmap(1000, 500)
                Using fileStream As IRandomAccessStream = Await file.OpenAsync(FileAccessMode.Read)
                    bitmapImage.SetSource(fileStream)
                End Using
                Dim reader As ZXing.BarcodeReader = New ZXing.BarcodeReader
                Dim result = reader.Decode(bitmapImage)
                code = result.Text
But i get an AccessViolationException when i use the Decode method, i can't find any solution, so i would love some tips on how to resolve this issue
Coordinator
Jun 25, 2013 at 9:15 PM
You didn't post a stack trace which would really help to get an answer. But I think I know what's wrong.
There is a problem with WriteableBitmap. It is not fully initialized before calling the method Decode.
Please try the following:
// initialize with 1,1 to get the current size of the image
Dim bitmapImage As New WriteableBitmap(1, 1)
Using fileStream As IRandomAccessStream = Await file.OpenAsync(FileAccessMode.Read)
    bitmapImage.SetSource(fileStream)
    // and create it again because otherwise the WB isn't fully initialized and decoding
    // results in a IndexOutOfRange/AccessViolationException
    bitmapImage = new WriteableBitmap(bitmapImage.PixelWidth, bitmapImage.PixelHeight);
    stream.Seek(0);
    bitmapImage.SetSource(stream);
End Using
Dim reader As ZXing.BarcodeReader = New ZXing.BarcodeReader
Dim result = reader.Decode(bitmapImage)
code = result.Text
If that shouldn't be the solution there is another discussion about it here
https://zxingnet.codeplex.com/discussions/393332
The main difference to your code snippet is the use of the keyword await.
But it is based upon the project WriteableBitmapEx.
Jun 26, 2013 at 8:40 AM
Thanks Michael ! It works great ;)
Jun 26, 2013 at 8:51 AM
Hi again, i was just wondering, when i try to decode a datamatrix, i get :
object reference not set to an instance.
Is it possible that it's due to the size of the datamatrix? It's pretty small, like a square of 2cm side (max)
Coordinator
Jun 26, 2013 at 9:27 AM
Possible. My problem again, you haven't posted the complete stack trace or a sample picture.
I can't reproduce the error.
Jun 26, 2013 at 1:26 PM
After a fiew try, i think the datamatrix is the issue, it works fine with others. Thank for your time michael
Jun 27, 2013 at 9:40 AM
Edited Jun 27, 2013 at 9:43 AM
Hi again michael, I still have some issue with datamatrix, i hope you can help me out.

Here is my sub
    Private Async Sub CapturePhoto_Click(sender As Object, e As RoutedEventArgs)
        Dim messagedialog As MessageDialog
        Try

            ' Using Windows.Media.Capture.CameraCaptureUI API to capture a photo
            Dim dialog As New Windows.Media.Capture.CameraCaptureUI
            Dim aspectRatio As New Size(16, 9)
            Dim code As String
            Dim NrefLabel As String
            dialog.PhotoSettings.CroppedAspectRatio = aspectRatio

            Dim file As StorageFile = Await dialog.CaptureFileAsync(Windows.Media.Capture.CameraCaptureUIMode.Photo)
            If file IsNot Nothing Then
                Dim bitmapImage As New WriteableBitmap(1000, 500)
                Using fileStream As IRandomAccessStream = Await file.OpenAsync(FileAccessMode.Read)
                    bitmapImage.SetSource(fileStream)
                    bitmapImage = New WriteableBitmap(bitmapImage.PixelWidth, bitmapImage.PixelHeight)
                    fileStream.Seek(0)
                    bitmapImage.SetSource(fileStream)
                End Using

                Dim reader As ZXing.BarcodeReader = New ZXing.BarcodeReader
                Dim result = reader.Decode(bitmapImage)

                If result IsNot Nothing Then
                    code = result.Text
                Else
                    messagedialog = New MessageDialog("Unable to find barcode in image sample")
                End If
                Await file.DeleteAsync()
            Else
                messagedialog = New MessageDialog("No photo captured")
            End If
        Catch ex As Exception
            messagedialog = New MessageDialog(ex.Message)
        End Try
        Await messagedialog.ShowAsync
    End Sub
And here some sample that i use :
http://imageshack.us/content_round.php?page=done&l=img22/2150/jg4y.jpg
http://imageshack.us/content_round.php?page=done&l=img521/6906/h05j.jpg
http://imageshack.us/content_round.php?page=done&l=img854/6109/xtqx.jpg
http://imageshack.us/content_round.php?page=done&l=img29/3176/mmqg.jpg
http://imageshack.us/content_round.php?page=done&l=img713/8656/c6j2.jpg
http://imageshack.us/content_round.php?page=done&l=img809/6228/nrjx.jpg
Coordinator
Jun 27, 2013 at 8:46 PM
I believe, that you mean, that you can't decode the images, right?
Ok, something about the images:
  • don't use images with such a high resolution. They are to blurry at the edges of the codes
  • the images should have a higher contrast
  • the code should be located more in the middle of the image. the library is optimized for using with smartphones. The user does naturally hold the code in the center of the window.
Without some pre-processing you will have no success with decoding:
  • reduce the size by 80 percent. a resolution of 800x600 pixel is enough. That step speeds up the whole detecting and decoding procedure.
  • use a simple algorithm to get a higher contrast
  • design your app in a way that the user centers the barcode in the middle of the screen