Two-dimensional code generation and scanning

Source: Internet
Author: User

Download class library Https://yunpan.cn/cBAZaMg5t2BXH access password 5a55

1. Add the settings.gradle in the outer layer of the project

Include ': App ', ': libzxing '


2.libzxing Build.gradle in the same version as your app
Compilesdkversion 23
Buildtoolsversion "23.0.3"

Defaultconfig {
Minsdkversion 15
Targetsdkversion 23


3. In Build.gradle's
Dependencies add
Compile project (': Libzxing ')



In the layout
<?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:orientation="Vertical"Tools:context=". Mainactivity"> <Button android:layout_width="match_parent"Android:layout_height="wrap_content"Android:text="Scan Two-dimensional code"Android:id="@+id/btn_scan"Android:layout_alignparentleft="true"Android:layout_alignparentstart="true"Android:onclick="Scan"/> <TextView android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="---Display the scan results----"android:textsize="22DP"Android:id="@+id/tv_scanresult"/> <EditText android:layout_width="match_parent"Android:layout_height="wrap_content"Android:id="@+id/et_text"Android:hint="here Please enter the content you want to generate QR code"/> <CheckBox android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="whether the QR code should contain the image logo"Android:id="@+id/cb_includelogo"android:checked="false"/> <Button android:layout_width="fill_parent"Android:layout_height="wrap_content"Android:text="generate two-dimensional code QRCode"Android:id="@+id/btn_createqrcode"Android:onclick="Createqrode"/> <ImageView android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:id="@+id/imgv_qrcode"android:layout_gravity="Center_horizontal"Android:background="@mipmap/appicon"/></linearlayout>

In activity

Package Tests.bwie.com.mygridview;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.button;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; Public classZxingactivity extends Appcompatactivity {PrivateButton Btn_scan; PrivateTextView Tv_scanresult; PrivateEditText Et_text; PrivateCheckBox Cb_include; PrivateButton Btn_createsrcode; PrivateImageView Iv_qrcode; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_zxing); //Find ControlsFindcomp (); }    Private voidFindcomp () {Btn_scan=(Button) Findviewbyid (R.id.btn_scan); Tv_scanresult=(TextView) Findviewbyid (R.id.tv_scanresult); Et_text=(EditText) Findviewbyid (R.id.et_text); Cb_include=(CheckBox) Findviewbyid (R.id.cb_includelogo); Btn_createsrcode=(Button) Findviewbyid (R.id.btn_createqrcode); Iv_qrcode=(ImageView) Findviewbyid (R.id.imgv_qrcode); }    //direct call to third-party Zxing library API for scanning     Public voidScan (view view) {Startactivityforresult (NewIntent (zxingactivity. This, captureactivity.class),0); } @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data)        {Super.onactivityresult (Requestcode, ResultCode, data); if(resultcode==RESULT_OK) {Bundle Bundle=data.getextras ();//retrieve the scanned result data through the bundleString sresult=bundle.getstring ("result");            Tv_scanresult.settext (Sresult); System. out. println ("viewing read values"+Sresult); }    }     Public voidCreateqrode (view view) {String SInput=Et_text.gettext (). toString (); if(Sinput.equals ("") {toast.maketext (zxingactivity). This,"You have not entered the QR code to generate, please enter the content to generate the QR code", Toast.length_short). Show (); }Else{            if(cb_include.ischecked () = =false){                //call the Createqrcode of the Zxing class library to generate a QR code----does not contain a pictureBitmap bitmap= Encodingutils.createqrcode (SInput, -, -,NULL);            Iv_qrcode.setimagebitmap (bitmap); }Else{                //call the Createqrcode of the Zxing class library to generate a two-dimensional code----include picturesBitmap bitmap= Encodingutils.createqrcode (SInput, -, -, Bitmapfactory.decoderesource (Getresources (), R.mipmap.appicon));            Iv_qrcode.setimagebitmap (bitmap); }        }    }}

Two-dimensional code generation and scanning

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.