参考:Android进阶 - 二维码扫描 - (jianshu.com)
zing-android地址:
https://github.com/journeyapps/zxing-android-embedded
注:本文相较于参考使用的版本是4.3.0版本,所以步骤二的部分文件相较于原文进行了修改。
步骤一
导入依赖
dependencies {
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
}
步骤二
引入文件
package com.example.xy.index;
import android.os.Bundle;
import android.view.KeyEvent;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.example.xy.R;
import com.example.xy.utils.StatusBarUtils;
import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
/**
* @Class: CustomCaptureActivity
* @Description: 自定义条形码/二维码扫描
* @Author: wangnan7
* @Date: 2017/5/19
*/
public class CustomCaptureActivity extends AppCompatActivity {
/**
* 条形码扫描管理器
*/
private CaptureManager mCaptureManager;
/**
* 条形码扫描视图
*/
private DecoratedBarcodeView mBarcodeView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StatusBarUtils.transparencyBar(this);
setContentView(R.layout.activity_zxing_layout);
mBarcodeView = (DecoratedBarcodeView)findViewById(R.id.zxing_barcode_scanner);
mCaptureManager = new CaptureManager(this, mBarcodeView);
mCaptureManager.initializeFromIntent(getIntent(), savedInstanceState);
mCaptureManager.decode();
}
@Override
protected void onResume() {
super.onResume();
mCaptureManager.onResume();
}
@Override
protected void onPause() {
super.onPause();
mCaptureManager.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mCaptureManager.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mCaptureManager.onSaveInstanceState(outState);
}
/**
* 权限处理
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
mCaptureManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
/**
* 按键处理
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return mBarcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
}
package com.example.xy.index;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
public class CustomRecyclerView extends RecyclerView {
private int mTouchSlop;
public CustomRecyclerView(Context context) {
super(context);
final ViewConfiguration vc = ViewConfiguration.get(context);
mTouchSlop = vc.getScaledTouchSlop();
}
public CustomRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
final ViewConfiguration vc = ViewConfiguration.get(context);
mTouchSlop = vc.getScaledTouchSlop();
}
int move_x,move_y;
@Override
public boolean dispatchTouchEvent(MotionEvent e) {
switch (e.getAction()){
case MotionEvent.ACTION_DOWN:
move_x = (int) e.getX();
move_y = (int) e.getY();
getParent().requestDisallowInterceptTouchEvent(true);
Log.e("motion_event","down x==y "+move_x+" ==== "+move_y);
break;
case MotionEvent.ACTION_MOVE:
Log.e("motion_event","move x==y "+move_x+" ==== "+move_y);
int y = (int) e.getY();
int x = (int) e.getX();
if(Math.abs(y-move_y)>mTouchSlop&&Math.abs(x-move_x)
package com.example.xy.index.wdiget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.TypedValue;
import com.google.zxing.ResultPoint;
import com.journeyapps.barcodescanner.Size;
import com.journeyapps.barcodescanner.ViewfinderView;
import java.util.ArrayList;
import java.util.List;
/**
* @Class: CustomViewfinderView
* @Description: 自定义扫描框样式
* @Author: wangnan7
* @Date: 2017/5/22
*/
public class CustomViewfinderView extends ViewfinderView {
/**
* 重绘时间间隔
*/
public static final long CUSTOME_ANIMATION_DELAY = 16;
/* ****************************************** 边角线相关属性 ************************************************/
/**
* "边角线长度/扫描边框长度"的占比 (比例越大,线越长)
*/
public float mLineRate = 0.1F;
/**
* 边角线厚度 (建议使用dp)
*/
public float mLineDepth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics());
/**
* 边角线颜色
*/
public int mLineColor = Color.WHITE;
/* ******************************************* 扫描线相关属性 ************************************************/
/**
* 扫描线起始位置
*/
public int mScanLinePosition = 0;
/**
* 扫描线厚度
*/
public float mScanLineDepth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics());
/**
* 扫描线每次重绘的移动距离
*/
public float mScanLineDy = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getResources().getDisplayMetrics());
/**
* 线性梯度
*/
public LinearGradient mLinearGradient;
/**
* 线性梯度位置
*/
public float[] mPositions = new float[]{0f, 0.5f, 1f};
/**
* 线性梯度各个位置对应的颜色值
*/
public int[] mScanLineColor = new int[]{0x00FFFFFF, Color.WHITE, 0x00FFFFFF};
public CustomViewfinderView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onDraw(Canvas canvas) {
refreshSizes();
if (framingRect == null || previewSize == null) {
return;
}
Rect frame = framingRect;
final Size previewSize = this.previewSize;
int width = canvas.getWidth();
int height = canvas.getHeight();
//绘制4个角
paint.setColor(mLineColor); // 定义画笔的颜色
canvas.drawRect(frame.left, frame.top, frame.left + frame.width() * mLineRate, frame.top + mLineDepth, paint);
canvas.drawRect(frame.left, frame.top, frame.left + mLineDepth, frame.top + frame.height() * mLineRate, paint);
canvas.drawRect(frame.right - frame.width() * mLineRate, frame.top, frame.right, frame.top + mLineDepth, paint);
canvas.drawRect(frame.right - mLineDepth, frame.top, frame.right, frame.top + frame.height() * mLineRate, paint);
canvas.drawRect(frame.left, frame.bottom - mLineDepth, frame.left + frame.width() * mLineRate, frame.bottom, paint);
canvas.drawRect(frame.left, frame.bottom - frame.height() * mLineRate, frame.left + mLineDepth, frame.bottom, paint);
canvas.drawRect(frame.right - frame.width() * mLineRate, frame.bottom - mLineDepth, frame.right, frame.bottom, paint);
canvas.drawRect(frame.right - mLineDepth, frame.bottom - frame.height() * mLineRate, frame.right, frame.bottom, paint);
// Draw the exterior (i.e. outside the framing rect) darkened
paint.setColor(resultBitmap != null ? resultColor : maskColor);
canvas.drawRect(0, 0, width, frame.top, paint);
canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint);
canvas.drawRect(0, frame.bottom + 1, width, height, paint);
if (resultBitmap != null) {
// Draw the opaque result bitmap over the scanning rectangle
paint.setAlpha(CURRENT_POINT_OPACITY);
canvas.drawBitmap(resultBitmap, null, frame, paint);
} else {
// 绘制扫描线
mScanLinePosition += mScanLineDy;
if(mScanLinePosition > frame.height()){
mScanLinePosition = 0;
}
mLinearGradient = new LinearGradient(frame.left, frame.top + mScanLinePosition, frame.right, frame.top + mScanLinePosition, mScanLineColor, mPositions, Shader.TileMode.CLAMP);
paint.setShader(mLinearGradient);
canvas.drawRect(frame.left, frame.top + mScanLinePosition, frame.right, frame.top + mScanLinePosition + mScanLineDepth, paint);
paint.setShader(null);
float scaleX = frame.width() / (float) previewSize.width;
float scaleY = frame.height() / (float) previewSize.height;
List currentPossible = possibleResultPoints;
List currentLast = lastPossibleResultPoints;
int frameLeft = frame.left;
int frameTop = frame.top;
if (currentPossible.isEmpty()) {
lastPossibleResultPoints = null;
} else {
possibleResultPoints = new ArrayList<>(5);
lastPossibleResultPoints = currentPossible;
paint.setAlpha(CURRENT_POINT_OPACITY);
paint.setColor(resultPointColor);
for (ResultPoint point : currentPossible) {
canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX),
frameTop + (int) (point.getY() * scaleY),
POINT_SIZE, paint);
}
}
if (currentLast != null) {
paint.setAlpha(CURRENT_POINT_OPACITY / 2);
paint.setColor(resultPointColor);
float radius = POINT_SIZE / 2.0f;
for (ResultPoint point : currentLast) {
canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX),
frameTop + (int) (point.getY() * scaleY),
radius, paint);
}
}
}
// Request another update at the animation interval, but only repaint the laser line,
// not the entire viewfinder mask.
postInvalidateDelayed(CUSTOME_ANIMATION_DELAY,
frame.left,
frame.top,
frame.right,
frame.bottom);
}
}
步骤三
点击事件出发扫描
//二维码/条形码
ivQr.setOnClickListener {
// 创建IntentIntegrator对象
val intentIntegrator = IntentIntegrator(me)
intentIntegrator.setTimeout(1000 * 30)
intentIntegrator.setCaptureActivity(CustomCaptureActivity::class.java)
// 开始扫描
intentIntegrator.initiateScan()
}
将扫码出的内容进行处理
在activity中重写onActivityResult方法,只看else中的代码,第一个是调用拍照,拍完之后的回调。
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when(requestCode){
TAKE_PHOTO -> {
if (resultCode === RESULT_OK) {
try {
//压缩然后调用接口上传图片到服务器并调用腾讯解析接口。
var picFile = File(filePath)
uploadPic(picFile)
} catch (e: FileNotFoundException) {
e.printStackTrace()
}
}
}
else -> {
// 获取解析结果
val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
if (result != null) {
if (result.contents == null) {
ToastUtils.showShort("取消扫描")
} else {
ToastUtils.showShort("扫描内容:" + result.contents)
}
} else {
super.onActivityResult(requestCode, resultCode, data)
}
}
}
}