android 에서 바코드 이미지를 생성하려면. zxing 라이브러리를 이용한다.

private void doCreateBarcode()
{
    com.google.zxing.MultiFormatWriter zxingWriter = new MultiFormatWriter();
    String sBarcode = "0000000000000000"; // 16자리 숫자

    try
    {
        int w = 180; // 바코드 이미지 넓이
        int h = 120; // 바코드 이미지 높이
        BitMatrix bm = zxingWriter.encode(sBarcode, BarcodeFormat.CODE_128, w, h);
        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

        for(int i=0; i < w; i++) //width
        {
            for(int j=0; j < h; j++) //height
            {
                bitmap.setPixel(i, j, bm.get(i, j) ? Color.BLACK: Color.WHITE);
            }
        }

        if(bitmap == null)
        {
            lib_common.doShowToast(this, "error");
            return;
        }

        ivBarcode.setImageBitmap(bitmap);
    }
    catch(com.google.zxing.WriterException exp)
    {
        lib_common.doShowToast(this, "WriterException");
    }
}



반응형
Posted by 돌비
,