Android:简单粗暴的二维码生成与扫描
- 之前写项目的时候有需求要生成和扫描二维码,我想弄的简单点,在网上找了好久终于在一个大佬的博客里找到了我所期待的简单粗暴,在这里记录一下。
1.添加依赖,在app目录下的build.gradle文件中添加:
implementation 'cn.yipianfengye.android:zxing-library:2.2'
2.申请相机和振动的权限,在AndroidManifest.xml文件中添加:
<uses-permission android:name="android.permission.CAMERA" /> <!-- 权限:调用相机 --> <uses-permission android:name="android.permission.VIBRATE" /><!-- 权限:振动 -->
3.在Activity或Fragment中执行初始化操作:
ZXingLibrary.initDisplayOpinion(this); //初始化ZXingLibrary
4.在逻辑代码中向用户申请调用相机的权限 (android 6.0以上需要动态申请相机的调用权限):
//获取相机拍摄的权限 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//版本>=6.0 ? if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { //向用户申请调用相机的权限 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1); } }
5.生成二维码
String codeStr = “hello world !”; Bitmap bitmap = CodeUtils.createImage(codeStr, 250, 250, null); //设置二维码的内容、宽、高、是否带有logo
6.启动CaptureActivity扫描二维码(二维码的扫描界面用户可以自行设计,这里使用默认界面扫描二维码)。注意要把QRCodeActivity换成自己的Context。
//启动CaptureActivity扫描二维码 startActivityForResult(new Intent(QRCodeActivity.this, CaptureActivity.class), CAPTURE_REQUEST_CODE);
7.在onActivityResult()方法中获取扫描二维码得到的信息
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { //用户没有进行有效的设置操作,返回 if (resultCode == RESULT_CANCELED) { Toast.makeText(getApplication(), "取消", Toast.LENGTH_SHORT).show(); return; } switch (requestCode) { case CAPTURE_REQUEST_CODE: { if (data != null) { Bundle bundle = data.getExtras(); if (bundle == null) { Log.d(TAG, "onActivityResult: bundle==null"); return; } if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) { String result = bundle.getString(CodeUtils.RESULT_STRING); Log.d(TAG, "onActivityResult: 解析结果:"+result); } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) { Log.d(TAG, "onActivityResult: 解析失败!"); } } else { Log.d(TAG, "onActivityResult: data==null"); } break; } }
- 附上我的部分代码
QRCodeActivity.java
1 package com.example.test3project; 2 3 import androidx.annotation.RequiresApi; 4 import androidx.appcompat.app.AppCompatActivity; 5 import androidx.core.app.ActivityCompat; 6 7 import android.Manifest; 8 import android.content.Intent; 9 import android.content.pm.PackageManager; 10 import android.graphics.Bitmap; 11 import android.os.Build; 12 import android.os.Bundle; 13 import android.util.Log; 14 import android.view.View; 15 import android.widget.Button; 16 import android.widget.EditText; 17 import android.widget.ImageView; 18 import android.widget.TextView; 19 import android.widget.Toast; 20 21 import com.uuzuche.lib_zxing.activity.CaptureActivity; 22 import com.uuzuche.lib_zxing.activity.CodeUtils; 23 import com.uuzuche.lib_zxing.activity.ZXingLibrary; 24 25 26 public class QRCodeActivity extends AppCompatActivity { 27 28 private ImageView imageQRCode; 29 private EditText editCreateQRCode; 30 private Button buttonCreateQRCode; 31 private Button buttonScanQRCode; 32 private TextView textQRCodeResult; 33 private static final String TAG = "QRCodeActivity"; 34 private static final int CAPTURE_REQUEST_CODE = 789; 35 36 @RequiresApi(api = Build.VERSION_CODES.M) 37 @Override 38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 setContentView(R.layout.activity_qrcode); 41 42 ZXingLibrary.initDisplayOpinion(this); //初始化ZXingLibrary 43 44 //获取相机拍摄的权限 45 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//版本>=6.0 ? 46 if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 47 //向用户申请调用相机的权限 48 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1); 49 } 50 } 51 52 imageQRCode = findViewById(R.id.imageQRCode); 53 editCreateQRCode = findViewById(R.id.editCreateQRCode); 54 buttonCreateQRCode = findViewById(R.id.buttonCreateQRCode); 55 buttonScanQRCode = findViewById(R.id.buttonScanQRCode); 56 textQRCodeResult = findViewById(R.id.textQRCodeResult); 57 58 buttonCreateQRCode.setOnClickListener(new View.OnClickListener() { 59 @Override 60 public void onClick(View v) { 61 String codeStr = editCreateQRCode.getText().toString(); 62 Bitmap bitmap = CodeUtils.createImage(codeStr, 250, 250, null); //设置二维码的内容、宽、高、是否带有logo 63 imageQRCode.setImageBitmap(bitmap); 64 } 65 }); 66 67 buttonScanQRCode.setOnClickListener(new View.OnClickListener() { 68 @Override 69 public void onClick(View v) { 70 //启动CaptureActivity扫描二维码 71 startActivityForResult(new Intent(QRCodeActivity.this, CaptureActivity.class), CAPTURE_REQUEST_CODE); 72 } 73 }); 74 75 } 76 77 @Override 78 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 79 80 //用户没有进行有效的设置操作,返回 81 if (resultCode == RESULT_CANCELED) { 82 Toast.makeText(getApplication(), "取消", Toast.LENGTH_SHORT).show(); 83 return; 84 } 85 86 switch (requestCode) { 87 case CAPTURE_REQUEST_CODE: { 88 89 if (data != null) { 90 Bundle bundle = data.getExtras(); 91 if (bundle == null) { 92 Log.d(TAG, "onActivityResult: bundle==null"); 93 return; 94 } 95 if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) { 96 String result = bundle.getString(CodeUtils.RESULT_STRING); 97 textQRCodeResult.setText("解析结果:" + result); 98 Log.d(TAG, "onActivityResult: 解析结果:"+result); 99 } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) { 100 textQRCodeResult.setText("解析二维码失败!"); 101 Log.d(TAG, "onActivityResult: 解析失败!"); 102 } 103 } else { 104 Log.d(TAG, "onActivityResult: data==null"); 105 } 106 107 break; 108 } 109 } 110 111 super.onActivityResult(requestCode, resultCode, data); 112 } 113 114 }
activity_qrcode.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 tools:context=".QRCodeActivity"> 9 10 <ImageView 11 android:id="@+id/imageQRCode" 12 android:layout_margin="10dp" 13 android:layout_width="250dp" 14 android:layout_height="250dp" 15 android:layout_gravity="center_horizontal"/> 16 17 <EditText 18 android:hint="输入文字" 19 android:gravity="center" 20 android:id="@+id/editCreateQRCode" 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content" /> 23 24 <Button 25 android:id="@+id/buttonCreateQRCode" 26 android:text="生成二维码" 27 android:layout_gravity="center_horizontal" 28 android:layout_margin="5dp" 29 android:layout_width="240dp" 30 android:layout_height="wrap_content" /> 31 32 <Button 33 android:id="@+id/buttonScanQRCode" 34 android:text="扫描二维码" 35 android:layout_gravity="center_horizontal" 36 android:layout_margin="5dp" 37 android:layout_width="240dp" 38 android:layout_height="wrap_content" /> 39 40 <TextView 41 android:layout_margin="10dp" 42 android:hint="扫描结果" 43 android:gravity="center" 44 android:textSize="20sp" 45 android:layout_width="match_parent" 46 android:layout_height="wrap_content" 47 android:id="@+id/textQRCodeResult" 48 /> 49 50 </LinearLayout>
CSDN地址:https://blog.csdn.net/qq_43529443/article/details/105480329:
文章的最后,附上那位大佬原文的地址以及github上这个项目的地址:
大佬原文地址
github地址