The Viewfinderview of zxing

Source: Internet
Author: User

Public final class Viewfinderview extends View {private static final int[] Scanner_alpha = {0, 64, 128, 192, 255, 192, 1  28, 64};  Private static final long animation_delay = 80L;  private static final int current_point_opacity = 0xA0;  private static final int max_result_points = 20;  private static final int point_size = 6;  Private Cameramanager Cameramanager;  Private final paint paint;  Private Bitmap Resultbitmap;  private final int maskcolor;  private final int resultcolor;  private final int lasercolor;  private final int resultpointcolor;  private int scanneralpha;  Private list<resultpoint> possibleresultpoints;  Private list<resultpoint> lastpossibleresultpoints;  This constructor was used when the class was built from an XML resource.    Public Viewfinderview (context context, AttributeSet Attrs) {Super (context, attrs);    Initialize these once for performance rather than calling them every time in OnDraw (). Paint = new Paint (Paint.anti_alias_flag); OpenKai Anti-aliasing Resources resources = getresources (); MaskColor = Resources.getcolor (r.color.viewfinder_mask);//cover layer Color Resultcolor = Resources.getcolor (R.color.result_    view);    Lasercolor = Resources.getcolor (R.color.viewfinder_laser);    Resultpointcolor = Resources.getcolor (r.color.possible_result_points);    Scanneralpha = 0;    possibleresultpoints = new arraylist<resultpoint> (5);  Lastpossibleresultpoints = null;  } public void Setcameramanager (Cameramanager cameramanager) {this.cameramanager = Cameramanager; } @SuppressLint ("Drawallocation") @Override public void OnDraw (canvas canvas) {if (Cameramanager = = null) {RE Turn Not ready yet, early draw before done configuring} rect frame = Cameramanager.getframingrect ();//Middle viewfinder Rect Previewframe = Cameramanager.getframingrectinpreview ();//zxing The official barcodescanner.apk decoding is successful when the upper left corner of the box holds the rectangle if (frame = = NULL ||    Previewframe = = null) {return; } int width = Canvas.getwidth (); Phone screen width int height = Canvas.getheight ();//the screen height//framing screen is divided into two pieces: a translucent piece of the outside (the shaded part), the middle of a full transparent piece. The translucent picture outside is made up of four rectangles (the top of the scan box is above the screen, the bottom of the scan box is below the screen, the left side of the scan box is to the left of the screen, the right side of the scan box to the right of the screen)//Draw the exterior (i.e outside the framing Rec    T) darkened paint.setcolor (resultbitmap! = null? resultcolor:maskcolor); Canvas.drawrect (0, 0, width, frame.top, paint);//Upper Rectangle Canvas.drawrect (0, Frame.top, frame.left, Frame.bottom + 1, Paint )///left Canvas.drawrect (frame.right + 1, frame.top, width, Frame.bottom + 1, paint);//Right Canvas.drawrect (0, Frame.bot  Tom + 1, width, height, paint);//Lower if (resultbitmap! = null) {//Draw the opaque result bitmap over the scanning      Rectangle Paint.setalpha (current_point_opacity);    Canvas.drawbitmap (Resultbitmap, NULL, frame, paint); } else {//Draw a red "laser scanner" line through the middle to show decoding is active Paint.setcolor (Laserco      LOR);      Paint.setalpha (Scanner_alpha[scanneralpha]); Scanneralpha = (scanneralpha + 1)% Scanner_alpha.leNgth;      int middle = frame.height ()/2 + frame.top;            Canvas.drawrect (Frame.left + 2, middle-1, frame.right-1, Middle + 2, paint);      float ScaleX = Frame.width ()/(float) previewframe.width ();      float ScaleY = Frame.height ()/(float) previewframe.height ();      list<resultpoint> currentpossible = possibleresultpoints;      list<resultpoint> currentlast = lastpossibleresultpoints;      int frameleft = Frame.left;      int frametop = Frame.top;      if (Currentpossible.isempty ()) {lastpossibleresultpoints = null;        } else {possibleresultpoints = new arraylist<resultpoint> (5);        Lastpossibleresultpoints = currentpossible;        Paint.setalpha (current_point_opacity);        Paint.setcolor (Resultpointcolor); Synchronized (currentpossible) {for (Resultpoint point:currentpossible) {canvas.drawcircle (Framele FT + (int) (POINT.GETX () * ScaleX), frametop + (int) (POINT.GEty () * ScaleY), point_size, paint);        }}} if (currentlast! = null) {Paint.setalpha (CURRENT_POINT_OPACITY/2);        Paint.setcolor (Resultpointcolor);          Synchronized (currentlast) {Float radius = point_size/2.0f;                              for (Resultpoint point:currentlast) {canvas.drawcircle (frameleft + (int) (POINT.GETX () * ScaleX),          Frametop + (int) (Point.gety () * ScaleY), radius, paint); }}}//Request another update at the animation interval, but only repaint the laser line,//Not T      He entire viewfinder mask. Postinvalidatedelayed (Animation_delay, Frame.left-point_size, Fram E.top-point_size, Frame.right + point_size, Frame.bottom + point_s    IZE); }} public void Drawviewfinder () {Bitmap ResulTbitmap = This.resultbitmap;    This.resultbitmap = null;    if (resultbitmap! = null) {resultbitmap.recycle ();  } invalidate ();   }/** * Draw a bitmap with the result points highlighted instead of the live scanning display.   * * @param barcode An image of the decoded barcode.    */public void Drawresultbitmap (Bitmap barcode) {resultbitmap = barcode;  Invalidate ();    } public void Addpossibleresultpoint (Resultpoint point) {list<resultpoint> points = possibleresultpoints;      Synchronized (points) {points.add (point);      int size = Points.size ();      if (Size > Max_result_points) {//Trim it points.sublist (0, SIZE-MAX_RESULT_POINTS/2). Clear (); }    }  }}



The Viewfinderview of zxing

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.