【问题标题】:Android Generate QR code and Barcode using ZxingAndroid 使用 Zxing 生成二维码和条形码
【发布时间】:2014-04-17 18:35:00
【问题描述】:

使用zxing生成二维码的代码是---

它需要字符串数据和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。由于我已经在使用 zxing,所以没有必要包含 itextbarbecue jars

【问题讨论】:

    标签: android barcode zxing


    【解决方案1】:

    试试这个代码

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

    希望对你有所帮助。

    【讨论】:

    • getActivity() 是做什么的?
    • getActivity() 的两个可能定义getActivity() in a Fragment returns the Activity the Fragment is currently associated with. (see http://developer.android.com/reference/android/app/Fragment.html#getActivity()).getActivity() is user-defined.
    • 即,如果您使用的是 Fragment,则使用 getActivity()。如果您使用的是 Activity,请使用 this 关键字来获取当前的 Activity 上下文。
    【解决方案2】:

    您正在使用 QRCodeWriter。如果您想编写其他类型的代码,请使用其他 Writer。

    检查这个MultiFormatWriter - 它可以写任何类型的栏或在子文件夹中找到特定的作者here(来自zxing库)

    【讨论】:

    • 谢谢它就像一个魅力。我正在粘贴代码......将来帮助某人'
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      你来了,

      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.");
      }
      

      【讨论】:

        【解决方案5】:

        我已经测试了生成条形码的公认答案,但在大 ImageView 中使用时输出模糊。要获得高质量的输出,BitMatrix、Bitmap 和最终 ImageView 的 width 应该相同。但是使用接受的答案这样做会使条形码生成非常慢(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;
        }
        

        这种方法即使对于非常大的输出也非常快,并且可以生成高质量的位图

        【讨论】:

          猜你喜欢
          • 2014-08-14
          • 2011-09-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多