2012-03-14 71 views
0

我做了一個條形碼生成器使用code128,但是當我的條形碼生成,然後它不會顯示在我的佈局。所以PLZ建議我一些解決方案,如何在我的佈局顯示生成的條形碼 輸入代碼在這裏使條碼生成器在android

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

回答

1

如果您想爲所需信息生成條形碼,那麼我們e此代碼​​

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.