第一步
//zing 扫描码
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
第二步
CustomZxingView
public class CustomZxingView extends ViewfinderView {
public int laserLinePosition = 0;
public float[] position = new float[]{0f, 0.5f, 1f};
public int[] colors = new int[]{0xff0699e6, 0xff0699e6, 0xff0699e6};
public LinearGradient linearGradient;
public CustomZxingView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 重写draw方法绘制自己的扫描框
*
* @param canvas
*/
@Override
public void onDraw(Canvas canvas) {
refreshSizes();
if (framingRect == null || previewFramingRect == null) {
return;
}
Rect frame = framingRect;
Rect previewFrame = previewFramingRect;
int width = canvas.getWidth();
int height = canvas.getHeight();
//绘制4个角
paint.setColor(Color.parseColor("#139D57"));//定义画笔的颜色
canvas.drawRect(frame.left, frame.top, frame.left + 100, frame.top + 10, paint);
canvas.drawRect(frame.left, frame.top, frame.left + 10, frame.top + 100, paint);
canvas.drawRect(frame.right - 100, frame.top, frame.right, frame.top + 10, paint);
canvas.drawRect(frame.right - 10, frame.top, frame.right, frame.top + 100, paint);
canvas.drawRect(frame.left, frame.bottom - 10, frame.left + 100, frame.bottom, paint);
canvas.drawRect(frame.left, frame.bottom - 100, frame.left + 10, frame.bottom, paint);
canvas.drawRect(frame.right - 100, frame.bottom - 10, frame.right, frame.bottom, paint);
canvas.drawRect(frame.right - 10, frame.bottom - 100, 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 {
int middle = frame.height() / 2 + frame.top;
laserLinePosition = laserLinePosition + 5;
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 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);
}
}
postInvalidateDelayed(16,
frame.left,
frame.top,
frame.right,
frame.bottom);
}
}
}
第三部
QrCodeActivity
public class QrCodeActivity extends AppCompatActivity implements DecoratedBarcodeView.TorchListener {
private DecoratedBarcodeView decoratedBarcodeView;
private CaptureManager captureManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 5.0以上系统状态栏透明
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
}
setContentView(R.layout.activity_qr_code);
//隐藏底部操作栏
getSupportActionBar().hide();
decoratedBarcodeView=this.findViewById(R.id.decoratedBarcodeView);
//重要代码,初始化捕获
captureManager = new CaptureManager(this, decoratedBarcodeView);
captureManager.initializeFromIntent(getIntent(), savedInstanceState);
captureManager.decode();
decoratedBarcodeView.setTorchListener(this);
}
@Override
public void onTorchOn() {
}
@Override
public void onTorchOff() {
}
@Override
protected void onPause() {
super.onPause();
captureManager.onPause();
}
@Override
protected void onResume() {
super.onResume();
captureManager.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
captureManager.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
captureManager.onSaveInstanceState(outState);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return decoratedBarcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
}
activity_qr_code:
layout_zxing:
在activity 中使用
new IntentIntegrator(this)
.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES)// 扫码的类型,可选:一维码,二维码,一/二维码
//.setPrompt("请对准二维码")// 设置提示语
.setCameraId(0)// 选择摄像头,可使用前置或者后置
.setBeepEnabled(true)// 是否开启声音,扫完码之后会"哔"的一声
.setCaptureActivity(QrCodeActivity.class)//自定义扫码界面
.initiateScan();// 初始化扫码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//扫码结果
IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (intentResult != null) {
if (intentResult.getContents() == null) {
//扫码失败
} else {
String result = intentResult.getContents();//返回值
mShowTv.setText("扫码结果:" + result);
}
}
https://download.csdn.net/download/Jonly_W/12321731