Android development: ZXing barcode scan-vertical screen solution, androidzxing

Source: Internet
Author: User

Android development: ZXing barcode scan-vertical screen solution, androidzxing

Thanks to the ZXing team, Project: workshop. Directory structure after zxing project decompression:

Android is the source code of the bar code scanner apk client provided by ZXing. android-core and core are required class libraries. The zxing portrait scan solution provided in this article uses the android version versionName 4.7.4, versionCode 104, and zxing class library version 3.2.0, which is generally used in theory 4.7.x.
The zxing class library cannot be directly imported into Eclipse. the method used by the author is to copy the java code in android-core and core after creating the project ZXLib. Note that there is only one CameraConfigurationUtils in android-core. java class. The main class libraries are core and CameraConfigurationUtils. java package name: com. google. zxing. client. android. camera. When copying the file, it must correspond to the corresponding package name of the core class library. Do not forget to set this project as library.
Tip: This article provides a solution. The source code of the class library is not modified. The modifications mentioned below are client source code.

After zxing imports zxing source code android into Eclipse, ZXLib is associated, that is, the newly created ZXing class library. After running, you can directly use the barcode scanning function, but the default is a horizontal screen. In my project, you need to use the portrait scan method, so you need to change the zxing barcode scan to the portrait scan method. The steps are detailed. Please read them carefully.

Step 1: Modify the AndroidManifest project list, and change the screenOrientation attribute of CaptureActivity in AndroidManifest to portrait:

<activity android:name=".CaptureActivity"              android:screenOrientation="portrait"              android:clearTaskOnLaunch="true"              android:stateNotNeeded="true"              android:theme="@style/CaptureTheme"              android:windowSoftInputMode="stateAlwaysHidden">


Step 2: delete the useless code in the onResume method in CaptureActivity:

//    if (prefs.getBoolean(PreferencesActivity.KEY_DISABLE_AUTO_ORIENTATION, true)) {//      setRequestedOrientation(getCurrentOrientation());//    } else {//      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);//    }

Step 3: add the code at the end of the onCreate method in CaptureActivity:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);    } else {        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);    }
After the first three steps, the barcode scanner has a portrait screen. I believe many people may encounter problems here. The zxing barcode scanner camera is rotated 90 degrees in preview direction and is easy to stretch, we need to correct the camera preview direction.


Step 4: add the code in the setDesiredCameraParameters method of CameraConfigurationManager:

Camera. setDisplayOrientation (90); // add this code is used to rotate the lens 90 degrees, so that the camera preview direction is correctly displayed in camera. setParameters (parameters );
The camera preview is normal and does not stretch, but the QR code recognition is much slower, and the one-dimensional bar code cannot be identified, which can only be identified by horizontal screen scanning, and the barcode imaging is still horizontal, the following steps solve the problem.


Step 5: Modify the getFramingRectInPreview method in CameraManager:

//      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;        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;

Step 6: Modify the decode (byte [] data, int width, int height) method in DecodeHandler:

PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);
Add code before

    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;


The conclusion is only the fifth step, but not the sixth step. An exception will be thrown in line 283 of the zxing class library's CameraConfigurationUtils.

IllegalStateException("Parameters contained no preview size!");
If only step 6 is performed, but step 5 is not, the Bar Code cannot be recognized normally.

Now, after the six steps above, the zxing barcode scanner can scan the screen normally. The rest of the work is to streamline the code and wish you a smooth completion of the project.
For more information, see the source of xiong_it and the original article: http://blog.csdn.net/xiong_it/article/details/46983293,thank you!

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.