一.添加权限

二.实现扫码功能

1.创建activity2

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="5dp"

tools:context=".MainActivity1">

android:id="@+id/button_start"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="扫一扫"

/>

android:id="@+id/textview_zxing"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="扫码的结果" />

效果如图

2.编辑对应的MainActivity2

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;

import com.google.zxing.integration.android.IntentResult;

public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {

private TextView textView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_shiyan);

findViewById(R.id.button_start).setOnClickListener(this);

textView=findViewById(R.id.textview_zxing);

}

@Override

public void onClick(View v) {

switch (v.getId()){

case R.id.button_start:

new IntentIntegrator(this)

.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES)// 扫码的类型,可选:一维码,二维码,一/二维码

.setCameraId(0)// 选择摄像头,可使用前置或者后置

.setBeepEnabled(true)// 是否开启声音,扫完码之后会"哔"的一声

.setCaptureActivity(QrCodeActivity.class)//自定义扫码界面

.initiateScan();// 初始化扫码

default:

}

}

@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();//返回值

textView.setText("二维码的值为:"+result);

}

}

}

}

3.创建自定义扫码界面

QrCodeActivity

import android.os.Bundle;

import android.os.PersistableBundle;

import android.view.KeyEvent;

import androidx.appcompat.app.AppCompatActivity;

import com.journeyapps.barcodescanner.CaptureManager;

import com.journeyapps.barcodescanner.DecoratedBarcodeView;

public class QrCodeActivity extends AppCompatActivity implements DecoratedBarcodeView.TorchListener {

private DecoratedBarcodeView decoratedBarcodeView;

private CaptureManager captureManager;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_qr_code);

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

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"

tools:context=".QrCodeActivity"

android:orientation="vertical">

android:id="@+id/decoratedBarcodeView"

android:layout_width="match_parent"

android:layout_height="match_parent"

app:zxing_framing_rect_height="250dp"

app:zxing_framing_rect_width="250dp"

app:zxing_preview_scaling_strategy="fitXY"

app:zxing_scanner_layout="@layout/layout_zxing"

app:zxing_use_texture_view="true" />

/>

4.编写 QrCodeActivity类

import android.os.Bundle;

import android.os.PersistableBundle;

import android.view.KeyEvent;

import androidx.appcompat.app.AppCompatActivity;

import com.journeyapps.barcodescanner.CaptureManager;

import com.journeyapps.barcodescanner.DecoratedBarcodeView;

public class QrCodeActivity extends AppCompatActivity implements DecoratedBarcodeView.TorchListener {

private DecoratedBarcodeView decoratedBarcodeView;

private CaptureManager captureManager;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_qr_code);

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);

}

}

5.编写 CustomZxingView类

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 com.google.zxing.ResultPoint;

import com.journeyapps.barcodescanner.ViewfinderView;

import java.util.ArrayList;

import java.util.List;

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);

}

@Override

public void onDraw(Canvas canvas) {

refreshSizes();

Rect previewFramingRect = null;

if (framingRect == null || previewFramingRect == null) {

return;

}

Rect frame = framingRect;

Rect previewFrame = previewFramingRect;

int width = canvas.getWidth();

int height = canvas.getHeight();

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);

}

}

}

三.效果展示

 

相关阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。