QR code scanning and generation-third-party Open Source-ZXing, third-party Open Source-zxing

Source: Internet
Author: User

QR code scanning and generation-third-party Open Source-ZXing, third-party Open Source-zxing

Extraction of ZXing QR code Function lib: https://github.com/xuyisheng/ZXingLib

1. Scan the QR code:

This CaptureActivity class is used for scanning. We directly download libzxing from the above class as the Module, for example:

 

First, add the permission:

<! -- CAMERA --> <uses-permission android: name = "android. permission. CAMERA"/> <! -- Vibration --> <uses-permission android: name = "android. permission. VIBRATE"/>

 

Since we use it as a Module, we can also use it directly. Here we can directly use the AndroidManifest about the CaptureActivity class in the dependency. copy the xml Registration Information and place it in our project:

<activity            android:name="com.xys.libzxing.zxing.activity.CaptureActivity"            android:configChanges="orientation|keyboardHidden"            android:screenOrientation="portrait"            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"            android:windowSoftInputMode="stateAlwaysHidden">        </activity>

We declare a Button in activity_main.xml:

<Button android: id = "@ + id/btnSan" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "Scan QR code"/>

In JAVA code, add click events after initialization:

 findViewById(R.id.btnSan).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class), 0);            }        });

To view the returned result, add a TextView in activity_main.xml to view the result:

<TextView        android:id="@+id/tv_content"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />

After initialization, add the returned code to the JAVA code:

 @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        // TODO Auto-generated method stub        super.onActivityResult(requestCode, resultCode, data);        if (resultCode == RESULT_OK) {            String result = data.getExtras().getString("result");            tv_content.setText(result);        }    }

 

In this way, we can see the returned items. The following is the result of the example:

 

 

2. Generate a QR code:

To generate a QR code, we need three elements: the content to be generated, the generated button, and the storage of the generated content. Therefore, we need to add such elements in layou_main.xml.

<EditText android: id = "@ + id/et_input" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_below = "@ + id/TV _content" android: layout_marginTop = "10dp" android: hint = "Enter the text of the QR code to be generated"/> <Button android: id = "@ + id/btn_generate" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_below = "@ + id/et_input" android: layout_marginTop = "10dp" android: text = "generate QR code"/> <ImageView android: id = "@ + id/img" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ + id/btn_generate" android: layout_centerHorizontal = "true" android: layout_marginTop = "10dp"/>

 

Initialize these controls and write them in the Button click event:

FindViewById (R. id. btn_generate ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {String str = et_input.getText (). toString (); if (str. equals ("") {Toast. makeText (MainActivity. this, "cannot be blank", Toast. LENGTH_SHORT ). show ();} else {// bitmap try {/*** parameter: 1. text 2 3. the width and height of the QR code. the logo in the middle of the QR code */Bitmap bitmap = EncodingUtils. createQRCode (str, 500,500, null); // sets the image img. setImageBitmap (bitmap);} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace ();}}}});

 

Run the following code to generate the QR code:

Of course, there is no logo. If you need to add a logo, you only need

 Bitmap bitmap = EncodingUtils.createQRCode(str, 500, 500, null);

You can change the null value to the logo you need.

 

The complete code is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout 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/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = "com. zzw. testerweima. mainActivity "> <Button android: id =" @ + id/btnSan "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: text = "Scan QR code"/> <TextView android: id = "@ + id/TV _content" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ + id/btnSan" android: layout_centerHorizontal = "true" android: layout_marginTop = "10dp"/> <EditText android: id = "@ + id/et_input" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_below = "@ + id/TV _content" android: layout_marginTop = "10dp" android: hint = "Enter the text of the QR code to be generated"/> <Button android: id = "@ + id/btn_generate" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_below = "@ + id/et_input" android: layout_marginTop = "10dp" android: text = "generate QR code"/> <ImageView android: id = "@ + id/img" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ + id/btn_generate" android: layout_centerHorizontal = "true" android: layout_marginTop = "10dp"/> </RelativeLayout>

 

Package com. zzw. testerweima; import android. content. intent; import android. graphics. bitmap; import android. support. v7.app. appCompatActivity; import android. OS. bundle; import android. util. log; import android. view. view; 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 class MainActivity extends AppCompatActivity {private TextView TV _content; private EditText et_input; private ImageView img; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); TV _content = (TextView) findViewById (R. id. TV _content); et_input = (EditText) findViewById (R. id. et_input); img = (ImageView) findViewById (R. id. img); findViewById (R. id. btnSan ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {startActivityForResult (new Intent (MainActivity. this, CaptureActivity. class), 0) ;}}); findViewById (R. id. btn_generate ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {String str = et_input.getText (). toString (); if (str. equals ("") {Toast. makeText (MainActivity. this, "cannot be blank", Toast. LENGTH_SHORT ). show ();} else {// bitmap try {/*** parameter: 1. text 2 3. the width and height of the QR code. the logo in the middle of the QR code */Bitmap bitmap = EncodingUtils. createQRCode (str, 500,500, null); // sets the image img. setImageBitmap (bitmap);} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace () ;}}}) ;}@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stub super. onActivityResult (requestCode, resultCode, data); if (resultCode = RESULT_ OK) {String result = data. getExtras (). getString ("result"); Log. d ("Main", result); TV _content.setText (result );}}}

 

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.