ZXing.QrCode in VB .net - troubles to start :(

Nov 18, 2013 at 6:12 PM
Dear all,
I have a troubles to start with ZXing library usage
Please, if someone could check my piece of code...
Or if you can past some example in VB .NET ...
(basically I`m stopped on convert of Bitmap to Byte array , as it seems...
result of this is index out of range in zxing.dll

Thanks a lot

code:
Imports ZXing
Imports ZXing.QrCode


Public Class Form1
    Public Shared Function ConvertToByteArray(ByVal value As Bitmap) As Byte()
        Dim bitmapBytes As Byte()
        Using stream As New System.IO.MemoryStream
            value.Save(stream, value.RawFormat)
            bitmapBytes = stream.ToArray
        End Using
        Return bitmapBytes

    End Function



    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim readCode As New ZXing.QrCode.QRCodeReader()

        Dim imageFile As New Bitmap("C:\qr2.jpg")
        Dim Lumin As New RGBLuminanceSource(ConvertToByteArray(imageFile), imageFile.Width, imageFile.Height)
        Dim HBin As New ZXing.Common.HybridBinarizer(Lumin)
        Dim Bitm As New ZXing.BinaryBitmap(HBin)
        Dim result = readCode.decode(Bitm)

    End Sub
End Class
Nov 18, 2013 at 8:19 PM
Edited Nov 18, 2013 at 8:19 PM
Guys,
sorry I`m replying to own post first, but I solved this:
        Dim readCode As New ZXing.QrCode.QRCodeReader()
        Dim bitmap As New Bitmap("C:\qr2.jpg")
        Dim luminanceSource = New ZXing.BitmapLuminanceSource(Bitmap)
        Dim binarizer = New ZXing.Common.HybridBinarizer(LuminanceSource)

        Dim mapa As New ZXing.BinaryBitmap(binarizer)
        Dim result = readCode.decode(mapa)

        txtCode.Text = CStr(result.Text)
So this is working...BUT:
I have troubles with encode :(

Why is this not working ?
 Dim dekoder As New ZXing.QrCode.QRCodeWriter()

        Dim vysledek = dekoder.encode(txtCode.Text, BarcodeFormat.QR_CODE, 50, 50)
        picBox.Image = vysledek.ToBitmap
result: found empty contents

Do I have to draw image point by point ? or how to 'easily' convert BitMatrix to Bitmap which should
be loaded to picture box ?

I really hard search google, but without any success

Thnaks
Coordinator
Nov 19, 2013 at 12:59 PM
First, some easier code for decoding:
Dim readCode As New ZXing.BarcodeReader()
Dim bitmap As New Bitmap("C:\qr2.jpg")
Dim result = readCode.Decode(bitmap)

txtCode.Text = CStr(result.Text)
Second, a snippet for encoding (untestet, but should work):
Dim encoder As New ZXing.BarcodeWriter()
encoder.Format = BarcodeFormat.QR_CODE
encoder.Options.Height = 50
encoder.Options.Width = 50
Dim vysledek = encoder.Write(txtCode.Text)
picBox.Image = vysledek
Make sure, that txtCode.Text isn't empty.
Nov 19, 2013 at 3:06 PM
oou ;) so pretty simple ?
At first THANKS a lot, it is working properly, but:
I dont understand now why I have QRCodeWriter/Reader class, when
BarcodeWriter/Reader class is working well for QRCode...

Thanks
Coordinator
Nov 19, 2013 at 3:53 PM
Explanation is very easy :)
The QRCodeWriter and reader classes (and all the others for the other barcode formats) are the original classes which were ported from java.
The BarcodeReader and BarcodeWriter classes are on top of them. They exist only in ZXing.Net not in the java original. They were written to provide a simpler
api to the user.
Jan 9, 2014 at 9:26 PM
Edited Jan 9, 2014 at 11:06 PM
I tried this exact code and I get error : Object reference not set to an instance of an object.


Imports ZXing

Public Class Form1
Private Sub btnScan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScan.Click
    Dim reader As New ZXing.BarcodeReader()

    Dim imagevar As New Bitmap("C:\DSC03552.JPG")

    Dim result = reader.Decode(imagevar)

    txtCode.Text = CStr(result.Text)
End Sub

End Class


The line that has the error is "txtCode.Text = CStr(result.Text)"

I'm using VS 2010 Express

Any reason why?
Thanks!

PS. I think it may be somehow returning NULL results, but I tried the same file in the demo exe and it will read the barcode just fine without issue. So the file should not be the issue.
Coordinator
Jan 10, 2014 at 6:59 AM
The WinForms demo uses some more options like TryHarder and AutoRotate. The result can be different to this sample code.
Anyway, before using "result" it has to be checked against null.
Jan 10, 2014 at 9:31 PM
micjahn,

It works now. Try harder turned on did the trick. Thank you kind sir!