After ZXing, one-dimensional code, two-dimensional code, and other codes are scanned separately.

Source: Internet
Author: User
Tags addall

After ZXing, one-dimensional code, two-dimensional code, and other codes are scanned separately.

I have never touched on the QR code before. I recently encountered a problem: how can I determine whether the QR code is scanned by a QR code or by a QR code? after hard work for one afternoon, I can search online materials,

There are two ways to solve this problem (the second method is recommended ):

1. Modify the source code (as mentioned later)

2. Determine by the returned Encoding

Method 1:

The key to source code modification involves three classes: CaptureActivity, DecodeThread, and DecodeFormatManager.


1.First, let's take a look at the Zxing source code, which contains a DecodeFormatManager encoding management class: the original final class, because some parameters in the class need to be used, so we need to change it to a common class, however, I don't know whether the change will affect the original code structure. There are also several variables that are also constant type. here we need to change them to static type:


Public class DecodeFormatManager {// final class DecodeFormatManager {public static Vector <BarcodeFormat> PRODUCT_FORMATS; public static Vector <BarcodeFormat> ONE_D_FORMATS; public static Vector <BarcodeFormat> QR_CODE_FORMATS; public static Vector <BarcodeFormat> DATA_MATRIX_FORMATS; // static final Vector <BarcodeFormat> PRODUCT_FORMATS; // static final Vector <BarcodeFormat> ONE_D_FORMATS; // static final Vector <BarcodeFormat> QR_CODE_FORMATS; // static final Vector <BarcodeFormat> DATA_MATRIX_FORMATS; static {PRODUCT_FORMATS = new Vector <BarcodeFormat> (5); PRODUCT_FORMATS.add (BarcodeFormat. UPC_A); // UPC Standard Code (General Product) PRODUCT_FORMATS.add (BarcodeFormat. UPC_E); // UPC shortening code (commodity short code) PRODUCT_FORMATS.add (BarcodeFormat. EAN_13); PRODUCT_FORMATS.add (BarcodeFormat. EAN_8); PRODUCT_FORMATS.add (BarcodeFormat. RSS14); ONE_D_FORMATS = new Vector <BarcodeFormat> (PRODUCT_FORMATS.size () + 4); encode (PRODUCT_FORMATS); // Add the code added in PRODUCT_FORMATS to ONE_D_FORMATS ONE_D_FORMATS.add (BarcodeFormat. CODE_39); ONE_D_FORMATS.add (BarcodeFormat. CODE_93); ONE_D_FORMATS.add (BarcodeFormat. CODE_128); ONE_D_FORMATS.add (BarcodeFormat. ITF); QR_CODE_FORMATS = new Vector <BarcodeFormat> (1); // QR_CODE is the QR code QR_CODE_FORMATS.add (BarcodeFormat. QR_CODE); DATA_MATRIX_FORMATS = new Vector <BarcodeFormat> (1); DATA_MATRIX_FORMATS.add (BarcodeFormat. DATA_MATRIX); // a QR code }}

This class is mainly used to add some common bar code formats to the Vector <BarcodeFormat> set, which contains a dimension code and a QR code.

The last DATA_MATRIX is also a QR code.


2.After a while, DecodeThread. This is the decoding class.

We do not need to perform any operations here, as long as we know how to decode it here. The key point is here:

If (decodeFormats = null | decodeFormats. isEmpty () {decodeFormats = new Vector <BarcodeFormat> (); decodeFormats. addAll (DecodeFormatManager. ONE_D_FORMATS); // One-dimensional code decodeFormats. addAll (DecodeFormatManager. QR_CODE_FORMATS); // QR code decodeFormats. addAll (DecodeFormatManager. DATA_MATRIX_FORMATS );}


Here we add several constant classes that have been added to the decoding method, so that the decoding method has all the decoding formats, including the one-dimensional code and two-dimensional code.

3.The key point is CaptureActivity, a class that returns information after scanning.

Zxing source code is believed to be available to everyone, and we also know the value returned after scanning. There is a public void handleDecode (Result result, Bitmap barcode) in it. method,

The returned value is here:


// Scan result and code
Log. e ("encoding: ------>", result. getBarcodeFormat (). toString () + "data:" + result. getText ());


The key is this sentence:

Result. getBarcodeFormat (). toString () returns the encoding format;

Result. getText (); The returned value is the scan value.

Therefore, we need to determine whether the Vector Array in the encoding management class contains this field. iteration:


/*************************************** * ******* Note: in this example, the encoding format returned after the CAPTCHA is compared with the QR code encoding format in the DecodeFormatManager * class **. If the encoding format is the same as that in the DecodeFormatManager * class, the field is assigned a value of 1 (one-dimensional code) * If no, the field is assigned 2 (that is, the QR code) **************************************** * *****/int size = DecodeFormatManager. ONE_D_FORMATS.size (); // traverses the one-dimensional code character set for (int I = 0; I <size; I ++) {Log. e ("one-dimension code format ------>", DecodeFormatManager. ONE_D_FORMATS.get (I) + ""); // compare the encoding format returned after the CAPTCHA capture with the one-dimensional code encoding format in the DecodeFormatManager class. // If the encoding format is the same, the value of the field is 1 (that is, the one-dimensional code) otherwise, the field is assigned 2 (QR Code) if (DecodeFormatManager. ONE_D_FORMATS.get (I ). equals (mBarcodeFormat) {ONE_D_FORMATS = 1 ;}else {QR_CODE_FORMATS = 2 ;}}



Method 2:

Add two constants to the public void handleDecode (Result result, Bitmap barcode); method, and customize the QR_CODE and DATA_MATRIX constant codes. Then, when the scan returns, use the result. getBarcodeFormat (). toString () obtains the return code, and compares it with the custom code. Then define a string or int variable as the identifier, as shown below:

Int CODE_TYPE =-1; // indicates (1-dimension code, 2. Two-dimensional code 3. Other codes) final String QR_CODE = "QR_CODE "; // QR code final String DATA_MATRIX = "DATA_MATRIX"; // other codes // The scan code is not empty if (! TextUtils. isEmpty (result. getBarcodeFormat (). toString () {String mBarcodeFormat = result. getBarcodeFormat (). toString (); // The encoding format returned after the CAPTCHA capture is if (mBarcodeFormat. equals (DATA_MATRIX) {CODE_TYPE = 3;} else if (mBarcodeFormat. equals (QR_CODE) {CODE_TYPE = 2;} else {CODE_TYPE = 1;} Log. e ("---> (1-dimensional code, 2, two-dimensional code 3, other code)", "" + CODE_TYPE );

Then you can switch the CODE_TYPE label to determine which operations are performed.



Some bar code rules:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.