Overview:
If you download the zxing source code and the Zxing Core.jar package, you can now make a series of changes to customize your QR code scan.
1. Modify the screen
This step is in fact relatively more complex. Because there is a need for two different aspects of the change, one is the screen of the switch, the other is the image scaling modification.
(1) Modify the properties of the Captureactivity class in manifest to android:screenorientation= "sensor"
(2) Modify the Captureactivity class to invoke the following code in the Onresume method:
private void SetOrientation () { WindowManager manager = (WindowManager) getsystemservice (Context.window_service); mrotation = Manager.getdefaultdisplay (). Getrotation (); Boolean orientationflag = true; int screenstate = CaptureActivity.this.getResources (). GetConfiguration (). Orientation; if (screenstate = = configuration.orientation_landscape) { Orientationflag = false; Morientationflag = false; } else if (screenstate ==configuration.orientation_portrait) { Orientationflag = true; Morientationflag = true; } Eventbus.getdefault (). Post (Orientationflag, reset_orientation); }
(3) Modify the Decodehandler class
In the Code
Planaryuvluminancesource Source = Activity.getcameramanager (). Buildluminancesource (data, width, height);
, add the following code above:
if (mportraitflag) { 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; Here we is swapping, that's the difference to #11 width = height; height = tmp; data = Rotateddata; }
The acquisition of Mportraitflag is achieved via an Android bus event:
@Subscriber (tag = "reset_orientation") private void Updateflag (Boolean flag) { Mportraitflag = flag; }
(4) in the Initfromcameraparameters method of the Cameraconfigurationmanager class
Point Screenresolutionforcamera = new Point (); The following code is added below the code:
if (Captureactivity.getorientationflag ()) { screenresolutionforcamera.x = screenresolution.x; SCREENRESOLUTIONFORCAMERA.Y = SCREENRESOLUTION.Y; if (Screenresolution.x < SCREENRESOLUTION.Y) { screenresolutionforcamera.x = SCREENRESOLUTION.Y; SCREENRESOLUTIONFORCAMERA.Y = screenresolution.x; } Cameraresolution = cameraconfigurationutils.findbestpreviewsizevalue (parameters, Screenresolutionforcamera); } else { cameraresolution = cameraconfigurationutils.findbestpreviewsizevalue (parameters, screenresolution); }
(5) in the In the Setdesiredcameraparameters method of the Cameraconfigurationmanager class
Camera.setparameters (parameters); Add the following code above the code:
String model = Android.os.Build.MODEL; int extraangle = 0; if (Model.equalsignorecase ("MediaPad")) { extraangle = -90; } else if (Model.startswith ("EBEN")) { Extraangle = -90; } Switch (Captureactivity.getrotationflag ()) {case surface.rotation_0: camera.setdisplayorientation ((360 + + extraangle); break; Case SURFACE.ROTATION_90: camera.setdisplayorientation ((0 + extraangle); break; Case surface.rotation_180: camera.setdisplayorientation ((+ + + extraangle); break; Case surface.rotation_270: camera.setdisplayorientation ((+ + + extraangle); break; Default: Break ; }
Here is a simple fit for some of the models.
(6) in the Getframingrectinpreview method of the Cameramanager class, make the following modifications:
if (mportraitflag) { rect.left = rect.left * cameraresolution.y/screenresolution.x; Rect.right = Rect.right * cameraresolution.y/screenresolution.x; Rect.top = Rect.top * CAMERARESOLUTION.X/SCREENRESOLUTION.Y; Rect.bottom = Rect.bottom * CAMERARESOLUTION.X/SCREENRESOLUTION.Y; } else { Rect.left = rect.left * cameraresolution.x/screenresolution.x; Rect.right = Rect.right * cameraresolution.x/ screenresolution.x; Rect.top = Rect.top * CAMERARESOLUTION.Y/SCREENRESOLUTION.Y; Rect.bottom = Rect.bottom * CAMERARESOLUTION.Y/ screenresolution.y; }
2. Modify the border size of the scan rectangle
We use the Finddesireddimensioninrange method in the Cameramanager class to modify the size of the sweep rectangle and the edge length ratio.
int Dim = 5 * RESOLUTION/8;
The above code in 5 and 8 is used to modify the value of the rectangle size, you can try to practice.
3. Modify the character encoding of the scan results
1th Step:
In the Com.google.zxing.client.android.CaptureActivity class,
CharacterSet = Intent.getstringextra (Intents.Scan.CHARACTER_SET);
This sentence is modified to:
CharacterSet = "Iso-8859-1";
2nd Step:
In the Handledecodeinternally method, modify the following code:
Charsequence displaycontents = resulthandler.getdisplaycontents (); String decodecontent = ""; try { decodecontent = new String (displaycontents.tostring (). GetBytes (CharacterSet), "UTF-8"); } catch ( Unsupportedencodingexception e) { e.printstacktrace (); }
The above two steps are to convert the encoding format to UTF-8 format.
4. Add Four corners of the sweep rectangle
For Com.google.zxing.client.android package under Viewfinderview class
/** * Draw Four Corners on a scan box * @param canvas * @param frame */private void Drawfourcorner (canvas canvas, Rect fra Me) {Paint.setcolor (Getresources (). GetColor (R.color.green)); Upper left corner canvas.drawrect (Frame.left, Frame.top, Frame.left +, Frame.top + ten, paint); Canvas.drawrect (Frame.left, frame.top, Frame.left + ten, Frame.top + +, paint); upper right corner canvas.drawrect (frame.right-30, Frame.top, frame.right, Frame.top + ten, paint); Canvas.drawrect (frame.right-10, Frame.top, Frame.right, Frame.top +, paint); Lower left corner canvas.drawrect (Frame.left, frame.bottom-10, Frame.left +, frame.bottom, paint); Canvas.drawrect (Frame.left, frame.bottom-30, Frame.left + ten, Frame.bottom, paint); Lower right corner Canvas.drawrect (frame.right-30, frame.bottom-10, Frame.right,frame.bottom, paint); Canvas.drawrect (frame.right-10, frame.bottom-30, Frame.right,frame.bottom, paint); }
Effect:
5. Modify the lines in the scan rectangle
Here we have three ways to display the scanned lines:
(1) Raw Red Line
(2) A customizable line that moves
(3) Customize a moving picture
The implementation code is as follows:
(1) native red lines
There is nothing specifically modified here, just a proper encapsulation of the module that draws the line, in order to better allow the "user" to choose which line should be used more appropriately.
/** * Native Scan lines * * @param canvas * @param frame */private void drawslidingrawline (canvas canvas, rect frame) {Rect preview Frame = Cameramanager.getframingrectinpreview ();p aint.setcolor (lasercolor);p Aint.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) Previewfra Me.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 (frameleft + (int) (POINT.GETX () * ScaleX), frametop+ (int) (POI Nt.gety () * ScaleY), point_size, Paint);}}} if (currentlast! = null) {Paint.setalpha (CURRENT_POINT_OPACITY/2);p Aint.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);}}}
(2) A customizable line that moves
/** * Below is a gradient line as the scan line * * @param canvas * @param frame */private void drawslidingsolidline (canvas canvas, Rect frame) {/ /gradient graph is rectangular mdrawable.setshape (gradientdrawable.rectangle);//gradient graph is Linetype mdrawable.setgradienttype ( gradientdrawable.linear_gradient);///Four fillet radii of the linetype rectangle mdrawable.setcornerradii (new float[] {8, 8, 8, 8, 8, 8, 8, 8});//Position Boundary Mr Ect.set (Frame.left + ten, Frame.top + i, frame.right-10, frame.top + 3 + i);//Set gradient map fill boundary mdrawable.setbounds (mrect);//Draw Gradual Variable line mdrawable.draw (canvas);}
(3) Customize a moving picture
/** * below for picture as Scan line * * @param canvas * @param frame */private void drawslidingimage (canvas canvas, Rect frame) {mrect.se T (frame.left-6, Frame.top + i-6, Frame.right + 6, frame.top+ 6 + i); Linedrawable.setbounds (Mrect); Linedrawable.draw (ca Nvas);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. http://blog.csdn.net/lemon_tree12138
Based on the QR code of zxing, you can transform it like this.