Friday, March 2, 2012

Barcode scan sample example

Some time your application require to scan barcode and display that data on Blackberry Filed. That's why i am planning to write example for scan barcode in blackberry as following example may be save your time. This example run only Blackberry OS 6.0 and and 7.0 to scan barcode on Blackberry OS 5.0 please user this url may help full.

import java.util.Hashtable;
import java.util.Vector;

import net.rim.device.api.barcodelib.BarcodeDecoder;
import net.rim.device.api.barcodelib.BarcodeDecoderListener;
import net.rim.device.api.barcodelib.BarcodeScanner;
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.FullScreen;
import net.rim.device.api.ui.container.MainScreen;


public class BarcodeScanSample extends MainScreen{
 private FullScreen _barcodeScreen;
 private BarcodeScanner _scanner; 
 private LabelField lblBarcodeText;
 private ButtonField btnScan;
 public BarcodeScanSample(String barcodeText){
  lblBarcodeText = new LabelField(barcodeText);
  btnScan = new ButtonField("Scan");
  btnScan.setChangeListener(new FieldChangeListener() {
   public void fieldChanged(Field field, int context) {
 private void scanBarcode() {
  // If we haven't scanned before, we will set up our barcode scanner
  if (_barcodeScreen == null) {

   // First we create a hashtable to hold all of the hints that we can
   // give the API about how we want to scan a barcode to improve speed
   // and accuracy.
   Hashtable hints = new Hashtable();

   // The first thing going in is a list of formats. We could look for
   // more than one at a time, but it's much slower. and set Barcode Format.
   Vector formats = new Vector();
   hints.put(DecodeHintType.POSSIBLE_FORMATS, formats);

   // We will also use the "TRY_HARDER" flag to make sure we get an
   // accurate scan
   hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);

   // We create a new decoder using those hints
   BarcodeDecoder decoder = new BarcodeDecoder(hints);

   // Finally we can create the actual scanner with a decoder and a
   // listener that will handle the data stored in the barcode. We put
   // that in our view screen to handle the display.
   try {
    _scanner = new BarcodeScanner(decoder, new MyBarcodeDecoderListener());
    _barcodeScreen = new MyBarcodeScannerViewScreen(_scanner);

   } catch (Exception e) {
    System.out.println("Could not initialize barcode scanner: " + e);

  // If we get here, all the barcode scanning infrastructure should be set
  // up, so all we have to do is start the scan and display the viewfinder
  try {
  } catch (Exception e) {
   System.out.println("Could not start scan: " + e);

  * MyBarcodeScannerViewScreen

* This view screen is simply an extension of MainScreen that will hold our * scanner's viewfinder, and handle cleanly stopping the scan if the user * decides they want to abort via the back button. * * @author PBernhardt * */ private class MyBarcodeScannerViewScreen extends MainScreen { public MyBarcodeScannerViewScreen(BarcodeScanner scanner) { super(); try { // Get the viewfinder and add it to the screen _scanner.getVideoControl().setDisplayFullScreen(true); Field viewFinder = _scanner.getViewfinder(); this.add(viewFinder); // Create and add our key listener to the screen this.addKeyListener(new MyKeyListener()); } catch (Exception e) { System.out.println("Error creating view screen: " + e); } } /*** * MyKeyListener *

* This KeyListener will stop the current scan cleanly when the back * button is pressed, and then pop the viewfinder off the stack. * * @author PBernhardt * */ private class MyKeyListener implements KeyListener { public boolean keyDown(int keycode, int time) { // First convert the keycode into an actual key event, taking // modifiers into account int key = Keypad.key(keycode); // From there we can compare against the escape key constant. If // we get it, we stop the scan and pop this screen off the stack if (key == Keypad.KEY_ESCAPE) { try { _scanner.stopScan(); } catch (Exception e) { System.out.println("Error stopping scan: " + e); } UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { UiApplication.getUiApplication().popScreen(_barcodeScreen); } }); return true; } // Otherwise, we'll return false so as not to consume the // keyDown event return false; } // We will only act on the keyDown event public boolean keyChar(char key, int status, int time) { return false; } public boolean keyRepeat(int keycode, int time) { return false; } public boolean keyStatus(int keycode, int time) { return false; } public boolean keyUp(int keycode, int time) { return false; } } } /*** * MyBarcodeDecoderListener *

* This BarcodeDecoverListener implementation tries to open any data encoded * in a barcode in the browser. * * @author PBernhardt * **/ private class MyBarcodeDecoderListener implements BarcodeDecoderListener { public void barcodeDecoded(final String rawText) { // First pop the viewfinder screen off of the stack so we can see // the main app UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { UiApplication.getUiApplication().popScreen(_barcodeScreen); } }); _barcodeScreen.invalidate(); //Display this barcode on LabelField on BarcodeScanSample MainScreen we can also set whatever field here. UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { UiApplication.getUiApplication().popScreen(); UiApplication.getUiApplication().pushScreen(new BarcodeScanSample(rawText)); _barcodeScreen.close(); _barcodeScreen=null; } }); } } }

You can test this code on device(terminal) and after scan completed barcode data will display on Label Field.


No comments:

Post a Comment