Original Sticker Address: http://www.cnblogs.com/Fndroid/p/5540688.html
Two-dimensional code in fact there are many kinds, but we commonly use a kind of code called QRCode , like the following, can be assured that the sweep, this is just my blog homepage link:
QR code encoding of the two-dimensional code, we need to know a few features:
1. Scanning can be swept from all angles, that is, how many degrees of rotation does not matter, do not believe it? The next time you go to KFC, try it.
2. The two-dimensional code has the fault tolerance rate, the greater the fault tolerance, the more complex the generated QR code, but the less prone to error, and when the QR code is obscured, the more easily scanned out. Here I upload the QR code fault tolerance is 30%, can start scanning from the upper left corner, probably scan to the extent of the time can be identified:
3. Two-dimensional code of the character content is limited, and the more content, the more complex QR code. If you want your QR code easily scanned by some low-pixel phones, try not to be too complicated.
Note: Two-dimensional code generation can be done through a variety of web sites, only need to input content to get.
Now that the QR code is generated, we need to know how to scan with a mobile phone and get the content represented by the QR code, here's a simple third-party library:Barcodescanner
Through this library, we can directly write an activity for capturing and identifying, and then process the results with the results returned by the activity, in the following steps:
1. Add dependencies (Android Studio Search Barcodescanner directly in the library dependency)
Compile ' me.dm7.barcodescanner:zxing:1.8.4 '
2. Create an activity with the code as follows:
public class Scanneractivity extends Appcompatactivity implements Zxingscannerview.resulthandler { private Zxingscannerview Mzxingscannerview; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Mzxingscannerview = new Zxingscannerview (this); Use Zxingscannerview as the layout Setcontentview (Mzxingscannerview); } @Override protected void Onresume () { super.onresume (); Mzxingscannerview.setresulthandler (this); Set processing result callback Mzxingscannerview.startcamera ();//Open Camera } @Override protected void OnPause () { super.onpause (); Mzxingscannerview.stopcamera (); Turn off the camera when the activity loses focus } @Override public void Handleresult (result result) {//Implements callback interface, callbacks data and ends activity Intent data = new Intent (); Data.putextra ("Text", Result.gettext ()); Setresult (RESULT_OK, data); Finish (); }}
3. Open this activity in the main activity and process the returned data:
public class Homeactivity extends Appcompatactivity { private TextView mtextview; Private WebView Mwebview; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_home); Mtextview = (TextView) Findviewbyid (r.id.tv); Mwebview = (WebView) Findviewbyid (R.ID.WV); } public void Scancode (view view) { Startactivityforresult (new Intent (this, scanneractivity.class), 1); } @Override protected void onactivityresult (int requestcode, int resultcode, Intent data) { Super.onactivityresult (Requestcode, ResultCode, data); if (ResultCode = = RESULT_OK) { mtextview.settext (Data.getstringextra ("text"));//Display recognized text Mwebview.loadurl (Data.getstringextra ("text")); Load the identified content as a URL into WebView }}}
4. Add Webcam and access network permissions:
<uses-permission android:name= "Android.permission.CAMERA"/><uses-permission android:name= " Android.permission.INTERNET "/>
The method of using this tool class is very simple, only need to use a zxingscannerview as the whole activity layout, and then set the callback interface to parse the success, implement callback method to return the data to the main activity.
Of course, if you need a custom scan interface effect, it's not that simple.
Development of scanning QR code developed by Android