Android Development: zxing Barcode Scanning-vertical Screen solutions

Source: Internet
Author: User

Write it in front. thanks to the zxing team, Project: Https://github.com/zxing/zxing, after the project is downloaded, Android barcode scanning mainly uses three modules: Android,android-core,core. Zxing Project after decompression directory structure:

Android is the zxing provided barcode scanner APK client source code, Android-core and core are required class library. The zxing vertical screen scanning solution provided in this article uses the Android version number versionname to 4.7.4,versioncode for the 104,zxing Class library version number 3.2.0, theoretically 4.7.x generic.
Zxing class Library cannot be directly imported into eclipse, the author adopts the new project Zxlib, the Java code in the Android-core,core in the copy, Note that there is only one Cameraconfigurationutils.java class in Android-core, and the main class library is the Core,cameraconfigurationutils.java package name: Com.google.zxing.client.andr Oid.camera, you need to copy the corresponding package name corresponding to the core class library, and finally don't forget to set this project as the library.
Friendship Hint: This article provides the solution, the class library source code does not make any modification, the following mentioned modification part is the client source code.

Zxing How to scan vertical screen After importing zxing source Android into Eclipse, associate Zxlib, which is the Zxing class library just created. After running can directly use barcode scanning function, but the default is horizontal screen, the author of the project needs to use vertical screen scanning, so need to zxing barcode scanning to vertical screen scanning. The steps are written in some detail, please read patiently.

First Step: Modify the Androidmanifest project list and change the Screenorientation property of Captureactivity to Portrait in Androidmanifest:

<activity android:name= ". Captureactivity "              android:screenorientation=" Portrait "              android:cleartaskonlaunch=" true "              Android: Statenotneeded= "true"              android:theme= "@style/capturetheme"              android:windowsoftinputmode= " Statealwayshidden ">


Step Two: 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 Three: add 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 been vertical screen, I believe many people have changed to the problem here, Zxing Barcode Scanner Camera Preview direction rotated 90 degrees, and easy to stretch, we need to correct the camera preview direction.


Fourth Step: Add code in Cameraconfigurationmanager's Setdesiredcameraparameters method:

Camera.setdisplayorientation (//add); This code function is to rotate the lens 90 degrees, so that the camera preview direction correctly display camera.setparameters (parameters);
Camera Preview is normal, there is no stretching, but the two-dimensional code recognition is much slower, and one-dimensional barcode identification, need horizontal screen scanning can be recognized, and bar code imaging is still horizontal, the following steps will solve the problem.


Fifth Step: 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;

Sixth Step: Modify Decodehandler in decode (byte[] data, int width, int height) method:

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;


Conclusion only the fifth step, and no sixth step, throws an exception in the Cameraconfigurationutils 283 row of the Zxing class library

IllegalStateException ("Parameters contained no preview size!");
If the sixth step is taken without the fifth step, the barcode will not be recognized properly.

Well, after the above six steps, Zxing Barcode scanner has been able to scan the normal vertical screen, the rest of the work is to streamline the code, I wish everyone successfully completed the project.
Reprint Please specify the author xiong_it and Source: http://blog.csdn.net/xiong_it/article/details/46983293, thank you!

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

Android Development: zxing Barcode Scanning-vertical Screen solutions

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.