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:

omgimanoob said...

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?

_ said...

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

I will write if I found any bugs!

bbg9507 said...

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

vandzi said...

Nice post! Did you try to generate QR code ?

Asad Satti said...

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.

software said...

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

Ali Irawan (Wen) said...

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

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

paw said...

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?

Ali Irawan (Wen) said...

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);

congtue85 said...

Hi,

How to encode a string to bitmap on Blackberry 5.0?

Ali Irawan (Wen) said...

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

Rhucha said...

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...

BagusSujarwo said...

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

Zenkology said...

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 :)

Ali Irawan (Wen) said...

@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

Ali Irawan (Wen) said...

@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.

Ali Irawan (Wen) said...

@Zenkology,

could you describe what you are going to do?

Ali Irawan (Wen) said...

@omgimanoob

on OS6, please use RIM library on

net.rim.device.api.barcodelib

Zenkology said...

@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 ) :)

Ali Irawan (Wen) said...

@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

ajju said...

ureHey, thanks a lot. Works great on 5.0 devices

ajju said...

hey thanks, works great on 5.0 devices

Naveen said...

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

Regards,
Naveen

Ali Irawan (Wen) said...

@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

Ismael said...

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?

deepak charya said...

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

samael666 said...

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 ??

nutan Sharma said...
This comment has been removed by the author.
Henawey said...

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

Henawey said...

thx very much for ali irawan for this fantastic post :D

Ali Irawan (Wen) said...

Thanks for adding the code

Ankita said...

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 :-)

Ali Irawan (Wen) said...

Sure nice to help :)

Bhuvana said...

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

Bhuvana said...

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?

Ali Irawan (Wen) said...

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

Umang Shah said...

Hi,

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

Thanks.

Ahmed Henawey said...

Here is it:

http://www.multiupload.nl/Y2M547TI3A

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

Hasanul Hakim said...

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

Ali Irawan (Wen) said...

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

Hasanul Hakim said...

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

Hasanul Hakim said...

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

ankit agrawal said...

Your package classes are very important and useful.

Thanks Wen.

Ali Irawan (Wen) said...

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

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

LV said...

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

Ali Irawan (Wen) said...

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

Rohan Makkar said...

thanks, for this workaround

Abdulrahman Haoua said...

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

Ravi Chandra Keshari said...

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

Jack May said...

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.

Jack May said...
This comment has been removed by the author.