Files
ZXingLite/app/src/main/java/com/king/zxing/app/QRCodeActivity.java
2022-03-02 17:41:43 +08:00

92 lines
3.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.king.zxing.app;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.google.zxing.Result;
import com.king.zxing.CameraScan;
import com.king.zxing.CaptureActivity;
import com.king.zxing.DecodeConfig;
import com.king.zxing.DecodeFormatManager;
import com.king.zxing.analyze.MultiFormatAnalyzer;
import com.king.zxing.app.util.StatusBarUtils;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
/**
* @author <a href="mailto:jenly1314@gmail.com">Jenly</a>
*/
public class QRCodeActivity extends CaptureActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toolbar toolbar = findViewById(R.id.toolbar);
StatusBarUtils.immersiveStatusBar(this,toolbar,0.2f);
TextView tvTitle = findViewById(R.id.tvTitle);
tvTitle.setText(getIntent().getStringExtra(MainActivity.KEY_TITLE));
}
@Override
public int getLayoutId() {
return R.layout.qr_code_activity;
}
@Override
public void initCameraScan() {
super.initCameraScan();
//初始化解码配置
DecodeConfig decodeConfig = new DecodeConfig();
decodeConfig.setHints(DecodeFormatManager.QR_CODE_HINTS)//如果只有识别二维码的需求这样设置效率会更高不设置默认为DecodeFormatManager.DEFAULT_HINTS
.setFullAreaScan(false)//设置是否全区域识别默认false
.setAreaRectRatio(0.8f)//设置识别区域比例默认0.8,设置的比例最终会在预览区域裁剪基于此比例的一个矩形进行扫码识别
.setAreaRectVerticalOffset(0)//设置识别区域垂直方向偏移量默认为0为0表示居中可以为负数
.setAreaRectHorizontalOffset(0);//设置识别区域水平方向偏移量默认为0为0表示居中可以为负数
//在启动预览之前,设置分析器,只识别二维码
getCameraScan()
.setVibrate(true)//设置是否震动默认为false
.setNeedAutoZoom(true)//二维码太小时可自动缩放默认为false
.setAnalyzer(new MultiFormatAnalyzer(decodeConfig));//设置分析器,如果内置实现的一些分析器不满足您的需求,你也可以自定义去实现
}
/**
* 扫码结果回调
* @param result
* @return 返回false表示不拦截将关闭扫码界面并将结果返回给调用界面
* 返回true表示拦截需自己处理逻辑。当isAnalyze为true时默认会继续分析图像也就是连扫
* 如果只是想拦截扫码结果回调,并不想继续分析图像(不想连扫),请在拦截扫码逻辑处通过调
* 用{@link CameraScan#setAnalyzeImage(boolean)}
* 因为{@link CameraScan#setAnalyzeImage(boolean)}方法能动态控制是否继续分析图像。
*
*/
@Override
public boolean onScanResultCallback(Result result) {
/*
* 因为setAnalyzeImage方法能动态控制是否继续分析图像。
*
* 1. 因为分析图像默认为true如果想支持连扫返回true即可。
* 当连扫的处理逻辑比较复杂时请在处理逻辑前调用getCameraScan().setAnalyzeImage(false)
* 来停止分析图像等逻辑处理完后再调用getCameraScan().setAnalyzeImage(true)来继续分析图像。
*
* 2. 如果只是想拦截扫码结果回调自己处理逻辑,但并不想继续分析图像(即不想连扫),可通过
* 调用getCameraScan().setAnalyzeImage(false)来停止分析图像。
*/
return super.onScanResultCallback(result);
}
public void onClick(View v){
switch (v.getId()){
case R.id.ivLeft:
finish();
break;
}
}
}