2014-03-13 108 views
10

代碼使用斑馬線是生成QR碼---Android的生成QR碼和條形碼使用斑馬線

這需要字符串數據和imageview這只是正常

private void generateQRCode_general(String data, ImageView img)throws WriterException { 
    com.google.zxing.Writer writer = new QRCodeWriter(); 
    String finaldata = Uri.encode(data, "utf-8"); 

    BitMatrix bm = writer.encode(finaldata, BarcodeFormat.QR_CODE,150, 150); 
    Bitmap ImageBitmap = Bitmap.createBitmap(150, 150,Config.ARGB_8888); 

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

    if (ImageBitmap != null) { 
     qrcode.setImageBitmap(ImageBitmap); 
    } else { 
     Toast.makeText(getApplicationContext(), getResources().getString(R.string.userInputError), 
       Toast.LENGTH_SHORT).show(); 
    } 
} 

現在的問題是,如何得到bar code使用相同的庫。我看到一些文件與bar codes有關,但我不知道該怎麼做。 由於我想在應用程序內生成bar code,並且不會調用任何web service。既然我已經使用斑馬線,在包括iText的燒烤罐子

回答

3

您正在使用QRCodeWriter。如果您想編寫其他類型的代碼,請使用另一個Writer。

入住這MultiFormatWriter - 它可以寫任何類型的酒吧或查找特定的作家在子文件夾here(這是從斑馬線庫)

+0

感謝它的工作就像一個魅力。我粘貼的代碼...蟎幫助未來的人' – Ciff

0

沒有一點試試這個代碼

Context context = getActivity(); 
Intent intent = new Intent("com.google.zxing.client.android.ENCODE"); 
intent.putExtra("ENCODE_TYPE", Text); 
intent.putExtra("ENCODE_DATA", "12345678901"); 
intent.putExtra("ENCODE_FORMAT", "UPC_A"); 
startActivity(intent); 

希望這可以幫助你。

+0

getActivity()是做什麼的? – Ciff

+0

getActivity()的兩種可能的定義在片段中的getActivity()返回片段當前關聯的活動。 (請參閱http://developer.android.com/reference/android/app/Fragment.html#getActivity()).' 或 'getActivity()是用戶定義的.' –

+0

即如果您使用片段,然後使用'getActivity()'。如果你使用Activity,那麼使用'this'關鍵字來獲取當前活動上下文。 –

6

像Gaskoin告訴... MultiFormatWrite它的工作:)這裏是代碼。

 com.google.zxing. MultiFormatWriter writer =new MultiFormatWriter(); 


     String finaldata = Uri.encode(data, "utf-8"); 

     BitMatrix bm = writer.encode(finaldata, BarcodeFormat.CODE_128,150, 150); 
     Bitmap ImageBitmap = Bitmap.createBitmap(180, 40,Config.ARGB_8888); 

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

     if (ImageBitmap != null) { 
      qrcode.setImageBitmap(ImageBitmap); 
     } else { 
      Toast.makeText(getApplicationContext(), getResources().getString(R.string.userInputError), 
        Toast.LENGTH_SHORT).show(); 
     } 
+0

+1它工作正常。 ;) –

1

你去那裏,

public static Bitmap createBarCode (String codeData, BarcodeFormat barcodeFormat, int codeHeight, int codeWidth) { 

    try { 
     Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>(); 
     hintMap.put (EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); 

     Writer codeWriter; 
     if (barcodeFormat == BarcodeFormat.QR_CODE) { 
      codeWriter = new QRCodeWriter(); 
     } else if (barcodeFormat == BarcodeFormat.CODE_128) { 
      codeWriter = new Code128Writer(); 
     } else { 
      throw new RuntimeException ("Format Not supported."); 
     } 

     BitMatrix byteMatrix = codeWriter.encode (
      codeData, 
      barcodeFormat, 
      codeWidth, 
      codeHeight, 
      hintMap 
     ); 

     int width = byteMatrix.getWidth(); 
     int height = byteMatrix.getHeight(); 

     Bitmap imageBitmap = Bitmap.createBitmap (width, height, Config.ARGB_8888); 

     for (int i = 0; i < width; i ++) { 
      for (int j = 0; j < height; j ++) { 
       imageBitmap.setPixel (i, j, byteMatrix.get (i, j) ? Color.BLACK: Color.WHITE); 
      } 
     } 

     return imageBitmap; 

    } catch (WriterException e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

當然,只要你想,只要改變這裏的構造函數,你可以支持多達BarcodeFormats作者:

Writer codeWriter; 
if (barcodeFormat == BarcodeFormat.QR_CODE) { 
    codeWriter = new QRCodeWriter(); 
} else if (barcodeFormat == BarcodeFormat.CODE_128) { 
    codeWriter = new Code128Writer(); 
} else { 
    throw new RuntimeException ("Format Not supported."); 
} 
4

我測試過公認的答案生成條形碼,但在大型ImageView中使用時,輸出爲模糊。爲了獲得高質量的輸出,BitMatrix的寬度,位圖和最終的ImageView應該是相同的。但使用接受的答案這樣做會使條碼生成真的很慢(2-3秒)。這是因爲

Bitmap.setPixel() 

是一個緩慢的操作,並且接受的答案是做密集使用該操作(2嵌套的for循環)的。

爲了克服這個問題,我已經修改了一點點位圖生成算法(只用它來生成條形碼)利用Bitmap.setPixels的(),它的速度要快得多:

private Bitmap createBarcodeBitmap(String data, int width, int height) throws WriterException { 
    MultiFormatWriter writer = new MultiFormatWriter(); 
    String finalData = Uri.encode(data); 

    // Use 1 as the height of the matrix as this is a 1D Barcode. 
    BitMatrix bm = writer.encode(finalData, BarcodeFormat.CODE_128, width, 1); 
    int bmWidth = bm.getWidth(); 

    Bitmap imageBitmap = Bitmap.createBitmap(bmWidth, height, Config.ARGB_8888); 

    for (int i = 0; i < bmWidth; i++) { 
     // Paint columns of width 1 
     int[] column = new int[height]; 
     Arrays.fill(column, bm.get(i, 0) ? Color.BLACK : Color.WHITE); 
     imageBitmap.setPixels(column, 0, 1, i, 0, 1, height); 
    } 

    return imageBitmap; 
} 

這種方法是真的很快,即使真的很大的輸出,併產生一個高品質的位圖

+0

完美。 :) 謝謝! – David