In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.