Fixed Android zxing-based QR code scanning--free switching between the screen

Source: Internet
Author: User

Overview:

This blog is based on the open source framework zxing. zxing 1d/2d Barcode Image processing Library in various formats implemented in Java, which contains ports that are linked to other languages. Zxing can be implemented using the phone's built-in camera to complete the barcode scanning and decoding. This project can be implemented by barcode encoding and decoding.


Resources Download:

http://download.csdn.net/detail/u013761665/8853561


Modify to display vertical screen:

1th Step:

Enter the Captureactivity class under the Com.google.zxing.client.android package:

Note the following code:

if (Prefs.getboolean (Preferencesactivity.key_disable_auto_orientation, True)) {            setrequestedorientation ( Getcurrentorientation ());        } else {            setrequestedorientation (activityinfo.screen_orientation_sensor_landscape);        }

2nd Step:

Enter the Decodehandler class under the Com.google.zxing.client.android package:

In the Code

Planaryuvluminancesource Source = Activity.getcameramanager (). Buildluminancesource (data, width, height);
added above:

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; Here we is swapping, that's the difference to #11            width = height;            height = tmp;            data = Rotateddata;


3rd Step:

Enter the Cameraconfigurationmanager class under the Com.google.zxing.client.android.camera package:

Under LOG.I (TAG, "screen resolution:" + screenresolution), add the following:

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;} Cameraresolution = cameraconfigurationutils.findbestpreviewsizevalue (parameters, Screenresolutionforcamera);


4th Step:

Enter under the Com.google.zxing.client.android.camera bag. Cameraconfigurationmanager class:

Add the above code camera.setparameters (parameters):

Camera.setdisplayorientation (90);


5th step:

Enter under the Com.google.zxing.client.android.camera bag. Cameramanager class:

Put the 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 with:

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;


free switching between the screen and the other:

The above 5-step operation will make the original horizontal screen scan into a vertical screen scan. But it can only be in one direction. If you want users to be able to use both horizontal and vertical screens, do the necessary processing.

The main idea is to set a flag to flag the current screen status. Flag is transformed at the onresume of the scanning interface captureactivity and provides getter interfaces externally. However, one of the other uses in my code is to use the Eventbus framework to publish information.


The contrast chart in the case of the screen:


Vertical screen



Horizontal screen



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Fixed Android zxing-based QR code scanning--free switching between the screen

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.