QR code in the application of more and more, so that is to find the relevant information to write a two-dimensional code scanning and generation of two-dimensional code of the demo.
This demo uses a third-party zxing library. GitHub's address:
Zxing
References in Androidstudio:
File---New---Import module imports zxing third-party libraries and then selects items by F4,
Click the plus sign to select File dependency to import the Zxing library.
Then we start writing the layout file.
Activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "android:orientation=" vertical "tools:context=" com.zwb.zxingdemo.MainActivity "> <button Android:id= "@+id/start" android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:text= "scan qr code"/> <textview android:layout_margintop= "20DP" android:textcolor= "#e90518" android:text= "Scan results:" android:textsize= "18sp" android:layout_width= "Match_parent" android:layout_he ight= "Wrap_content"/> <textview Android:id= "@+id/tv_show" android:layout_width= "match_parent" android:layout_height= "Wrap_content"/> <view android:background= "@color/colorprimary" android:layout_margintop= "10DP" Android:layout_w Idth= "Match_parent" android:layout_height= "5DP"/> <linearlayout android:layout_margintop= "20DP" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal "> <edittext android:id=" @+id/ed_text "android:layout_width=" 0DP "Android:la yout_weight= "9" android:layout_margintop= "10DP" android:layout_height= "wrap_content"/> &L T CheckBox android:id= "@+id/cb_logo" android:text= "logo" android:layout_width= "wrap_content "android:layout_height=" wrap_content "/> </LinearLayout> <button android:id=" @+id/mak E_ma "Android:text= "Generate two-dimensional code" android:layout_width= "Match_parent" android:layout_height= "wrap_content"/> <ImageView Android:id= "@+id/img" android:layout_margintop= "10DP" android:background= "@mipmap/ic_launcher" android:layout_gravity= "center" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"/& Gt;</linearlayout>
Mainactivity.java
Import Android.content.intent;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.view.view;import Android.widget.checkbox;import Android.widget.edittext;import Android.widget.imageview;import Android.widget.textview;import Android.widget.toast;import com.xys.libzxing.zxing.activity.CaptureActivity; Import Com.xys.libzxing.zxing.encoding.encodingutils;import Org.w3c.dom.text;public class MainActivity extends appcompatactivity {private TextView TextView; Private ImageView ImageView; private EditText input; Private CheckBox logo; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView = ((TextView) Findviewbyid (r.id.tv_show)); ImageView = ((ImageView) Findviewbyid (r.id.img)); Logo = (CheckBox) Findviewbyid (R.id.cb_logo)); Findviewbyid(R.id.start). Setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { Startactivityforresult (New Intent (Mainactivity.this, Captureactivity.class), 0); } }); Findviewbyid (R.id.make_ma). Setonclicklistener (New View.onclicklistener () {public void OnClick (View v) { Input = ((EditText) Findviewbyid (R.id.ed_text)); String text = Input.gettext (). toString (); if (Text.equals (")") {Toast.maketext (mainactivity.this, "Input cannot be empty", Toast.length_short). Show (); } else {/** * Four parameters: Incoming text, width, height, logo */ Bitmap Bitmap = Encodingutils.createqrcode (text, $, $, log O.ischecked ()? Bitmapfactory.decoderesource (Getresources (), R.mipmap.ic_launcher): NULL); Imageview.setimagebitmap (bitmap); } } }); } @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityr Esult (Requestcode, ResultCode, data); if (ResultCode = = RESULT_OK) {Bundle bundle = Data.getextras (); String result = bundle.getstring ("result"); Textview.settext (result); } }}
Scan two-dimensional codes and generate QR codes using zxing