itechDroid itechDroid - 4 months ago 36
Android Question

Make barcode generator in android

i made a barcode generator using code128 but when my barcode generate then it wont display in my layout. so plz suggest me some solution that how to display generated barcode in my layout
enter code here

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
l1=(LinearLayout) findViewById(R.id.linear);
Code128 barcode = new Code128();
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
canvas=new Canvas(b);
barcode.setData("11223344556688777");
barcode.setProcessTilde(false);

barcode.setUom(IBarcode.UOM_PIXEL);
barcode.setX(1f);
barcode.setY(75f);

barcode.setLeftMargin(10f);
barcode.setRightMargin(10f);
barcode.setTopMargin(10f);
barcode.setBottomMargin(10f);

barcode.setResolution(72);
barcode.setShowText(true);

barcode.setTextFont(new AndroidFont("Arial", Typeface.NORMAL, 12));
barcode.setTextMargin(6);
barcode.setTextColor(AndroidColor.black);

barcode.setBackColor(AndroidColor.white);
RectF bounds = new RectF(30, 30, 0, 0);
try {
barcode.drawBarcode(canvas, bounds);
l1.draw(canvas);
l1.addView(t1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Answer

If you want to generate bar code for desired information, then use this code

package com.barcodegeneration.in;

import com.onbarcode.barcode.android.AndroidColor;
import com.onbarcode.barcode.android.AndroidFont;
import com.onbarcode.barcode.android.Code128;
import com.onbarcode.barcode.android.Code39;
import com.onbarcode.barcode.android.IBarcode;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.view.View;

public class MakeBarcode extends View{

    public MakeBarcode(Context context) {

        super(context);
        System.out.println("im in view class");
        // TODO Auto-generated constructor stub
    }
public void onDraw(Canvas canvas)
{
    super.onDraw(canvas);
     try {
        generate_barcode(canvas);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
public static void generate_barcode(Canvas canvas)throws Exception {
    Code128 barcode = new Code128();
    barcode.setData("ash");
  //  barcode.setData("ashutosh");
    barcode.setProcessTilde(false);
    System.out.println("i m inside code 39 without static mmm");
    System.out.println("i m inside code 39 mmm");
    barcode.setUom(IBarcode.UOM_PIXEL);

    barcode.setX(1f);

    barcode.setY(75f);

    barcode.setLeftMargin(10f);
    barcode.setRightMargin(10f);
    barcode.setTopMargin(10f);
    barcode.setBottomMargin(10f);


    barcode.setResolution(72);


    barcode.setShowText(true);

    barcode.setTextFont(new AndroidFont("Arial", Typeface.NORMAL, 12));

    barcode.setTextMargin(6);
    barcode.setTextColor(AndroidColor.black);


    barcode.setBackColor(AndroidColor.white);

        RectF bounds = new RectF(30, 30, 0, 0);
        try {

    barcode.drawBarcode(canvas, bounds);


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
}

}


please like this post if useful for you.