项目中需要使用到条形码的扫描识别,网上查阅资料发现,大部分人都使用的 Zxing
但是对这些条形码扫描的界面都不太满意,所以自定义一个条形码扫描界面
引入库
implementation 'com.google.zxing:core:3.3.3'
implementation('com.journeyapps:zxing-android-embedded:3.6.0') { transitive = false }
自定义扫描界面
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import com.google.zxing.ResultPoint;
import com.journeyapps.barcodescanner.ViewfinderView;
import java.util.ArrayList;
import java.util.List;
/**
* 自定义扫描界面
*/
public class QrView extends ViewfinderView {
public int laserLinePosition = 0;
public float[] position = new float[]{0f, 0.5f, 1f};
public int[] colors = new int[]{0x0027B14D, 0xff27B14D, 0x0027B14D};
public LinearGradient linearGradient;
private int ScreenRate;
public QrView(Context context, AttributeSet attrs) {
super(context, attrs);
float density = context.getResources().getDisplayMetrics().density;
ScreenRate = (int) (15 * density);
}
@Override
public void onDraw(Canvas canvas) {
int CORNER_WIDTH = 15;
refreshSizes();
if (framingRect == null || previewFramingRect == null) {
return;
}
Rect frame = framingRect;
Rect previewFrame = previewFramingRect;
int width = canvas.getWidth();
int height = canvas.getHeight();
//绘制4个角
paint.setColor(getResources().getColor(R.color.colorPrimary));//定义画笔的颜色
canvas.drawRect(frame.left, frame.top, frame.left + ScreenRate, frame.top + CORNER_WIDTH, paint);
canvas.drawRect(frame.left, frame.top, frame.left + CORNER_WIDTH, frame.top + ScreenRate, paint);
canvas.drawRect(frame.right - ScreenRate, frame.top, frame.right, frame.top + CORNER_WIDTH, paint);
canvas.drawRect(frame.right - CORNER_WIDTH, frame.top, frame.right, frame.top + ScreenRate, paint);
canvas.drawRect(frame.left, frame.bottom - CORNER_WIDTH, frame.left + ScreenRate, frame.bottom, paint);
canvas.drawRect(frame.left, frame.bottom - ScreenRate, frame.left + CORNER_WIDTH, frame.bottom, paint);
canvas.drawRect(frame.right - ScreenRate, frame.bottom - CORNER_WIDTH, frame.right, frame.bottom, paint);
canvas.drawRect(frame.right - CORNER_WIDTH, frame.bottom - ScreenRate, frame.right, frame.bottom, paint);
// 画出外部(即构图矩形之外)变暗
paint.setColor(resultBitmap != null ? resultColor : maskColor);
canvas.drawRect(0, 0, width, frame.top, paint);
canvas.drawRect(0, frame.top, frame.left, frame.bottom, paint);
canvas.drawRect(frame.right, frame.top, width, frame.bottom, paint);
canvas.drawRect(0, frame.bottom, 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 {
laserLinePosition = laserLinePosition + 8;
if (laserLinePosition >= frame.height()) {
laserLinePosition = 0;
}
linearGradient = new LinearGradient(frame.left + 1, frame.top + laserLinePosition, frame.right - 1, frame.top + 10 + laserLinePosition, colors, position, Shader.TileMode.CLAMP);
// Draw a red "laser scanner" line through the middle to show decoding is active
paint.setShader(linearGradient);
//绘制扫描线
canvas.drawRect(frame.left + 1, frame.top + laserLinePosition, frame.right - 1, frame.top + 10 + laserLinePosition, paint);
paint.setShader(null);
float scaleX = frame.width() / (float) previewFrame.width();
float scaleY = frame.height() / (float) previewFrame.height();
List<ResultPoint> currentPossible = possibleResultPoints;
List<ResultPoint> 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);
}
}
postInvalidateDelayed(16, frame.left, frame.top, frame.right, frame.bottom);
}
}
}
View_qr.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.journeyapps.barcodescanner.BarcodeView
android:id="@+id/zxing_barcode_surface"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:zxing_framing_rect_height="50dp"
app:zxing_framing_rect_width="250dp" />
<com.ludoven.zxing.QrView
android:id="@+id/zxing_viewfinder_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:zxing_possible_result_points="@color/colorPrimary"
app:zxing_result_view="@color/zxing_custom_result_view"
app:zxing_viewfinder_laser="@color/colorPrimary"
app:zxing_viewfinder_mask="@color/zxing_custom_viewfinder_mask" />
<TextView
android:id="@+id/zxing_status_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="30dp"
android:background="@color/zxing_transparent"
android:text="@string/zxing_msg_default_status"
android:textColor="@color/zxing_status_text" />
</merge>
ScanActivity
import android.os.Bundle
import android.os.PersistableBundle
import android.view.KeyEvent
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.journeyapps.barcodescanner.CaptureManager
import kotlinx.android.synthetic.main.activity_scan.*
class ScanActivity : AppCompatActivity() {
private lateinit var captureManager: CaptureManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_scan)
captureManager = CaptureManager(this, dbv)
captureManager.initializeFromIntent(intent, savedInstanceState)
captureManager.decode()
}
private val onBackListener: View.OnClickListener = View.OnClickListener { finish() }
override fun onSaveInstanceState(
outState: Bundle?,
outPersistentState: PersistableBundle?
) {
super.onSaveInstanceState(outState, outPersistentState)
captureManager.onSaveInstanceState(outState)
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
return dbv.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event)
}
override fun onPause() {
super.onPause()
captureManager.onPause()
}
override fun onResume() {
super.onResume()
captureManager.onResume()
}
override fun onDestroy() {
super.onDestroy()
captureManager.onDestroy()
}
}
activity_scan.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="@+id/dbv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:zxing_framing_rect_height="200dp"
app:zxing_framing_rect_width="200dp"
app:zxing_preview_scaling_strategy="fitXY"
app:zxing_scanner_layout="@layout/view_qr"
app:zxing_use_texture_view="true" />
</RelativeLayout>
使用时调用
val integrator = IntentIntegrator(this)
//QR_CODE_TYPES 二维码
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
integrator.captureActivity = ScanActivity::class.java
integrator.initiateScan()
返回结果
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// 跳转扫描页面返回扫描数据
var scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
// 判断返回值是否为空
if (scanResult != null) {
//返回条形码数据
var result = scanResult.contents
refundsNumber.setText(result)
ToastUtils.show(result)
} else {
ToastUtils.show("扫描失败")
}
}