Using Android to generate two-dimensional code

Source: Internet
Author: User

Although there are many tools to generate two-dimensional code on the Internet, it is good to generate a two-dimensional code that belongs to your own code, the implementation method is very simple, you need to import a third-party class library

 Step One: Import third-party class library files: Import third-party class library files into Android studio

  Step Two: the XML file layout, a edittext for the input of two-dimensional code content, a ImageView used to display the generated QR code, a generate button

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical">    <EditTextAndroid:id= "@+id/ed"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"        />    <ButtonAndroid:id= "@+id/btn2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Generate two-dimensional code"        />    <ImageViewAndroid:id= "@+id/im"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /></LinearLayout>

Step Three: call the class and method in the third-party class library file in the Mainactivity.java file to generate the QR code

1 Package com.contentprovide.liuliu.aaa;2 3 import Android. Manifest;4 import android.content.Intent;5 import Android.content.pm.PackageManager;6 import Android.graphics.Bitmap;7 import Android.support.v4.app.ActivityCompat;8 import Android.support.v4.content.ContextCompat;9 import android.support.v7.app.AppCompatActivity;Ten import Android.os.Bundle; One import Android.view.View; A import Android.widget.Button; - import Android.widget.EditText; - import Android.widget.ImageView; the import Android.widget.TextView; -  - import com.xys.libzxing.zxing.activity.CaptureActivity; - import Com.xys.libzxing.zxing.decode.DecodeFormatManager; + import Com.xys.libzxing.zxing.decode.DecodeHandler; - import com.xys.libzxing.zxing.encoding.EncodingUtils; + import Com.xys.libzxing.zxing.utils.CaptureActivityHandler; A  at Public class Mainactivity extends Appcompatactivity { - Button btn2; -  - EditText Ed; - ImageView im; -  in @Override - protected void OnCreate (Bundle savedinstancestate) { to super.oncreate (savedinstancestate); + Setcontentview (r.layout.activity_main); -  the ed = (EditText) Findviewbyid (R.id.ed); * im = (ImageView) Findviewbyid (r.id.im); $ btn2 = (Button) Findviewbyid (R.ID.BTN2);Panax Notoginseng  -  the Btn2.setonclicklistener (New View.onclicklistener () { + @Override A Public void OnClick (view view) { the String s = Ed.gettext (). toString (); + try {//catch exception -  Bitmap Bitmap = Encodingutils.createqrcode (s, n, +, NULL);  im.setimagebitmap (bitmap);  $ } catch (Exception e) { -  -                 } the  -             }Wuyi         }); the  -  Wu     } -  About  $}

Step is very simple, there are a few steps to achieve the basic realization of the two-dimensional code generation.

Using an Android scan to parse a two-dimensional code

Using Android to generate a QR code

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.