In BlackBerry® 6.0 the capability was added for third party developers to programatically create and scan 1D and 2D barcodes. This is handled by a combination of the net.rim.device.api.barcodelib package and the com.google.zxing package. There is a lot of power in the zxing package if you need it, but for now we are going to focus on the two most common use cases: Creating and displaying a QR Code, and scanning a QR code using the camera. Attached to this article is a sample app that will do both of these things. It could be easily modified to handle other barcode formats, should you require. Also, there is another sample app which scans 1D and 2D bar codes.
Creating a Barcode
Generating your own barcode and displaying it on the screen is extremely easy:
What this code block will do is create a QRCode that encodes the contents of a text field on the screen. We can get away with a very low level of error correction, ErrorCorrectionLevel.L, so we will use that to keep the density of the barcode low (You may want to change this value depending on your use case). It will then take the actual ByteMatrix of the code and create a Bitmap from it, which will be displayed on the screen in our bitmap field.
Scanning a Barcode
Scanning a barcode is also pretty easy, but there are a few more steps. First, we have to set up the BarcodeDecoder:
BarcodeDecoder decoder = new BarcodeDecoder(hints);
At its core, a BarcodeDecoder requires a HashTable of hints. These hints tell the decoder what you are looking for. The most important hint is the formats Vector. You can put several different formats in at once, but it will make detecting a barcode MUCH slower. It is recommended to only look for a single type of barcode during a scan if you can help it.
Use of the "DecodeHintType.TRY_HARDER" hint should be avoided whenever possible. This flag tells the system to focus on accuracy for hard to read bar codes, which can help read a bar code in difficult conditions but results in a 10 to 20 time increase in decoding time.
Once our BarcodeDecoder is setup, we can create our BarcodeScanner:
_scanner = new BarcodeScanner(decoder, new MyBarcodeDecoderListener());
MyBarcodeDecoderListener is a simple class implementing the DecoderListener interface. It has one method,
void barcodeDecoded(String rawText)
When you successfully scan a barcode, the scanner stops and this method is fired with the decoded text of the barcode. From there, you would do what you need to. See the attached sample application for how you would launch the Browser using that text as the URL.
With your BarcodeScanner in hand, you can get the viewfinder and VideoControl. To display the viewfinde in full screen you would do something like this:
Once that is accomplished, all you need to do is start the scan via
One thing to watch out for is that once you start the scan, the device will continue scanning until either a barcode is detected or stopScan() is called. If you make the viewfinder full screen, a BlackBerry device user will have the expectation that hitting the back button will stop the scan. For an example of how to do that easily, please see the attached sample applcation.