Android QR code scanning (zxing usage summary)

Source: Internet
Author: User

Android QR code scanning (zxing usage summary)

There are three main points:

1. Draw the interface, including four corners, scrolling up and down, and adding text to the canvas

2. Adjust the position of the QR code scan box.

3. Solve the Problem of image deformation.

 

1. Draw the onDraw () method in ViewfinderView. java on the page
@ Overridepublic void onDraw (Canvas canvas) {// scan box in the middle. Modify the size of the scan box and modify Rect frame = CameraManager in CameraManager. get (). getFramingRect (); int top = frame. top; int left = frame. left; int right = frame. right; int bottom = frame. bottom; frame = new Rect (left, top, right, bottom); // initialize the top and bottom if (! IsFirst) {isFirst = true; slideTop = frame. top; slideBottom = frame. bottom;} // obtain the width and height of the screen. int width = canvas. getWidth (); int height = canvas. getHeight (); // draw the shadow part outside the scan box, which consists of four parts. The scan box is displayed on the screen, bottom of the scan box to the bottom of the screen // left of the scan box to the left of the screen, right of the scan box to the right of the screen // Draw the exterior (I. e. outside the framing rect) darkenedpaint. setColor (resultBitmap! = Null? ResultColor: maskColor); canvas. drawRect (0, 0, width, frame. top, paint); canvas. drawRect (0, frame. top, frame. left, frame. bottom + 1, paint); canvas. drawRect (frame. right + 1, frame. top, width, frame. bottom + 1, paint); canvas. drawRect (0, frame. bottom + 1, width, height, paint); if (resultBitmap! = Null) {// Draw the opaque result bitmap over the scanning rectanglepaint. setAlpha (OPAQUE); canvas. drawBitmap (resultBitmap, frame. left, frame. top, paint);} else {// draw the corner on the side of the scan box. A total of 8 parts are painted. setColor (Color. GREEN); canvas. drawRect (frame. left, frame. top, frame. left + ScreenRate, frame. top + CORNER_WIDTH, paint); canvas. drawRect (frame. left, frame. top, frame. left + CORNER_WIDTH, frame. top + ScreenRate, paint); canvas. drawRect (frame. right-ScreenRate, frame. top, frame. right, frame. top + CORNER_WIDTH, paint); canvas. drawRect (frame. right-CORNER_WIDTH, frame. top, frame. right, frame. top + ScreenRate, paint); canvas. drawRect (frame. left, frame. bottom-CORNER_WIDTH, frame. left + ScreenRate, frame. bottom, paint); canvas. drawRect (frame. left, frame. bottom-ScreenRate, frame. left + CORNER_WIDTH, frame. bottom, paint); canvas. drawRect (frame. right-ScreenRate, frame. bottom-CORNER_WIDTH, frame. right, frame. bottom, paint); canvas. drawRect (frame. right-CORNER_WIDTH, frame. bottom-ScreenRate, frame. right, frame. bottom, paint); // draw the middle line. Every time you refresh the interface, the middle line moves down SPEEN_DISTANCEslideTop ++ = SPEEN_DISTANCE; if (slideTop> = frame. bottom) {slideTop = frame. top;} Rect lineRect = new Rect (); lineRect. left = frame. left; lineRect. right = frame. right; lineRect. top = slideTop; lineRect. bottom = slideTop + 18; canvas. drawBitmap (BitmapDrawable) (getResources (). getDrawable (R. drawable. qrcode_scan_line ))). getBitmap (), null, lineRect, paint); // The words in the scan box // paint. setColor (Color. WHITE); // paint. setTextSize (TEXT_SIZE * density); // paint. setAlpha (0x40); // paint. setTypeface (Typeface. create (System, Typeface. BOLD); // String text = put the QR code into the box to automatically scan; // float textWidth = paint. measureText (text); // canvas. drawText (text, (width-textWidth)/2, (float) (frame. bottom + (float) TEXT_PADDING_TOP * density), paint); Collection
 
  
CurrentPossible = possibleResultPoints; Collection
  
   
CurrentLast = lastPossibleResultPoints; if (currentPossible. isEmpty () {lastPossibleResultPoints = null;} else {possibleResultPoints = new HashSet
   
    
(5); lastPossibleResultPoints = currentPossible; paint. setAlpha (OPAQUE); paint. setColor (resultPointColor); for (ResultPoint point: currentPossible) {canvas. drawCircle (frame. left + point. getX (), frame. top + point. getY (), 6.0f, paint) ;}} if (currentLast! = Null) {paint. setAlpha (OPAQUE/2); paint. setColor (resultPointColor); for (ResultPoint point: currentLast) {canvas. drawCircle (frame. left + point. getX (), frame. top + point. getY (), 3.0f, paint) ;}// only refresh the content of the scan box. Do not refresh postInvalidateDelayed (ANIMATION_DELAY, frame. left, frame. top, frame. right, frame. bottom );}}
   
  
 

2. Adjust the position if full screen is not set, the position of the scan box and the position height of the image will have a 50px error. The formula for not full screen: (Y-title bar-status bar) /2-scan box height)/2 = Y axis coordinates as long as they are not full screen: there will be errors. As a result, the two-dimensional code in the box will be scanned back or away from the two-dimensional code. If it is set to full screen, there will be no error. Please let me know what causes CameraManager. getFramingRect () method in java
Public Rect getFramingRect () {Point screenResolution = configManager. getScreenResolution (); if (framingRect = null) {if (camera = null) {return null ;} /* determine the width and height of the QR code scan box at fixed scale resolution */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;} // calculates the y axis coordinate of the X axis, you can adjust the position int leftOffset = (screenResolution. x-width)/2; int topOffset = (screenResolution. y)/2-height)/2; framingRect = new Rect (leftOffset, topOffset, leftOffset + width, topOffset + height); Log. d (TAG, Calculated framing rect: + framingRect);} return framingRect ;}

3. The void setDesiredCameraParameters (Camera camera) method in CameraConfigurationManager. java

/** * Sets the camera up to take preview images which are used for both preview * and decoding. We detect the preview format here so that * buildLuminanceSource() can build an appropriate LuminanceSource subclass. * In the future we may want to force YUV420SP as it's the smallest, and the * planar Y can be used for barcode scanning without a copy in some cases. */void setDesiredCameraParameters(Camera camera) {// Camera.Parameters parameters = camera.getParameters();// Log.d(TAG, Setting preview size:  + cameraResolution);// parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);// setFlash(parameters);// setZoom(parameters);// //setSharpness(parameters);// //modify here// camera.setDisplayOrientation(90);// camera.setParameters(parameters);Camera.Parameters parameters = camera.getParameters();List
 
   supportedPreviewSizes = parameters.getSupportedPreviewSizes();int position = 0;if (supportedPreviewSizes.size() > 2) {position = supportedPreviewSizes.size() / 2 + 1;// supportedPreviewSizes.get();} else {position = supportedPreviewSizes.size() / 2;}int width = supportedPreviewSizes.get(position).width;int height = supportedPreviewSizes.get(position).height;Log.d(TAG, Setting preview size:  + cameraResolution);camera.setDisplayOrientation(90);cameraResolution.x = width;cameraResolution.y = height;parameters.setPreviewSize(width, height);setFlash(parameters);setZoom(parameters);// setSharpness(parameters);camera.setParameters(parameters);}
 


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.