Mainly introduces the use of Zxing Library for barcode and QR code scanning
The library used in this post is someone else's streamlined
In the code comment inside the author- Ryan.tang
Here is the main introduction about the scanning interface
Here's a description of the properties
Scanview Core Source Code Analysis
Scanview Custom Properties
<declare-styleable name= "Scanview" > <!--Specify the background image of the scan area--<attr name= "Scanbackground" format= "re Ference "/> <!--Specify the size of the scan area--<attr name=" Scanwidth "format=" Dimension "/> <attr nam E= "Scanheight" format= "Dimension"/> <!--specify colors around the scan area--<attr name= "Aroundcolor" format= "Color" /> <!--the distance to the top of the scan area--<attr name= "scantopdistance" format= "Dimension"/> <!--scanning mode-- > <attr name= "Scanmode" format= "enum" > <enum name= "None" value= "0"/><!--nothing. <enum name= "line" value= "1"/><!--with scan cable--<enum name= "point" value= "2"/><!--flashing --<enum Name= "both" value= "3"/><!--All--</attr> <!--the height of the scan line-- <attr name= "Lineheight" format= "Dimension"/> <!--scan Line Color--<attr name= "LineColor" format = "Color"/> ≪! --Border Width--<attr name= "frameborder" format= "Dimension"/> <!--border Color--<attr Name= "fr Amecolor "format=" Color "/> <!--Specify a picture of the scan line--<attr name=" linesrc "format=" Reference "/> <!--Specify the color of the flicker point--<attr name= "Pointcolor" format= "Color"/> <!--scan mode state--<attr N Ame= "Scanstatus" format= "enum" > <enum name= "Slow" value= "1"/><!--slow-to-<enum name= "Normal" value= "0"/><!--Normal--<enum name= "Fast" value= "2"/><!--fast--</attr> <!--scan line scrolling mode--<attr name= "Linescrollmode" format= "enum" > <enum name= "Repeat" va Lue= "0"/><!--repeat--<enum name= "cycle" value= "1"/><!--Cycle---</attr> & lt;! --Scan timeout--<attr name= "Scantimevalue" format= "integer"/> <!--scan time units support seconds--<a TTR name= "ScantimEunit "format=" enum "> <enum name=" Second "value=" 0 "/> <enum name=" Minute "value=" 1 "/&G T </attr> </declare-styleable>
Draw around the scanning area
Divided into whether to set the background picture two kinds of situation
private void Drawaround (canvas canvas) {if (MSCANBG = = null) {canvas.drawrect (0, 0, screenwidth, scant Opdistance-frameborder, Mpaintshape); Canvas.drawrect (0, Scantopdistance-frameborder, Scanhorizontaldsitance-frameborder, scanTopDistance + Scanheight + frameborder, mpaintshape); Canvas.drawrect (scanhorizontaldsitance + scanwidth + frameborder, Scantopdistance-frameborder, ScreenWidth, Scantopdistance + scanheight + frameborder, mpaintshape); Canvas.drawrect (0, scantopdistance + scanheight + frameborder, ScreenWidth, ScreenHeight, Mpaintshape); } else {//Draw peripheral canvas.drawrect (0, 0, screenwidth, scantopdistance, Mpaintshape); Canvas.drawrect (0, Scantopdistance, scanhorizontaldsitance, Scantopdistance + scanheight, mPaintShape); Canvas.drawrect (scanhorizontaldsitance + scanwidth, scantopdistance, ScreenWidth, Scantopdistance + scanheight, mpaintshape); Canvas.drawrect (0, scantopdistance + scanheight, ScreenWidth, ScreenHeight, Mpaintshape); } }
Draw Scan Area
is divided into whether to set the background picture if you do not set a background picture to draw a box according to the specified size or default size
private void Drawscanarea (canvas canvas) { //Draw scan Area if (MSCANBG = = null) {//no background picture set using default box mpaintshape.se Tstyle (Paint.Style.STROKE); Mpaintshape.setstrokewidth (frameborder); Mpaintshape.setcolor (FrameColor); draw a border canvas.drawrect (Scanhorizontaldsitance-frameborder, Scantopdistance-frameborder, Scanhorizontaldsitance + scanwidth + frameborder, scantopdistance + scanheight + frameborder, mPaintShape); } else {//Set background canvas.drawbitmap (MSCANBG, Scanhorizontaldsitance, Scantopdistance, Mpaintbitmap); } }
Draw scan Lines
private void DrawLine (canvas canvas) {if (mscanline! = null) {Canvas.drawbitmap (Mscanline, Scanhorizontaldsitance + (Scanwidth-mscanline.getwidth ())/2, Linepositionheight, Mpaint BITMAP); } else {Mpaintshape.setstyle (Paint.Style.FILL); Mpaintshape.setcolor (LineColor); Canvas.drawline (Scanhorizontaldsitance, linepositionheight, Scanhorizontaldsitance + scanWidth, LinePos Itionheight + lineheight, mpaintshape); } switch (linesrollmode) {case line_scroll_mode_repeat:if (Linepositionheight > Scan Topdistance + scanheight) linepositionheight = scantopdistance; Linepositionheight + = Linemovespeed; Break Case Line_scroll_mode_cycle:if (!upflag) {linepositionheight + = Linemovespeed; } else {LineposiTionheight-= Linemovespeed; } if (Linepositionheight > scantopdistance + scanheight) Upflag = true; if (Linepositionheight < scantopdistance) Upflag = false; Break } }
The drawLine can achieve the same effect as a UC sweep by controlling the height of the rectangle.
Add the following code
Mpaintshape.setcolor (Color.parsecolor ("#4455ff55")); Canvas.drawrect (scanhorizontaldsitance, scantopdistance, scanhorizontaldsitance + scanwidth, Linepositionheight, Mpaintshape);
Draw a flashing point
private void Drawpoint (canvas canvas) {if (Currentpoints.isempty ()) {lastpoints = null; } else {//records the current data to the previous position set in lastpoints = Currentpoints; Sets the brush Mpaintshape.setalpha (0xff) that draws the blinking point; Mpaintshape.setcolor (Pointcolor); Mpaintshape.setstyle (Paint.Style.FILL); Loop draw for (Resultpoint point:currentpoints) {//Draw flash point canvas.drawcircle (scan Horizontaldsitance + point.getx (), Scantopdistance + point.gety (), 6.0f, Mpaintshape); }} if (lastpoints! = null) {//sets the Brush Mpaintshape.setalpha (0XFF/2) that draws the blinking point; Mpaintshape.setcolor (Pointcolor); Mpaintshape.setstyle (Paint.Style.FILL); Loop draw for (Resultpoint point:lastpoints) {//Draw Flicker Point canvas.drawcircle (scanhor Izontaldsitance + point.getx (), Scantopdistance + point.gety (), 3.0f, Mpaintshape); }}//Empty This data currentpoints.clear (); }
Data source for flashing points
This method is automatically called internally
public void Addpossibleresultpoint (Resultpoint point) { // adds the dot to the collection Currentpoints.add (points); }
Jump captureactivity when in use
Full library file
Go through it.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Scan barcodes and QR codes using zxing