Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the function of scanning QR Code on Mobile phone by Androidstudio

Shulou Source: shulou.com Published: 2022-06-03 09:38:36 10月04日 Update

This article is about how Androidstudio can scan QR codes on mobile phones. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Android phone version needs to obtain camera permission dynamically after 6.0.

1. Get camera permission

two。 Add dependency

Implementation 'cn.yipianfengye.android:zxing-library:2.2'

3.activity_main.xml

4.MainActivity.java

Package com.example.a86156.saomiao;import android.content.Intent;import android.content.pm.PackageManager;import android.os.Build;import android.support.annotation.NonNull;import android.support.v4.app.ActivityCompat;import android.support.v4.content.ContextCompat;import android.support.v7.app.ActionBar;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.Toast Import com.uuzuche.lib_zxing.activity.CaptureActivity;import com.uuzuche.lib_zxing.activity.CodeUtils;import com.uuzuche.lib_zxing.activity.ZXingLibrary;public class MainActivity extends AppCompatActivity {Button btn; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); / / hide the system default title ActionBar actionBar = getSupportActionBar (); if (actionBarrack permission null) {actionBar.hide ();} / initialize camera permissions ZXingLibrary.initDisplayOpinion (this) Btn = findViewById (R.id.btn) Btn.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {/ / first determines whether the mobile phone version is above 6.0.If it is above 6.0, you need to dynamically apply for permission if (Build.VERSION.SDK_INT > 22) {if (ContextCompat.checkSelfPermission (MainActivity.this, android.Manifest.permission.CAMERA)! = PackageManager.PERMISSION_GRANTED) {/ / determine whether you have permission or not. There is no application for ActivityCompat.requestPermissions (MainActivity.this, new String [] {android.Manifest.permission.CAMERA}, 1) for permission here. } else {/ / indicates that you have obtained the camera permission. What do you want to do? Intent intent = new Intent (MainActivity.this, CaptureActivity.class); startActivityForResult (intent, 1);}} else {/ / this indicates that the system version is under 6.0.There is no need to obtain permission dynamically. Intent intent = new Intent (MainActivity.this, CaptureActivity.class); startActivityForResult (intent, 1);} / / access to mobile camera @ Override public void onRequestPermissionsResult (int requestCode, @ NonNull String [] permissions, @ NonNull int [] grantResults) {super.onRequestPermissionsResult (requestCode, permissions, grantResults); if (requestCode = = 1) {if (grantResults [0] = PackageManager.PERMISSION_GRANTED) {Intent intent = new Intent (this, CaptureActivity.class); startActivityForResult (intent, 1) } else {Toast.makeText (MainActivity.this, "Please open camera permissions", Toast.LENGTH_SHORT). Show ();} / / process scan results @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {super.onActivityResult (requestCode, resultCode, data); if (resultCode = = RESULT_OK) {if (data! = null) {Bundle bundle = data.getExtras (); if (bundle = = null) {return } if (bundle.getInt (CodeUtils.RESULT_TYPE) = = CodeUtils.RESULT_SUCCESS) {/ / get the scan result String result = bundle.getString (CodeUtils.RESULT_STRING); Log.d ("res:", result);}}

Thank you for reading! This is the end of the article on "how to scan QR codes on Androidstudio". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Permissions mobile phone camera dynamic version function 2D QR code content more article system result good practical camera article title look knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Redmi MariaDB Huawei Shulou Tech Info