Android studio 实现手机扫描二维码功能
程序员文章站
2022-06-29 18:26:23
安卓手机版本在6.0以后需要动态获取相机权限
1.获取相机权限
安卓手机版本在6.0以后需要动态获取相机权限
1.获取相机权限
<!-- 获取手机相机的权限 --> <uses-permission android:name="android.permission.camera" /> <uses-permission android:name="android.permission.flashlight" />
2.添加依赖
implementation 'cn.yipianfengye.android:zxing-library:2.2'
3.activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_gravity="center" android:layout_height="wrap_content"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启相机扫描" android:id="@+id/btn" /> </linearlayout>
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); //隐藏系统默认的标题 actionbar actionbar = getsupportactionbar(); if(actionbar!=null){ actionbar.hide(); } //初始化相机权限 zxinglibrary.initdisplayopinion(this); btn = findviewbyid(r.id.btn); btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //先判断手机版本是否在6.0以上,如果在6.0以上则需要动态申请权限 if (build.version.sdk_int > 22) { if (contextcompat.checkselfpermission(mainactivity.this, android.manifest.permission.camera) != packagemanager.permission_granted) { //先判断有没有权限 ,没有就在这里进行权限的申请 activitycompat.requestpermissions(mainactivity.this, new string[]{android.manifest.permission.camera}, 1); } else { //说明已经获取到摄像头权限了 想干嘛干嘛 intent intent = new intent(mainactivity.this, captureactivity.class); startactivityforresult(intent, 1); } } else { //这个说明系统版本在6.0之下,不需要动态获取权限。 intent intent = new intent(mainactivity.this, captureactivity.class); startactivityforresult(intent, 1); } } }); } //获取手机相机权限 @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, "请打开相机权限", toast.length_short).show(); } } } //处理扫描结果 @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) { //获取到扫描的结果 string result = bundle.getstring(codeutils.result_string); log.d("res:",result); } } } } }
总结
以上所述是小编给大家介绍的android studio 实现手机扫描二维码功能,希望对大家有所帮助
上一篇: 桑树叶的禁忌,大家了解桑树叶吗
下一篇: 幽默透顶的雷人男女