Android Zxing adjusts the scanning area to optimize the image fetch speed

Source: Internet
Author: User

Zxing is a QR code scanning project provided by google.

The default scan area of the Demo itself is only 360*480, which requires a long distance to scan the entire QR code.

Therefore, we need to adjust the image size ourselves.

Make adjustments in the CameraManager. java class.

The default size is the following four parameters.

//  private static final int MIN_FRAME_WIDTH = 240;//  private static final int MIN_FRAME_HEIGHT = 240;//  private static final int MAX_FRAME_WIDTH = 480;//  private static final int MAX_FRAME_HEIGHT = 360;


You can increase the value according to the screen size: Minimum width and height; maximum width and height.

The parameter actually plays a role in the getFramingRect () method.

The following are provided in the original Demo:

/*** Calculates the framing rect which the UI shocould draw to show the user where to place the * barcode. this target helps with alignment as well as forces the user to hold the device * far enough away to ensure the image will be in focus. ** @ return The rectangle to draw on screen in window coordinates. */public Rect getFramingRect () {Point screenResolution = configManager. getScreenResolution (); if (framingRect = null) {if (camera = null) {return null;} // native int width = screenResolution. x * 3/4; if (width <MIN_FRAME_WIDTH) {width = MIN_FRAME_WIDTH;} else if (width> MAX_FRAME_WIDTH) {width = MAX_FRAME_WIDTH;} int height = screenResolution. y * 3/4; if (height <MIN_FRAME_HEIGHT) {height = MIN_FRAME_HEIGHT;} else if (height> MAX_FRAME_HEIGHT) {height = MAX_FRAME_HEIGHT;} 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 ;}

I changed the code to adapt to different screen sizes

Public Rect getFramingRect () {Point screenResolution = configManager. getScreenResolution (); if (framingRect = null) {if (camera = null) {return null;} // int width = screenResolution after modification. x * 7/10; int height = screenResolution. y * 7/10; int leftOffset = (screenResolution. x-width)/2; int topOffset = (screenResolution. y-height)/3; framingRect = new Rect (leftOffset, topOffset, leftOffset + width, topOffset + height); Log. d (TAG, "Calculated framing rect:" + framingRect) ;}return framingRect ;}

I occupy 7/10 of the screen width and height
Of course, if the image size is changed, it will occupy a little more memory... the scanning speed will be much faster.

The preceding figure shows the actual size of the read image.

The actual UI beautification is drawn in the ViewfinderView class.

If you have any shortcomings, please leave a message below. Thank you.

Hope to be useful to you

Http://download.csdn.net/detail/aaawqqq/7281577 Resources

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.