How to scan and generate QR Code in Android
Editor to share with you how to scan and generate QR codes in Android, I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.
The details are as follows
Demand:
It is necessary to store the data in the QR code, and then scan the QR code to get the data and display it on the page.
Code implementation:
1. Quote a third party
/ / AndroidX version of implementation 'com.king.zxing:zxing-lite:1.1.7-androidx'
two。 Declare CaptureActivity in AndroidManifest.xml
3. Main code
Import android.content.Intent;import android.graphics.Bitmap;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.Toast;import androidx.annotation.Nullable;import androidx.appcompat.app.AppCompatActivity;import com.king.zxing.CaptureActivity;import com.king.zxing.util.CodeUtils;import org.json.JSONObject;import static com.king.zxing.CaptureFragment.KEY_RESULT Public class MainActivity extends AppCompatActivity implements View.OnClickListener {protected EditText edtResutlt; protected Button btnScan; protected Button btnCreate; protected EditText edtData; protected ImageView ivQr; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); super.setContentView (R.layout.activity_main); PermissionUtils.applyPermission (this); initView ();} @ Override public void onClick (View view) {if (view.getId () = = R.id.btn_scan) {/ / Jump to the scanning interface Intent intent = new Intent (this, CaptureActivity.class) StartActivityForResult (intent, 1);} else if (view.getId () = = R.id.btn_create) {String data = edtData.getText (). ToString (). Trim (); if (TextUtils.isEmpty (data)) {Toast.makeText (this, "Please enter text", Toast.LENGTH_SHORT). Show ();} else {/ / generate QR code Bitmap qrCode = CodeUtils.createQRCode (data, 600, null); ivQr.setImageBitmap (qrCode) } / * get scanned data in this method * @ param requestCode * @ param resultCode * @ param data * / @ Override protected void onActivityResult (int requestCode, int resultCode, @ Nullable Intent data) {super.onActivityResult (requestCode, resultCode, data); if (resultCode = = RESULT_OK) {if (requestCode = = 1) {String result = data.getStringExtra (KEY_RESULT); Log.e ("aaa", "resu-- >" + result) / display to the page edtResutlt.setText (result);} private void initView () {edtResutlt = (EditText) findViewById (R.id.edt_resutlt); btnScan = (Button) findViewById (R.id.btn_scan); btnScan.setOnClickListener (MainActivity.this); btnCreate = (Button) findViewById (R.id.btn_create); btnCreate.setOnClickListener (MainActivity.this); edtData = (EditText) findViewById (R.id.edt_data); ivQr = (ImageView) findViewById (R.id.iv_qr) }} above is all the contents of the article "how to scan and generate QR codes in Android". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!