Zxing: key code for customizing and modifying the QR code

Source: Internet
Author: User

Zxing: key code for customizing and modifying the QR code

Official source code: http://code.google.com/p/zxing/downloads/list;

Decompress the downloaded zxing-x.x.zip file. We only need to use the sample project in the android directory,


Import the android project to eclipse, and do not forget to import the corresponding jar to libs. At this time, the sample project should be ready to run, but we do not need many functions of this project, and the scanning interface is horizontal, so you need to modify it.

Next, we will simplify the example project:


Step 1: Copy necessary packages and Classes
After the import is complete, there will be a lot of red forks, most of which are related to the package access permissions, because many classes in the sample code are final type, we just need to make them public;
Some associated files (color.xml+ids.xml+strings.xml,rawbeep.ogg) under values ).
After preliminary adjustment, the package structure is as follows:



Step 2: Modify PreferencesActivity and CaptureActivity


The sample project uses a lot of configurations, so the PreferencesActivity class is used in many places. It doesn't matter if you keep it, but don't forget to copy some associated files under res in the sample project (preferences. xml, arrays. xml );
However, PreferencesActivity is completely redundant, and it is also eye-catching. Therefore, I need to remove it and modify all the classes that use PreferencesActivity, that is, the remaining classes that report the Red Cross, we need to fix some configurations and remove unnecessary settings and leave no code here;
Similarly, CaptureActivity also has many methods that we don't need. Most of them are about the processing after decoding is successful. If you want to keep them, you need to copy a lot of classes and remove them.


Part 3: change to landscape
After the above two steps, we should be able to run our own project (do not forget to add permissions). Of course, this is a horizontal screen, so we need to modify several places to change it to a vertical screen:
1. comment out the following code in the initFromCameraParameters () method of the CameraConfigurationManager class:

if(width < height) {Log.i(TAG,"Display reports portrait orientation; assuming this is incorrect");int temp = width;width = height;height = temp;}

2. In the setDesiredCameraParameters () method of the CameraConfigurationManager class, add the following code before camera. setParameters (parameters:

camera.setDisplayOrientation(90);

3. In the getFramingRectInPreview () method of the CameraManager class, replace the following code:

rect.left = rect.left * cameraResolution.x / screenResolution.x;rect.right = rect.right * cameraResolution.x / screenResolution.x;rect.top = rect.top * cameraResolution.y / screenResolution.y;rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;

Replace

<span style="font-size:14px;">rect.left = rect.left * cameraResolution.y / screenResolution.x;rect.right = rect.right * cameraResolution.y / screenResolution.x;rect.top = rect.top * cameraResolution.x / screenResolution.y;rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;</span>


4. In the decode method of the DecodeHandler class, add the following code before activity. getCameraManager (). buildLuminanceSource:

byte[] rotatedData = new byte[data.length];for(int y = 0;y < height; y++) {   for(int x = 0; x < width; x++)       rotatedData[x * height + height - y - 1] = data[x + y * width];    } int tmp = width; width = height; height = tmp; data = rotatedData;

5. The key step is to solve the problem of image stretching after the portrait screen. In the initFromCameraParameters () method of the CameraConfigurationManager class:
Add the following code after Log. I (TAG, "Screen resolution:" + screenResolution:

Point screenResolutionForCamera = new Point();screenResolutionForCamera.x = screenResolution.x;screenResolutionForCamera.y = screenResolution.y;if(screenResolution.x < screenResolution.y) {screenResolutionForCamera.x = screenResolution.y;screenResolutionForCamera.y = screenResolution.x;}

At the same time, modify the next sentence to cameraResolution = findBestPreviewSizeValue (parameters, screenResolutionForCamera );
In addition, do not forget to set android: screenOrientation = "portrait" in manifest. Now, the portrait screen has been modified.

Step 4: Modify the position and size of the scan box
At this time, the scan box is a vertically stretched rectangle, Which is ugly. We can change it to a square or flat shape.
Replace the following code with the getFramingRect () method of the CameraManager class:
int width = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);int height = findDesiredDimensionInRange(screenResolution.y,MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);

Replace

DisplayMetrics metrics = context.getResources().getDisplayMetrics();int width = (int)(metrics.widthPixels * 0.6);int height = (int) (width * 0.9);

The size of the scan box is more flexible based on the screen resolution, and the offset topOffset is changed to (screenResolution. y-height)/4.


Step 5: Modify the four corners and scan lines in the scan box
The line in the sample code is centered and does not move. We can change it to a scanned line that is moved up or down, and change the style of the line.
Draw four corners in the onDraw () method in the ViewfinderView class of the custom scan layout. The key code is as follows:

// Draw four corners of paint. setColor (getResources (). getColor (R. color. green); // canvas in the upper left corner. drawRect (frame. left, frame. top, frame. left + 15, frame. top + 5, paint); canvas. drawRect (frame. left, frame. top, frame. left + 5, frame. top + 15, paint); // canvas in the upper right corner. drawRect (frame. right-15, frame. top, frame. right, frame. top + 5, paint); canvas. drawRect (frame. right-5, frame. top, frame. right, frame. top + 15, paint); // canvas in the lower left corner. drawRect (frame. left, frame. bottom-5, frame. left + 15, frame. bottom, paint); canvas. drawRect (frame. left, frame. bottom-15, frame. left + 5, frame. bottom, paint); // canvas in the lower right corner. drawRect (frame. right-15, frame. bottom-5, frame. right, frame. bottom, paint); canvas. drawRect (frame. right-5, frame. bottom-15, frame. right, frame. bottom, paint );

In addition, the key code is as follows:

If (I + = 5) <frame. bottom-frame. top) {/* use the gradient line as the scanning line below * // The gradient chart is a rectangle // mDrawable. setShape (GradientDrawable. RECTANGLE); // The gradient chart is linear // mDrawable. setGradientType (GradientDrawable. LINEAR_GRADIENT); // The four rounded corner radius of the linear rectangle mDrawable. setCornerRadii (new float [] {8, 8, 8, 8, 8, 8, 8}); // location boundary // mRect. set (frame. left + 10, frame. top + I, frame. right-10, // frame. top + 1 + I); // you can specify the border for filling the gradient chart. // mDrawable. setBounds (mRect); // draw a gradient line // mDrawable. draw (canvas);/* the image below is used as the scanning line */mRect. set (frame. left-6, frame. top + I-6, frame. right + 6, frame. top + 6 + I); lineDrawable. setBounds (mRect); lineDrawable. draw (canvas); // refresh invalidate ();} else {I = 0 ;}
Two line styles are used here. One is a gradient line and the other is a similar scanning line.


Sample source code project

Simplified source code .rar (2.38 MB, downloads: 293)


Android Zxing QR code scan interface Customization

The custom Photo Frame actually changes the layout of the layout file corresponding to your CaptureActivity class. The red line inherits a layout of view rewriting, which is available in many demos.
Search.apkbus.com/..form.a

How to Use the QR code of zxing to encode the code (such as A, B, C) into A QR code

Www.apkbus.com/..139981
Here is a QR code generation and identification tutorial shared by a netizen. It is accompanied by a Demo. Let's take a look at it and hope it will help you! Hope to adopt it!

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.