Home

Wednesday, April 28, 2010

Bar Code generation in Android

 

I had an assignment in which I must how the sensor values as a bar code. For that I searched internet for the libraries. I found one here.

In the world of android who would pay to get such a library. If they are able to do it, even I can do it. Thanks to Google and XZing team. So, I finally found a way in the xzing site.

I downloaded the source code and created a jar file with the exact classes needed. The file can be download from here.

 

The code which I used to generate the bar code is here.

 

private void    generateBarCode()
    {
        img                 =   (ImageView)findViewById(R.id.img);
        Code128Writer c9     =   new Code128Writer();
        mBitmap          =   Bitmap.createBitmap(450, 150, Config.ARGB_8888);
        try {
            BitMatrix   bm  =   c9.encode( swapped? "tel:12345345": "tel:13453432", BarcodeFormat.CODE_128, 450, 150);
            mBitmap          =   Bitmap.createBitmap(450, 150, Config.ARGB_8888);
           for(int i=0;i<450;i++)
           {
               for(int j=0;j<150;j++)
               {
                   mBitmap.setPixel(i, j, bm.get(i, j)? Color.BLACK:Color.WHITE);
               }
           }
        } catch (WriterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(mBitmap!=null)
        {
            img.setVisibility(View.VISIBLE);
            img.setImageBitmap(mBitmap);
            showBarCode.setVisibility(View.GONE);
        }
        else

    }

 

The above code sets the bar code image to the view.

 

Please comment here if you need more help.

1 comment:

  1. Thanks for your sharing,i learn a lot.I need to generate 1D barcode image and set it to ImageView according to given 13-character code. Can you help me with this please?
    barcode generation in Android

    ReplyDelete