Wednesday, 28 March 2012

Generate QRCode Image In Blackberry

This following Code gives the Bitmap Image of QRCode when User Enters Text/PhoneNumber/Url:
---------------------------------------QRCodeScreen.java-----------------------------------

public class QRCodeScreen extends MainScreen implements FieldChangeListener
{
    private static final int BARCODE_WIDTH=250;
    private Font font=Font.getDefault().derive(Font.BOLD|Font.ITALIC, 20);
    private BitmapField barcodeImage;
    public BasicEditField enterText;
    public ButtonField create;
    public QRCodeScreen()
    {
        createGUI();
    }

    private void createGUI()
    {
        barcodeImage=new BitmapField(new Bitmap(BARCODE_WIDTH, BARCODE_WIDTH),FIELD_HCENTER);
        barcodeImage.setBorder(BorderFactory.createBevelBorder(new XYEdges(2, 2, 2, 2)));
        add(barcodeImage);
       
        LabelField label=new LabelField("Enter Any Text/URL/Phone Number/SMS",Field.NON_FOCUSABLE);
        label.setFont(font);
        label.setPadding(5, 0, 5, 0);
        add(label);
       
        enterText=new BasicEditField("", "",250,BasicEditField.FOCUSABLE);       
        enterText.setFont(font);
        add(enterText);
       
        create=new ButtonField("Create BarCode",FIELD_HCENTER);
        create.setPadding(5, 0, 0, 0);
        create.setFont(font);
        create.setChangeListener(this);
        add(create);
    }
   
    public void fieldChanged(Field field, int context)
    {
        if(field==create)
        {
            if(enterText.getText().equals(""))
            {
                QRCodeStartUp.errorHandling("Please Enter Data");
            }
            else
            {
                generateQRCode();                       
            }
        }
    }

    private void generateQRCode()
    {
        try
        {
            QRCode qrCode=new QRCode();
            Encoder.encode(enterText.getText(), ErrorCorrectionLevel.L, qrCode);
            ByteMatrix barcode=qrCode.getMatrix();
            Bitmap bitmap=BarcodeBitmap.createBitmap(barcode, BARCODE_WIDTH);
            barcodeImage.setBitmap(bitmap);
            enterText.setText("");
        }
        catch (Exception e)
        {
            QRCodeStartUp.errorHandling("Exception: "+e.getMessage());
        }   
    }
   
    protected boolean onSavePrompt()
    {
        return true;
    }   
}

1 comment:

  1. Good job,you are amazing.I decide to generate QRCode in android of my Blackberry use the following code you give.
    By the way,I have a Blackberry Curve and want to scan a QR code containing a web address. I cannot use Blackberry App World as it has never worked correctly on my phone. I read somewhere that the Blackberry already has some way of reading a QR code installed on it without having to download any new software.Can you help?

    ReplyDelete