简体   繁体   English

Android使用Zxing生成QR码和条码

[英]Android Generate QR code and Barcode using Zxing

Code to generate Qr code using zxing is --- 使用zxing生成Qr代码的代码是---

It takes string data and the imageview This works just fine 它需要字符串数据和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(); 
    }
}

Now my question is ,how to get bar code using the same library.i saw some files related to bar codes but i am not sure how to do it. 现在我的问题是,如何使用相同的库获取bar code 。我看到一些与bar codes相关的文件,但我不知道该怎么做。 Since I want to generate the bar code within the application and not call any web service . 因为我想在应用程序中生成bar code而不是调用任何web service Since i am already using zxing,no point in including itext and barbecue jars 由于我已经在使用zxing,因此不包括itext烧烤罐

I've tested the accepted answer to generate a Barcode but the output is blurry when used in a big ImageView. 我已经测试了生成条形码的已接受答案,但在大型ImageView中使用时输出模糊 To get a high quality output, the width of the BitMatrix, the Bitmap and the final ImageView should be the same. 要获得高质量输出,BitMatrix,Bitmap和最终ImageView的宽度应该相同。 But doing so using the accepted answer will make the Barcode generation really slow (2-3 seconds). 但是使用接受的答案这样做会使条形码生成非常慢(2-3秒)。 This happens because 这是因为

Bitmap.setPixel()

is a slow operation, and the accepted answer is doing intensive use of that operation (2 nested for loops). 是一个缓慢的操作,接受的答案是密集使用该操作(2嵌套for循环)。

To overcome this problem I've modified a little bit the Bitmap generation algorithm (only use it for Barcode generation) to make use of Bitmap.setPixels() which is much faster: 为了克服这个问题,我修改了一点位图生成算法(仅用于生成条形码)以使用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;
}

This approach is really fast even for really big outputs and generates a high quality bitmap . 即使对于非常大的输出,这种方法也非常快,并且可以生成高质量的位图

Like Gaskoin told... MultiFormatWrite it worked :) here is the code. 就像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(); 
        }

You are using QRCodeWriter. 您正在使用QRCodeWriter。 If you want to write another type of code, use another Writer. 如果要编写其他类型的代码,请使用另一个Writer。

Check this MultiFormatWriter - it can write any type of bar or find specific writers here in subfolders (this is from zxing library) 勾选此MultiFormatWriter -它可以写任何类型的酒吧或查找特定的作家在这里的子文件夹(这是从斑马线库)

There you go, 你去,

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

Of course you can support as many BarcodeFormats as you want, just change the constructor here : 当然,您可以根据需要支持尽可能多的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.");
}

try this code 试试这段代码

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

hope this helps you. 希望这能帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM