zxing QR Code Refinement (portrait, stretch, scan box size and Scan line movement)

Source: Internet
Author: User

This post was last edited by Levil_ad on 2013-12-30 13:55

recently nothing to do the next two-dimensional code scanning, with the zxing Open source code, the official source address: http://code.google.com/p/zxing/downloads/list;
I did it on the basis of ZXing2.2, so I only downloaded the zxing-2.2.zip;
Also need zxing core jar package,: http://repo1.maven.org/maven2/com/google/zxing/core/2.2/, just download Core-2.2.jar on the line;
To extract the downloaded zxing-2.2.zip, we only need to use the sample project in the Android directory,
<ignore_js_op>
Import the Android project into Eclipse, and don't forget to import the Core-2.2.jar into Libs, at which point the sample project should be ready to run, but there are many features that we don't need, and the scanning interface is horizontal, so we need to modify it.

Next we'll simplify the example project:
First step : Copy the necessary packages and classes
Create your own project and import Core-2.2.jar, copy all the necessary code from the sample project to your own project, and the features of each package and class in the sample project are not explained here, and you are interested to study it yourself;
I made a little change to the package structure, after the import is done there will be a lot of red forks, most of the access rights of the package, because many of the sample code is final type, we will be public on the line;
In addition, some associated files under res ( color.xml, Ids.xml, Strings.xml, and Beep.oggunder raw) are required.
The initial adjustment package structure is as follows:
<ignore_js_op>

Step two : preferencesactivity and captureactivity modification
The sample project uses a lot of configuration, so a lot of places are used preferencesactivity This class, it doesn't matter if you keep it, but don't forget to copy some associated files under Res in the example project (preferences.xml, Arrays.xml);
but preferencesactivitycompletely superfluous, and looked also an eyesore, so I Remove it, you need to use the Preferencesactivity class are modified, is the remaining those reported Red fork of the class, we need to fix some of the configuration, the extra set of judgment removed, here I do not paste code;
Also captureactivity there are many ways that we do not need, mostly about decoding the success of the processing, if you want to retain the additional copies of many classes, so it is removed, here also do not paste code, the attachment source has.

Part III: Modify to vertical screen
After two steps above, our own project should be able to run (don't forget to add permission., of course, is horizontal, so we need to modify several places to modify it to a vertical screen:
1.CameraconfigurationmanagerClass ofinitfromcameraparameters ()method to comment out the following code:
[Mw_shl_code=java,true]
if (width < height) {
LOG.I (TAG, "Display reports portrait orientation; Assuming this is incorrect");
int temp = width;
width = height;
Height = temp;
}[/mw_shl_code]

2.CameraconfigurationmanagerClass ofsetdesiredcameraparameters ()method, add the following code before camera.setparameters (parameters):
[Mw_shl_code=java,true]camera.setdisplayorientation (90); [/mw_shl_code]

3.CameramanagerClass ofGetframingrectinpreview ()method, replace the following code:
[Mw_shl_code=java,true]
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; [/mw_shl_code]
replaced by
[Mw_shl_code=java,true]
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; [/mw_shl_code]

4.DecodehandlerClass ofDecodemethod, add the following code before Activity.getcameramanager (). Buildluminancesource ():
[Mw_shl_code=java,true]
byte[] Rotateddata = new Byte[data.length];
for (int y = 0; y < height; y++) {
for (int x = 0; × < width; x + +)
Rotateddata[x * height + height-y-1] = data[x + y * Width];
}
int tmp = width;
width = height;
height = tmp;
data = Rotateddata; [/mw_shl_code]

5. A critical step to solve the image stretching problem after vertical screen. In the Initfromcameraparameters () method of the Cameraconfigurationmanager class:
In LOG.I (TAG, "screen resolution:" + screenresolution), add the following code:
[Mw_shl_code=java,true]
Point Screenresolutionforcamera = new Point ();
screenresolutionforcamera.x = screenresolution.x;
SCREENRESOLUTIONFORCAMERA.Y = SCREENRESOLUTION.Y;
if (Screenresolution.x < SCREENRESOLUTION.Y) {
screenresolutionforcamera.x = SCREENRESOLUTION.Y;
SCREENRESOLUTIONFORCAMERA.Y = screenresolution.x;
}[/mw_shl_code]
At the same time modify the next sentence for cameraresolution = Findbestpreviewsizevalue (Parameters,screenresolutionforcamera);

In additionManifestDo not forget to set the android:screenorientation= "portrait", so that the vertical screen modification is complete.

Fourth Step: Scan box position and size modification
The scan box at this point is a vertically stretched rectangle, which is hard to see, and we can modify it to a square or flat type.
CameramanagerClass ofGetframingrect ()method, replace the following code:
[Mw_shl_code=java,true]
int width = Finddesireddimensioninrange (screenresolution.x, Min_frame_width, max_frame_width);
int height = Finddesireddimensioninrange (screenresolution.y,min_frame_height, max_frame_height); [/mw_shl_code]
replaced by
[Mw_shl_code=java,true]
Displaymetrics metrics = context.getresources (). Getdisplaymetrics ();
int width = (int) (Metrics.widthpixels * 0.6);
int height = (int) (width * 0.9); [/mw_shl_code]
Here we are based on the screen resolution to set the size of the scan box more flexible, while the offset Topoffset modified to (screenresolution.y-height)/4

Fifth Step: Scan box Four Corners and scan line modification
The lines in the sample code are centered and motionless, and we can modify them to move up and down the sweep line, and you can change the style of the line.
Drawing in the OnDraw () method in the Viewfinderview class of the Custom scan layoutfour x Corners, the key code is as follows:
[Mw_shl_code=java,true]
Draw a four corner
Paint.setcolor (Getresources (). GetColor (R.color.green));
Upper left corner
Canvas.drawrect (Frame.left, frame.top, Frame.left + 15,frame.top + 5, paint);
Canvas.drawrect (Frame.left, frame.top, Frame.left + 5,frame.top + +, paint);
upper right corner
Canvas.drawrect (frame.right-15, Frame.top, Frame.right,frame.top + 5, paint);
Canvas.drawrect (frame.right-5, Frame.top, Frame.right,frame.top +, paint);
Lower left corner
Canvas.drawrect (Frame.left, frame.bottom-5, Frame.left + 15,frame.bottom, paint);
Canvas.drawrect (Frame.left, frame.bottom-15, Frame.left + 5,frame.bottom, paint);
Lower right corner
Canvas.drawrect (frame.right-15, frame.bottom-5, Frame.right,frame.bottom, paint);
Canvas.drawrect (frame.right-5, frame.bottom-15, Frame.right,frame.bottom, paint); [/mw_shl_code]

In addition, the scanned lines are modified toscan up and downLine, the key code is as follows:
[Mw_shl_code=java,true]
if ((i + = 5) < Frame.bottom-frame.top) {
/* Use a gradient line as the scan line */
Gradient graph is Rectangular
Mdrawable.setshape (Gradientdrawable.rectangle);
Gradient graph is Linetype
Mdrawable.setgradienttype (gradientdrawable.linear_gradient);
Four fillet radii of a linetype rectangle
Mdrawable
. setcornerradii (New float[] {8, 8, 8, 8, 8, 8, 8, 8});
Position boundaries
Mrect.set (Frame.left + ten, Frame.top + I, frame.right-10,
Frame.top + 1 + i);
Set gradient plot fill boundary
Mdrawable.setbounds (Mrect);
Draw a gradient line
Mdrawable.draw (canvas);

/* The picture as the scan line */
Mrect.set (frame.left-6, Frame.top + i-6, Frame.right + 6,frame.top + 6 + i);
Linedrawable.setbounds (Mrect);
Linedrawable.draw (canvas);

Refresh
Invalidate ();
} else {
i = 0;
}[/mw_shl_code]
Two line styles are used here, one is a gradient line, and the other is a similar picture scan line.
detailed code please see the attachment source.

Run as follows:
<ignore_js_op> This is a gradient line <ignore_js_op> This is a picture line, but the scan should be animated very smooth, here is used to draw several times

In addition, after the successful scan of the mobile phone shake and beep in the Beepmanager modified, inside I put two additional audio files

zxing QR Code Refinement (portrait, stretch, scan box size and Scan line movement)

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.