Android Development Learning Path-two-dimensional code learning

Source: Internet
Author: User

This month outfit force a little bit less, for what, because to test software shoot Chicken division, fast to Children's day, hurriedly write a Bo memorial for a lost youth, hmm, please ignore this sentence.

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:barcodescaner

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

Compile ' me.dm7.barcodescanner:zxing:1.8.4 '

2. Create an activity with the code as follows:

 Public classScanneractivityextendsAppcompatactivityImplementsZxingscannerview.resulthandler {PrivateZxingscannerview Mzxingscannerview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Mzxingscannerview=NewZxingscannerview ( This);    Zxingscannerview as the layout Setcontentview (Mzxingscannerview); } @Overrideprotected voidOnresume () {Super. Onresume (); Mzxingscannerview.setresulthandler ( This); Set the processing result callback Mzxingscannerview.startcamera (); Open Camera} @Overrideprotected voidOnPause () {Super. OnPause (); Mzxingscannerview.stopcamera (); Turn off the camera when the activity loses focus} @Override Public voidHandleresult (Result result) {//Implements callback interface, callbacks data and ends activity Intent=NewIntent (); Data.putextra ("Text", Result.gettext ());        Setresult (RESULT_OK, data);    Finish (); }}

3. Open this activity in the main activity and process the returned data:

 Public classHomeactivityextendsappcompatactivity {PrivateTextView Mtextview; PrivateWebView Mwebview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_home); Mtextview=(TextView) Findviewbyid (r.id.tv); Mwebview=(WebView) Findviewbyid (R.ID.WV); }     Public voidscancode (view view) {Startactivityforresult (NewIntent ( This, Scanneractivity.class), 1); } @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {        Super. Onactivityresult (Requestcode, ResultCode, data); if(ResultCode = =RESULT_OK) {Mtextview.settext (Data.getstringextra ("Text")); Displays the 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.

Android Development Learning Path-two-dimensional code learning

Related Article

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.