前言
简单实现一个二维码扫描回调的activity,能够直接复制到项目中进行使用,使用Zxing库进行实现(横着扫描真是太难受辣,加个库就好看多了,嘿嘿)。文章参考:Android使用ZXing实现二维码的扫描和创建
一、扫描效果
二、使用步骤
1.引入库
代码如下:
implementation 'com.github.bingoogolapple.BGAQRCode-Android:zxing:1.3.8'
implementation 'com.google.zxing:core:3.4.1'
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
2.实现扫描activity
首先建立一个空的activity命名为ScanActivity ,替换其布局文件为如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".ScanActivity">
<cn.bingoogolapple.qrcode.zxing.ZXingView
android:id="@+id/zxing_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
替换其activity代码如下:
package "你自己的路径及R文件路径";
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;
public class ScanActivity extends AppCompatActivity implements QRCodeView.Delegate {
private ZXingView mZxingView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
mZxingView = findViewById(R.id.zxing_view);
mZxingView.setDelegate(this);
}
@Override
protected void onStart() {
super.onStart();
mZxingView.startCamera();// 打开后置摄像头开始预览,但是并未开始识别
//mZxingView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT);// 打开前置摄像头开始预览,但是并未开始识别
//mZxingView.startSpot();// 开始识别
mZxingView.startSpotAndShowRect();// 显示扫描框,并开始识别
}
@Override
protected void onStop() {
super.onStop();
mZxingView.stopCamera();// 关闭摄像头预览,并隐藏扫描框
}
@Override
public void onScanQRCodeSuccess(String result) {
Toast.makeText(getBaseContext(),"扫描结果为:"+result,Toast.LENGTH_SHORT).show();
Intent resultIntent = new Intent();
resultIntent.putExtra("SCAN_RESULT",result );
setResult(Activity.RESULT_OK, resultIntent);
mZxingView.stopSpot();// 停止识别
finish();
}
@Override
public void onCameraAmbientBrightnessChanged(boolean isDark) {
Log.d("IS_DARK",String.valueOf(isDark));
}
@Override
public void onScanQRCodeOpenCameraError() {
Toast.makeText(getBaseContext(),"ERROR",Toast.LENGTH_SHORT).show();
mZxingView.stopSpot();// 停止识别
finish();
}
}
3.调用扫描activity
启动扫描
ActivityResultLauncher<Intent> launcher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
this::handleActivityResult
);
Intent intent = new Intent(requireActivity(), ScanActivity.class);
launcher.launch(intent);
回调处理
private void handleActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
String scanResult = data.getStringExtra("SCAN_RESULT");
LogUtils.d("get QR data is "+scanResult);
// 根据data来处理结果
// 例如,你可以从data中获取数据并更新UI
} else {
// 处理取消或错误的情况
}
}
三、备注
mZxingView.startCamera();// 打开后置摄像头开始预览,但是并未开始识别
mZxingView.stopCamera();// 关闭摄像头预览,并隐藏扫描框
mZxingView.startSpot();// 开始识别
mZxingView.stopSpot();// 停止识别
mZxingView.startSpotAndShowRect();// 显示扫描框,并开始识别
mZxingView.stopSpotAndHiddenRect();// 停止识别,并隐藏扫描框
mZxingView.showScanRect();// 显示扫描框
mZxingView.hiddenScanRect();// 隐藏扫描框
mZxingView.getScanBoxView().setOnlyDecodeScanBoxArea(true);// 仅识别扫描框中码