Custom QR code scanning open-source library ZXing and open-source zxing

Source: Internet
Author: User

Custom QR code scanning open-source library ZXing and open-source zxing

(Sorry, the article is still being modified but accidentally published =)

I recently used the ZXing open-source library for QR code scanning. I would like to share with you some experience in code modification and cropping during the development process.

My code library:

Https://github.com/SickWorm/ZXingDialog

The code is not maintained on github, so there is no log. However, I added the "@ ch" comment for all the modifications to facilitate location locating.

Official source code:

Https://github.com/zxing/zxing

 

Functions:

1. Function cropping (only the QRCode QR code scanning function is retained, and other code scanning functions such as bar code are removed)

2. Remove resource dependencies and provide the code scanning function in the form of Dialog.

3. API compatibility (the source code is only compatible with more than 4.0 and is now compatible with 2.1)

4. Convert to portrait screen (source code: Landscape screen)

5. code scanning speed optimization (mainly divided into three points, only one point is completed)

6. device compatibility (for low-resolution devices)

This article also mentions:

7. Custom Interface

8. Optimization and debugging methods

 

1. Establish a project

ZXing Source Code does not provide a complete instance project for us to use. To build a project, we need three folders under the source code:

Core/

Android-core/

Android/

The procedure is as follows:

1. Create a new project

2. overwrite all the files in the android directory to the new project (including the files required to build the app, such as resource files and AndroidManifest. xml)

3. Upload all android-core Java files to the src directory (note! The src folder in android-core needs to be modified. The original path is android-core \ src \ main \ java \ com \ google \ zxing \ client \ android \ camera, we need to remove the main \ java two-layer folder in the middle, otherwise the package path cannot be identified in Eclipse)

4. Upload All Java files under the core directory to the src directory (note! Remove the main/java two-tier folder as in step 3 ). In this way, ZXing can be run, and my src directory is like this:

It can be run directly, and the effect is good. If you encounter some errors, it may be because the compiled JDK version is earlier than 1.7. ArrayList <> is used in the source code, which is not supported before 1.7. You can modify the source code or increase the JDK version.

But you may not be satisfied with this interface. The scan box is too large and the screen is full screen. API 15 (Android 4.0.3) is also required ). We will modify these requirements.

2. code optimization 1. Function cropping (only the QRCode QR code scanning function is retained, and other code scanning functions such as barcode are removed)

My goal is to keep the QR code only for recognition, without additional features. I don't want to explain this step in detail, because I don't remember it anymore .. You can view the results of the Code directly.

Which of the following can be deleted:

QR code in the format of com. google. zxing. aztec. ** aztec

Com. google. zxing. client. android. book. * functions related to Google Books

Com. google. zxing. client. android. clickboard. * not clear. copy and paste?

Com. google. zxing. client. android. encode. * is used to generate various codes.

Com. google. zxing. client. android. history. * Save the scan record

Com. google. zxing. client. android. result. ** functional Code related to the code scanning application function

Com. google. zxing. client. android. share. * share function

Com. google. zxing. client. android. wifi. * WiFi related, unclear Purpose

Com. google. zxing. datamatrix. ** QR code in datamatrix format

Com. google. zxing. maxicode. ** QR code in maxicode format

Com. google. zxing. multi. ** does it seem to be supported by multiple formats? I didn't use this package. If you have any concerns, please let me know.

Com. google. zxing. oned. ** one dimension one-dimensional code, that is, a bar code (you can find something strange when you search for oned on Baidu ..)

Com.google.zxing0000417. ** PDF417 format bar code

You need to modify the following:

Com. google. zxing. client. android. CaptureActivity: removes the Code related to other functions and only retains the core function, that is, the code scanning function. The Interface contains a SurfaceView in FrameLayout. I won't go into details about code removal. Let's look at the uploaded code directly. For this file, I refer to the upload.

Com. google. zxing. MultiFormatReader: this parameter specifies the format that supports decoding. You must remove all formats except QR_CODE. Otherwise, an error is returned because the decoding package is deleted. For more information, see the uploaded code.

Com. google. zxing. client. result. ProductResultParser: In the parse function, same as above.

Com. google. zxing. client. android. DecodeThread: In the constructor, same as above.

In addition:

Com. google. zxing. client. result I didn't delete the code for this package, and it should also be optimized.

2. Remove resource dependencies and provide the code scanning function in the form of Dialog.

After the streamlining of Step 4, there are actually only two places to be modified:

1. Scan the interface

2. Scan the beep sound file that is played when the video is successfully played.

 

1: after removing the remaining features, we only need a SurfaceView and a View on the painting interface for the core features. The Code is as follows:

    /**     * use Java code to build layout instead of xml file     * @ch     */    private void buildLayout() {        requestWindowFeature(Window.FEATURE_NO_TITLE);         FrameLayout layout = new FrameLayout(this);        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);        layout.setLayoutParams(params);        surfaceView = new SurfaceView(this);        surfaceView.setLayoutParams(params);        layout.addView(surfaceView);        viewfinderView = new ViewfinderView(this, null);        layout.addView(viewfinderView);        setContentView(layout);    }

ViewfinderView is the View that comes with ZXing. If you want to modify the interface, you can directly modify it. We will mention it.

2: Because my ultimate goal is to package it into a jar package, the beep file cannot be placed in res, but in assets.

// To be supplemented

 

3. API compatibility (the source code is only compatible with more than 4.0 and is now compatible with 2.1)

This part of modification is marked as // @ ch api compatible in the source code.

CaptureActivity. java: // @ ch api compatible if (VERSION. SDK_INT <11) {// surfaceview will push buffer automatically surfaceHolder. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS);} // you need to set it manually before API 11; otherwise, it cannot be displayed. The default setting is later than API11. CameraConfigurationManager. java: // @ ch api compatible if (VERSION. SDK_INT <13) {theScreenResolution. x = display. getWidth (); theScreenResolution. y = display. getHeight ();} else {display. getSize (theScreenResolution);} // getSize is a new API after API 13. getWidth and getHeight must be used before. AutoFocusManager. java // @ ch api compatible if (VERSION. SDK_INT> 11) {newTask.exe cuteOnExecutor (AsyncTask. THREAD_POOL_EXECUTOR);} else {newTask.exe cute ();} // The multi-thread mode is set in the source code. THREAD_POOL_EXECUTOR indicates that this task can run at most five threads simultaneously, wait for more than five instances. In versions earlier than API 11, this is the default option. In fact, there is only a single thread, so you can execute it at will. OpenCameraInterface. java: // @ ch api compatible if (VERSION. SDK_INT <9) {return openWithLowApi ();}...... /*** for lower than API 9 * @ ch api compatible */public static Camera openWithLowApi () {// If the device does not have a back-facing camera, this returns null Camera camera = Camera. open (); return camera;} // The open camera of the source code can distinguish between front and back cameras. However, before API 9, there was no concept of front camera, so I did a bit of work.

There are just a few places, but I have been here for an hour.

 

4. Convert to portrait screen (source code: Landscape screen)

ZXing is a horizontal screen by default, but our general apps will be made into a vertical screen. It is not good if the screen is forcibly switched to a horizontal screen when scanning the code. When I modified the ZXing portrait screen, I followed the portrait screen setting method of the general APP. The result showed that there was no good source code, and the Code had to be placed very small to complete. After the debugging process, we found that the scan code resolved area is different from the area on the screen, so we can see that this part of the modification has encountered a problem. Then I searched for an article from my predecessors and found that it was not completely changed.

 

5. code scanning speed optimization (mainly divided into three points, now only one point is completed) 6. device compatibility (for low-resolution devices) This article will also mention: 7. Custom interface 8. Optimization and debugging methods

Copyright. For more information, see the source:

Http://www.cnblogs.com/sickworm/p/4562081.html

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.