[Turn] "zxing-based coding and decoding practice" streamlined barcode scanner

Source: Internet
Author: User

Through the analysis of the "fix barcode" zxing Project source code interpretation (2.3.0 version, Android section), it is now possible to achieve the ultimate goal: streamlining barcode scanner and separating the encoding and decoding into two separate sections for quick integration into your project in the future.

Coding

In the previous analysis, the coding section has been disclosed, the core part of only two package. Now share the part to remove the sharing app, share bookmarks, and retain the ability to share the Clipboard and share contacts and encoded text:

The coding part of the logic is clear, the code is organized neatly, in the previous understanding zxing the overall structure of the foundation has been to do subtraction.

After streamlining, the project was named Xbarcodegenerator .

Features that you can implement include:

    • Share a contact

    • Share the Clipboard

    • Encode input text

The main interface layout has been slightly modified, and a new button has been added. Code Escrow to:zxing-simplification

Decoding

On the basis of barcode scanner, make the following adjustments:

    • Remove the coding section. Remove the entire Com.google.zxing.client.android.encode package and the corresponding layout

    • Remove the Share section. Remove Com.google.zxing.client.android.share package and corresponding layout

    • Thin settings. Remove the "Custom search URL", modify the headlight description, default to use autofocus, remove the search country, no continuous attention

    • Adjust the horizontal screen to display the vertical screen

    • Landscaping Viewfinderview (optional)

Adjusting the horizontal screen for vertical screen scanning is divided into 5 steps:

1. Adjust the direction of the captureactivity for vertical screen display

Modify the Androidmanifest.xml:

android:screenorientation= "Landscape"

For

android:screenorientation= "Portrait"

2. Adjust the camera preview screen orientation

Add a line of code at the end of the cameraconfigurationmanager.setdesiredcameraparameters:

Camera.setdisplayorientation (90);

Adjusting the clock orientation of the camera preview is consistent with the natural orientation of the phone's vertical screen. The method must be called before Startpreview, and the setting is invalid after the preview interface is displayed.

Reference: camera.setdisplayorientation (int)

3. Adjust the scan window size

Modify the Cameramanager.getframingrectinpreview () in the

Public synchronized Rect Getframingrectinpreview () {... rect.left = rect.left * cameraresolution.x/screenresolution.x; Rect.right = rect.right * cameraresolution.x/screenresolution.x;rect.top = rect.top * Cameraresolution.y/screenresolut Ion.y;rect.bottom = rect.bottom * Cameraresolution.y/screenresolution.y;framingrectinpreview = rect; ...}

Modified to:

Public synchronized Rect Getframingrectinpreview () {... rect.left = rect.left * cameraresolution.y/screenresolution.x; Rect.right = rect.right * cameraresolution.y/screenresolution.x;rect.top = rect.top * Cameraresolution.x/screenresolut Ion.y;rect.bottom = rect.bottom * Cameraresolution.x/screenresolution.y;framingrectinpreview = rect; ...}

Since the initial orientation of the screen has been modified, the phone resolution is changed from the original width*height to the Height*width form, but the camera resolution is fixed, so there is a need to make some adjustments to calculate the correct scaling ratio.

4. Set the scan box to square

    public synchronized rect getframingrect ()  {         if  (framingrect == null)  {             if  (camera == null)  {                 return null;             }             point screenresolution = configmanager.getscreenresolution ();             if  (screenresolution == null)   {                //  called early, before init even finished                 return null;            }             int width =  Finddesireddimensioninrange (screenresolution.x, min_frame_width,                     max_frame_width);             //  Set the scan box to a square              int height = width;             int leftOffset =  (screenresolution.x -  width)  / 2;            int  topoffset =  (screenresolution.y - height)  / 2;             framinGrect = new rect (Leftoffset, topoffset, leftoffset + width, topoffset  + height);                         LOG.D (tag,  "Calculated framing rect :  " + framingrect);        }                 return framingRect;     }

Let Height=width

5. Invert the scanned graphic

To modify the Decodehandler.decode method, add the following code

Private void decode (byte[] data, int width, int height)  {long  Start = system.currenttimemillis (); result rawresult = null;//  new inversion data code starts 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;//  Added end Planaryuvluminancesource source = activity.getcameramanager (). Buildluminancesource ( Rotateddata,width, height);if  (source != null)  {BinaryBitmap bitmap =  The New binarybitmap (source);try {//  Preview interface eventually takes a bitmap and decodes it Rawresult  = multiformatReader.decodewithstate (bitmap);}  catch  (readerexception re)  {// continue} finally {multiformatreader.reset ( );}} ...

If you want to imitate a scan window, you can refer to "based on Google zxing QR code, barcode scanning, imitation QR code scanning effect"

After streamlining the project named Xbarcodescanner, The code is managed to:zxing-simplification

[Turn] "zxing-based coding and decoding practice" streamlined barcode scanner

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.