Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

How to use the Barcode API

by Retired on ‎08-24-2010 05:29 PM - edited on ‎10-11-2011 04:27 PM by Retired (48,828 Views)

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:



QRCode qrCode = new QRCode();

Encoder.encode(_barcodeTextField.getText(), ErrorCorrectionLevel.L, qrCode);

ByteMatrix barcode = qrCode.getMatrix();

Bitmap bitmap = BarcodeBitmap.createBitmap(barcode, BARCODE_WIDTH);


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:



Hashtable hints = new Hashtable();

Vector formats = new Vector();


hints.put(DecodeHintType.POSSIBLE_FORMATS, formats);

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:



Field viewFinder = _scanner.getViewfinder();



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.

by New Developer
on ‎09-21-2010 04:18 PM

Ther barcode scanning API is not working for ISBN codes at the back of books.


Is there anything special I need to do to get those recognized?

by New Developer
on ‎10-05-2010 03:13 PM

ANy updates on the comment posted above?

by New Developer
on ‎10-14-2010 11:39 AM

I'm having the same problem. The BarcodeScanner will not scan 1D UPC codes. It see them on the screen just fine but it doesn't recognize them. Was this tested by RIM at all?

by Retired
on ‎10-19-2010 11:30 AM

1D barcodes work quite well, but the issue with the ZXing library is that it expects them to be oriented so that they go horizontally across the camera. On the Torch, this means you actually need to hold it in the landscape position (buttons to the right, to be precise). You also need to make sure the barcode is very clearly in focus in the center of the screen, which usually requires the device to be held substantially further back than you would expect.

by Developer
on ‎11-06-2010 12:34 PM

i tried the demo code on the torch it does not work.

nothing is works on all orientation.


I also tried the bar code demo in the SDK and it can't even scan one code.


how do you get this thing to work?

by Retired
on ‎11-09-2010 03:42 PM

Are you attempting to scan a QR Code? This sample is set to only scan that type of barcode. This is what I mean: 


by New Developer
on ‎12-02-2010 04:03 PM

smiley, I have tried the demo code on two separate Torch's.  It works well on one, but the camera on the other one won't focus enough to get a decent image. Could that be your problem as well?

Users Online
Currently online: 10 members 549 guests
Please welcome our newest community members: