一:添加依赖
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
二:添加权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
三:自定义扫码界面布局文件activity_capture.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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=".CaptureActivity"
android:gravity="center_horizontal"
android:orientation="vertical">
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="@+id/dbv_custom"
android:layout_width="match_parent"
android:layout_height="235dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:layout_marginRight="20dp"
android:clickable="false"
app:zxing_preview_scaling_strategy="fitXY"/>
</LinearLayout>
四:自定义扫码界面
package com.example.demo;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
public class CaptureActivity extends AppCompatActivity {
private CaptureManager capture;
private DecoratedBarcodeView barcodeScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_capture);// 自定义布局
barcodeScannerView = (DecoratedBarcodeView) findViewById(R.id.dbv_custom);
capture = new CaptureManager(this, barcodeScannerView);
capture.initializeFromIntent(getIntent(), savedInstanceState);
capture.decode();
}
@Override
protected void onResume() {
super.onResume();
capture.onResume();
}
@Override
protected void onPause() {
super.onPause();
capture.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
capture.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
capture.onSaveInstanceState(outState);
}
@SuppressLint("MissingSuperCall")
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
capture.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
}
五:调用扫码功能界面布局文件activity_scan.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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=".MainActivity"
android:gravity="center"
android:orientation="vertical"
android:contentDescription=" hf_no_number">
<TextView
android:id="@+id/text_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:contentDescription=" hf_no_number"/>
<Button
android:id="@+id/btn"
android:text="扫码"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription=" hf_no_number"/>
</LinearLayout>
六:调用扫码功能界面
package com.example.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
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 ScanActivity extends AppCompatActivity{
private TextView text_scan;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_scan);
initView();
}
private void initView() {
text_scan = findViewById(R.id.text_scan);
findViewById(R.id.btn).setOnClickListener(view -> initScan());
}
public void initScan() {
IntentIntegrator integrator = new IntentIntegrator(this);
// 设置要扫描的条码类型,ONE_D_CODE_TYPES:一维码,QR_CODE_TYPES-二维码
integrator.setDesiredBarcodeFormats();
integrator.setCaptureActivity(CaptureActivity.class); //设置打开摄像头的Activity
integrator.setPrompt("请对准二维码"); //底部的提示文字,设为""可以置空
integrator.setCameraId(0); //前置或者后置摄像头
integrator.setBeepEnabled(false); //扫描成功的「哔哔」声,默认开启
integrator.setBarcodeImageEnabled(true);
integrator.initiateScan();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == IntentIntegrator.REQUEST_CODE) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null && scanResult.getContents() != null) {
String result = scanResult.getContents();
text_scan.setText(result);
}
}
}
}