Read barcodes with newer zxing classes

We had zxing classes in our MBS Xojo Plugins to recognize barcodes for many years. The older zxing library we used got outdated, but there is a successor. The zxing library got branched and we can use the newer versions with a different interface to make new classes and a module for Xojo:

The new module has two methods to take a picture and find barcodes:

Example

Let’s try the new function. First we use our BarcodeGeneratorMBS class to generate a QRCode with some text. Then we use that picture to send it through the decoder:

Var b As New BarcodeGeneratorMBS
b.Symbology = b.BarcodeQrcode
b.Encode "Hello Xojo Developer!"
Dim pic As Picture = b.Picture

Dim result As ZXingReaderResultMBS = ZxingBarcodeMBS.ReadBarcode(pic)

MessageBox result.Text

We get back a ZXingReaderResultMBS object with the results and it contains the text. The result also includes the location of the coordinates for where the barcode was found in the image. You can inspect what format the barcode has, the EC Level or the orientation. You can query the raw bytes of the barcode content and its type, but usually we just ask for the text. A couple of flags tell you whether the library needed to invert or mirror the barcode to detect it properly.

The ReadBarcodes functions does the same as ReadBarcode, but continue to find more barcodes after the first. So if you have a picture taking with multiple barcodes, the function can provide an array of ZXingReaderResultMBS objects.

Options

You can set a couple of options using the ZXingReaderOptionsMBS class. By default we look for all types of barcodes. But you can of course pick which types to use and e.g. use EAN 13 or 8 barcodes with optional add-on symbols like this:

// let's do EAN
Var o As New ZXingReaderOptionsMBS
o.formats = o.BarcodeFormatEAN13 or o.BarcodeFormatEAN8
o.eanAddOnSymbol = o.EanAddOnSymbolRead

Then pass options:

Dim result As ZXingReaderResultMBS = ZxingBarcodeMBS.ReadBarcode(pic, options)

You can set a few more options like whether to validate checksums or try a few more things like rotation, invert or down scale. And you can decide how to convert the image to black and white, what character set you expect and the maximum number of barcodes (symbols) to look for.

Please try the new classes in 24.4 release of MBS Xojo Plugins, currently in testing, but final release is coming soon.

1 Like