A few days of transplant, the following is the process of continuous improvement
A lot of pictures.
Extract the file directory as follows, where core is zxing decoding library, can be imported into Eclipse generated Jar;android folder is the core of the entire Android, open the camera and so on here, Android-core only a Java file inside the surface, Used to configure the camera.
I didn't create a jar for my core files, because it was no problem to put it directly into Android Studo, I made it into a library. The ID number of the resource file in the library can not be used (anyway, do not let me compile), directly into the static int value forget it. There are also the menu will be reported some wrong, but it doesn't matter, the back is to be deleted, follow the tips of Android studio to get it.
Android things in the client directory, we just move the code here, the core code does not need to engage. Which want WiFi, share, history, etc. code is useless, because I want to get only two things, first: Scan results, second: Generate two-dimensional code. Other deletions are good.
The original library is to support dynamic recognition of rotation, but such a function I think it is meaningless, in the sweep code when the change zone more egg pain, deleted, and directly in the manifest configuration, all right
Below I first concise directory first, at this time the camera data in the identification of the time or horizontal screen, but also need to change, but, not urgent, and then make it, now the directory is too bloated, calculate, or the core into a jar good.
Just throw the core code in. Project compilation do not choose jdk1.8, will be wrong ... Choose 1.7, anyway choose 1.8 Android Studio not to compile for me, my version is 2.1.2 is up to date ...
Direct export all, simple rough
Then put it in the Libs of Android studio.
The only thing that's left in the moment is Android. Now, the generation of two-dimensional code to get out first.
Navigate to the Shareactiviy is the jump to generate QR code activity
Two-dimensional code to display the page, the rest began to delete
Like so many implicit conditions are not necessary, delete some, left to generate two-dimensional code implicit on the line
Finally, there are three pages left:
The first camera, the second input text, the third one shows the two-dimensional code
Next, start removing the extra feature code in the directory
WiFi directory directly deleted, history (record historical behavior bar, did not look closely) is also directly deleted, clipboard Shear Board, book (should be search, did not look at) also do not need to cut!,result in the Supplement what supplement XXX seemingly do not need, delete!
The first simplification, a great deal of effort, deleted a lot of things, finally can show everything, back again will be streamlined!
Now proceed to solve the problem of the following picture:
The original app is a horizontal screen, and now become a vertical screen, it seems to be quite normal, but scanning on ...
As can be seen, the data is still horizontal screen, we have to do is to map the vertical screen window to the horizontal screen
Talk about the overall process:
1, initialize the Surfaceview, audio, scan time-out timing, mobile phone vibration
2, initialize in surfacecreated callback (Turn on hardware camera, calculate resolution, set camera parameters, calculate and analyze the matrix size of the two-dimensional code box , set camera Preview data callback, open preview)
3, onpreviewframe the camera preview data (data is horizontal) sent through SendMessage to Decodethread
4, Decodethread open a handler constantly processing sent over the data (extracting the data of the two-dimensional code rectangle, black and white, recognition) processed by SendMessage sent to live UI thread processing
5, Captureactivityhandler (UI handler) received the correct scan results, the phone vibrates, "drops" a sound, and through the Setresult to return the data
6, the main application received the corresponding data to display
below into the code analysis: The
Previewcallback is a fetch callback for the picture data, where Onpreviewframe () is the direct way to get the picture data of Byte, through which the data is sent to the Decodethread decoding thread, the loop is constant, This decoding thread is the thread that handles the handler, and eventually decodes the
Previewcallback in the Decodehandler class:
Decodethread:
Decodehandler:
The core of the
inside Focus: private void decode (byte[] data, int width, int height) This is the decoding function
It's a waste of time if you decode the incoming data. All the red boxes that method is to use a rectangle, frame the data, decoding only in the rectangle inside the solution, so scan the time to align that box is the case.
All now need to solve the mapping problem from that function. The
Red box is a rectangular box that determines the data. This mapping relationship is only suitable for camera rotation 90 degree case.
Modified! The
rotates (horizontally), but the rectangle is calculated as normal. Yes, I can.
Adjust the display rectangle size, position, here I modified the height, consistent with the width
Decoding management (because you can solve barcodes, etc.), I have removed the redundant here, I do not need one-dimensional code, two-dimensional code is enough.
But it's still not perfect. If I do not want to full screen, need the status bar, if changed to have a title, carefully found that the original 1080*1920 data squeeze to 1080*17xx (more than 17 less forget) in the space, graphics vertical is squeezed, such as.
I think, can not be squeezed, because it looks bad (I am perfect), so I think of a simple method, can only show part of it, because the sweep code does not need to see all, so that, the Surfaceview force set the height, so that although the image is less, But at least it won't be stretched. (There is a better way to find a weapon)
Back in the study, two deadly problems were found. The code is written according to the horizontal screen, in the search for the most appropriate resolution is not correct, need to change, mainly to the X and Y exchange, because the horizontal screen x is longer than Y, vertical screen y is longer than X.
The changes are in two places below this method.
The second problem is that the camera rotation problem, my phone is rotated 90 degrees, a rotation of 270 degrees, in the identification of the need to judge, otherwise there will be wonderful problems. 0 degrees and 180 have not been measured, no cell phone.
Finally, the basic problem solved, it made a package, the completion of the QR Code of the page has also been deleted, leaving a scan activity with a QR code display activity.
Here's how it works:
Continuous change for a long time, very tired, the code must be redundant, look at it
< key > Project code: http://download.csdn.net/detail/a756213932/9574643
Teach you to transplant zxing QR code, compatible with Android6.0