当前位置:主页 > 移动开发 > Android代码 >

Android studio 实现手机扫描二维码功能

时间:2021-08-18 08:16:41 | 栏目:Android代码 | 点击:

 安卓手机版本在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);
        }
      }
    }
  }
}

总结

您可能感兴趣的文章:

相关文章