andorid jar/library source parsing zxing

Catalog: andorid jar/library source code parsing 

Zxing:

Role:

Generate and identify, two-dimensional code, bar code.

Chestnuts:

Generate a QR code to assign to ImageView

     QRCodeWriter qrCodeWriter = new QRCodeWriter();
        Map<EncodeHintType, String> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //Remember to customize the width and length
        BitMatrix encode = null;
        try {
            encode = qrCodeWriter.encode("hello,world!", BarcodeFormat.QR_CODE, width, height, hints);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        int[] colors = new int[width * height];
        //utilize for The loop writes out the information to be represented
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                if (encode.get(i, j)) {
                    colors[i * width + j] = Color.BLACK;
                } else {
                    colors[i * width + j] = Color.WHITE;
                }
            }
        }

        Bitmap bit = Bitmap.createBitmap(colors, width, height, Bitmap.Config.RGB_565);
        imageView.setImageBitmap(bit);

Generate a barcode and assign it to ImageView

     final int WHITE = 0xFFFFFFFF;
        final int BLACK = 0xFF000000;
        MultiFormatWriter writer = new MultiFormatWriter();
        BitMatrix result = null;
        try {
            BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128;
            result = writer.encode("1234567123456", barcodeFormat, 400,
                    100, null);
        } catch (WriterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int width = result.getWidth();
        int height = result.getHeight();
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            int offset = y * width;
            for (int x = 0; x < width; x++) {
                pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

        imageView2.setImageBitmap(bitmap);

Identification of QR codes

   private void Test3() {
        recogQRcode(imageView);
    }

    public void recogQRcode(ImageView imageView){
        Bitmap QRbmp = ((BitmapDrawable) (imageView).getDrawable()).getBitmap();   //Picture bitmap turn
        int width = QRbmp.getWidth();
        int height = QRbmp.getHeight();
        int[] data = new int[width * height];
        QRbmp.getPixels(data, 0, width, 0, 0, width, height);    //Get Pixels
        RGBLuminanceSource source = new RGBLuminanceSource(width, height, data);
        BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
        QRCodeReader reader = new QRCodeReader();
        Result re = null;
        try {
            //Get results
            re = reader.decode(bitmap1);
        } catch (NotFoundException e) {
            e.printStackTrace();
        } catch (ChecksumException e) {
            e.printStackTrace();
        } catch (FormatException e) {
            e.printStackTrace();
        }

        lblMsg1.setText(re.getText());
    }

- Identify barcodes

     Bitmap QRbmp = ((BitmapDrawable) (imageView2).getDrawable()).getBitmap();   //Picture bitmap turn
        int width = QRbmp.getWidth();
        int height = QRbmp.getHeight();
        int[] data = new int[width * height];
        QRbmp.getPixels(data, 0, width, 0, 0, width, height);    //Get Pixels
        RGBLuminanceSource source = new RGBLuminanceSource(width, height, data);

        //Binary Picture Conversion bitmap object(Explain:Establish HybridBinarizer object,Require incoming LuminanceSource,So in source(Binary Picture),And through BinaryBitmap convert to bitmap object)
        BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
        //CodaBarReader codaBarReader= new CodaBarReader();    //codaBarReader  QR code
        try {
            //MultiFormatReader Is a class for reading images(stay core package)
            Result result = new MultiFormatReader().decode(bitmap1);      //Identify Barcode,And two-dimensional codes(Explain:Get the information we need)
            lblMsg2.setText(result.getText());
        } catch (NotFoundException e) {
            e.printStackTrace();
            lblMsg2.setText("ex " + e.getMessage());
        }

Source interpretation:

A: Generate a two-dimensional code

1. Create a QRCodeWriter object, call encode, pass in the data that needs to generate the QR code, format the parameters, size, and encoding information

2. Call Encoder's encode method, a, to determine the current encoding used.b, determine the encoding mode (number, byte, and 0x00-0x5f) for generating the two-dimensional code data.c, with an additional ECI section (part of the two-dimensional code format).d, fill the FNC1 information into the header.e, write mode is marked in the header.

3. Write two-dimensional code text data into different result bit collections according to different modes.

4. Determine whether the version and capacity are within range.

5. Merge headers, data length, data content into a bit collection.

6. Fill in the data so that 8 is an integer multiple of the bit data.

7. Mix bitwise data information and error correction codes together and store new data (algorithm is complex, refer to the algorithm of two-dimensional code generation specifically).

8. Call MatrixUtil.buildMatrix to construct the QR code matrix and return the QR code information.QRCode uses ByteMatrix to store the matrix, which is a two-dimensional bytes array inside.private final byte[][] bytes;

9. Convert the color of the matrix from 0==white, 1==black for the input matrix and 0==black for the output matrix and 255==white for the output matrix (that is, 8-bit grayscale bitmap).

10. Create a color array and store the color data in it.Call Bitmap.createBitmap, pass the color collection information to him, and return a Bitmap that is the QR code data.

B: Generate barcodes

1. Create a MultiFormatWriter object and call its encode method, pass in the barcode data, the encoding information, and the size of the barcode result.

2. Select different writers according to different encoding, using Code128Writer here

3, a, judge the barcode content, length limit [1,80]....omitted

4. Use binary to convert binary to picture.That's the end result.

C: Identify the QR code.(reverse resolution)

D: Identify the barcode.(reverse resolution)

Source: https://github.com/zxing/zxing

Introduction:

api 'com.google.zxing:android-core:3.3.0'
api 'com.google.zxing:core:3.3.2'

Note: (If you can't load from google, you need to change the source, the code and diagram below)

maven{ url'http://maven.aliyun.com/nexus/content/groups/public/' }
maven{ url'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
  

Tags: Android encoding Maven Google nexus

Posted on Tue, 05 May 2020 13:19:38 -0400 by dad00