Pages

Wednesday, May 25, 2011

Barcode Scanner for BlackBerry SDK 5.0

Since in BlackBerry SDK 5.0 have no built-in barcode lib API as in BlackBerry SDK 6.0. So I decide to create my own package to provide similar functionality to scan barcode (I'm using QR Code)

I'm using BlackBerry Java Plugin 1.3 Eclipse.
And Simulator of 9300.

You can download the package classes here.

Example of Usage:

        BarcodeScanner scanner;
        BarcodeDecoder decoder;
        BarcodeDecoderListener listener;
        Field viewFinder;

            Hashtable hints = new Hashtable(1);
            Vector formats = new Vector(1);
            formats.addElement(BarcodeFormat.QR_CODE);
            hints.put(DecodeHintType.POSSIBLE_FORMATS, formats);

            decoder = new BarcodeDecoder(hints);

            listener = new BarcodeDecoderListener() {

                public void barcodeFailDecoded(Exception ex) {
                  
                }

                public void barcodeDecoded(String rawText) {

                    synchronized(Application.getEventLock()){
                       barcodeResult.setText(rawText);

                      
                    }
                }

                public void barcodeDecodeProcessFinish() {
                     synchronized(Application.getEventLock()){

                         UiApplication.getUiApplication.
                            popScreen(ViewFinderScreen.this);

                     }
                }
            };
            try {
                scanner = new BarcodeScanner(decoder, listener);
                viewFinder = scanner.getViewFinder();

                scanner.getVideoControl.setDisplayFullScreen(true);
                add(viewFinder);
              
                scanner.startScan();
            } catch (Exception e) {
                Status.show(e.getMessage());
                e.printStackTrace();
            }       

To stop scan

            try {
          scanner.stopScan();
     } catch (MediaException e) {
         e.printStackTrace();
     }


Dowload sample usages here
QR Code Sample

51 comments:

  1. thanks for the good work!

    UsingBarcodeLib works wonderfully on OS5 simulator, but on OS6 simulators it shows the QR code and does nothing.. any idea?

    ReplyDelete
  2. Great work! Seems to work on OS5 and OS6 devices.

    I will write if I found any bugs!

    ReplyDelete
  3. Thanks for sharing these. could build on api 4.5 and tested ok on 4.5+ simulators and 6 device.

    ReplyDelete
  4. Nice post! Did you try to generate QR code ?

    ReplyDelete
  5. I am using Blackberry 9700, OS 6, and Java SDK 5 on Eclipse 3.6.2.
    This code doesn't scan QR-code below a certain size (compared to scanner in Java SDK 6) and takes longer to detect the code. I can visually see that there is something wrong with focus.

    ReplyDelete
  6. This is very interesting to try. I want to try to make my first blackberry.

    ReplyDelete
  7. In OS 6, there is already a Barcode scanner library, and already added google ZXing library.

    Please check on net.rim.device.api.barcodelib

    ReplyDelete
  8. I'm having difficulties on testing the library on 8520. Have you tested on 8520? because from what i've experienced, since lack of auto focus, I can't capture the barcode using 8520 camera. any idea?

    ReplyDelete
  9. There are several way to capture barcode.

    There is some tutorial show how to capture using camera, to capture image on screen.
    This one is using camera when user click.

    Some other way is using ViewFinder which can invoke camera to capture image and show on ViewFinder (which is field and can be put on screen)

    Try to use different delay, such as 3, 4 seconds then capturing screen using

    Bitmap bmpScreenshot = new Bitmap(Display.getWidth(),Display.getHeight());
    Display.screenshot(bmpScreenshot);

    ReplyDelete
  10. Hi,

    How to encode a string to bitmap on Blackberry 5.0?

    ReplyDelete
  11. Mostly here is code convert from array of bytes to bitmap

    EncodedImage image = EncodedImage.createEncodedImage(imageData, 0, imageData.length);

    Bitmap bmp = image.getBitmap();

    If you have string representation of image, I think you should convert it to array of bytes. using getBytes() method. Hope it helps

    ReplyDelete
  12. Great work...!
    I use this for all 5.0 below versions of os, it's working and scanning qrcodes but takes long time to scan and can't capture images below certain dimensions, can you please increase the speed of scanning...

    ReplyDelete
  13. wen..gimana cara embed ke aplikasi yang menggunakan JRE 6. saya pake directive #ifdef (supaya bs digunakan di versi mana aja)

    //#ifdef OS_6_0
    if(_viewFinderScreen == null) {
    _viewFinderScreen = new ViewFinderScreen();
    }
    UiApplication.getUiApplication().pushScreen(_viewFinderScreen);
    _viewFinderScreen.startScan();
    //#else
    pake lib barcode utk v5.0
    //
    #endif

    tolong email apa saja code2 dan lib yg dibutuhkan ke bagussujarwo@ymail.com
    thanks

    ReplyDelete
  14. hey , and if I want to open the cam to detect if there's a barcode to be scanned can you give me an example :)

    ReplyDelete
  15. @BagusSujarwo

    (Indonesian)
    Untuk di OS 6.0
    Aku sarankan menggunakan library dari RIM yang sudah disediakan di OS 6.0

    Contoh di atas adalah workaround untuk di OS 5.0, dikarenakan secara default tidak ada

    Cek di dokumentasi package BarcodeLib

    net.rim.device.api.barcodelib

    ReplyDelete
  16. @Rucha

    for increase speed of scanning

    maybe you can modify the code change the interval

    http://www.4shared.com/zip/lNUj0cCJ/BBBarcodeLib.html

    Get the code here, and please modify. Thanks.

    ReplyDelete
  17. @Zenkology,

    could you describe what you are going to do?

    ReplyDelete
  18. @omgimanoob

    on OS6, please use RIM library on

    net.rim.device.api.barcodelib

    ReplyDelete
  19. @Ali irwan actually um going to add a scan for qr code in my app so i need to open camera after I press a button (sry for being late in reply ) :)

    ReplyDelete
  20. @Zenkology

    Okay, you can use ViewFinder, please see my code on blackberry barcode lib that I'd share.

    Just modify, and use the ViewFinder. The ViewFinder itself, shows what camera capture and display it on screen as a Field. ViewFinder is subclass of Field

    You will need class VideoPlayer which will have method getSnapshot()

    trigger the method when button is pushed. I've found this example somewhere in RIM Knowlegde base.
    try hit a question on www.stackoverflow.com instead

    ReplyDelete
  21. ureHey, thanks a lot. Works great on 5.0 devices

    ReplyDelete
  22. hey thanks, works great on 5.0 devices

    ReplyDelete
  23. Hi, Its not working on BB OS 7.0 device, please help me out.

    Regards,
    Naveen

    ReplyDelete
  24. @Naveen

    if you using BB OS 7.0
    please don't use this code, untested.

    Please use default BlackbBerry library for barcode scanning. (Barcodelib is available built in since 6.0)

    Check this link

    ReplyDelete
  25. First of all, thanks for sharing this!

    This lib worked just fine with 9300 and 8520 but it doesn't with 9700. I modified the code to show me the bitmap it's getting in the screenshot and 9700's picture is always a blank screen...any idea what would it be?

    ReplyDelete
  26. hey man i am facing problem with default qr code reader lib for OS 6.0 with device model no 9800. Code opens camera but does not read qr code neither it gives any exception pls help me

    ReplyDelete
  27. Not works in BB Storm with OS 5.0 neither in BB Curve 9300 with OS 5.0.

    In my BB 9360 with OS 7.0 works great, can scan EAN-13, UPC-A, QR.

    Why no works in OS 5.0 ??

    ReplyDelete
  28. This comment has been removed by the author.

    ReplyDelete
  29. if anyone cannot generate bar-code there is the solution:
    private void generate(BarcodeFormat format, int width, int height) {
    Hashtable hints = new Hashtable();
    // Assuming UTF-8 since text is passed via JSON.
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result = null;
    try {
    result = writer.encode(text.getText(), format, width, height,
    hints);
    } catch (WriterException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    width = result.getWidth();
    height = result.getHeight();

    // Create a new bitmap which will be filled pixel by pixel from the
    // generated BitMatrix.
    Bitmap bitmap = new Bitmap(width, height);

    // An integer array to hold a row of bitmap pixels at a time.
    int[] pixels = new int[width];

    // Loop through the generated BitMatrix and fill the Bitmap.
    for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
    // Pixel needs to be black (0xFF000000) or white
    // (0xFFFFFFFF)
    pixels[x] = result.get(x, y) ? 0xFF000000 : 0xFFFFFFFF;
    }
    bitmap.setARGB(pixels, 0, width, 0, y, width, 1);
    }

    _barcodeField.setBitmap(bitmap);
    }
    where width bitmapfield width,and height bitmapfield height

    ReplyDelete
  30. thx very much for ali irawan for this fantastic post :D

    ReplyDelete
  31. Hey...Veryyy good work!!!....Thanks for the sample app. The ZXling has demo application for blackberry QR code reader but that is not proper & not decoding image.

    But by using & understanding ur code we can make changes in ZXling demo project so that it cud work....

    Thankssssss....a lottttttt :-)

    ReplyDelete
  32. Do we need to buy the package in http://www.4shared.com/file/lNUj0cCJ/BBBarcodeLib.html to download the code?

    ReplyDelete
  33. http://www.4shared.com/file/lNUj0cCJ/BBBarcodeLib.html - where the code is available for scanning QA code for version less than 6.0 is not free.
    Did you guys pay for it to get the cource?

    ReplyDelete
  34. No need. It's totally free.
    Please download and review the code.
    You can modify for your need

    ReplyDelete
  35. Hi,

    Please provide me any example for QR code which run into device because right now i am not download your sample code.

    Thanks.

    ReplyDelete
  36. Here is it:

    http://www.multiupload.nl/Y2M547TI3A

    Please tell me if u don't understand anything :)

    ReplyDelete
  37. it works on 8520, 9360 but it didn't work on 9550. any clue?

    ReplyDelete
  38. How you turn on the debugger? To find out what error message occurs ?
    MOre clear information is better to troubleshoot.

    ReplyDelete
  39. hmmm, after i debug. Display.screenshot results blankscreen or usingbarcodescreen.

    ReplyDelete
  40. http://supportforums.blackberry.com/t5/Java-Development/Display-screenshot-problem-capturing-camera-view-on-9780-9650/td-p/1623943

    i think the only solution is by using videocontroller.getsnapshot() instead

    ReplyDelete
  41. Your package classes are very important and useful.

    Thanks Wen.

    ReplyDelete
  42. Okay, thanks.
    It's nice to help people.

    Any suggesstion for more topic via email to boylevantz@gmail.com

    ReplyDelete
  43. Hi All,
    I take a MissingNativeError
    using your source as a Library.
    Somebody can help me?
    I creata a library using Eclipse and I import it on my project.
    thank you in advance

    ReplyDelete
  44. I prefer you copy the source code an embedded in your app.

    But If you do want to create library make sure when you create a project in Eclipse, choose blackberry library project.

    It will be set in application Descriptor whether it's library or application

    ReplyDelete
  45. Hello, 4shared.com https login seems to be not working at the moment, and I really need this, can you send me it to mrabooode@gmail.com

    ReplyDelete
  46. When we use this code through menu in a webview screen, invokelater method is not execute.

    ReplyDelete
  47. Thanks for your recommendation of the barcode scanner SDK and the example of usage is wonderful.
    I enjoy reading your article and hope to see more.

    ReplyDelete
  48. This comment has been removed by the author.

    ReplyDelete