N_Android

Zxing

ZXing是一个开源的, 用Java实现的多种格式的1D/2D条码图像处理库, 它包含了联系到其他语言的端口.zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码

ZXing Android Embedded

实例

依赖

Add the following to your build.gradle file:

// Config for SDK 24+
repositories {
    mavenCentral()
}
dependencies {
    implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
}

Older SDK versions

By default, only SDK 24+ will work, even though the library specifies 19 as the minimum version. For SDK versions 19+, one of the changes below are required. Some older SDK versions below 19 may work, but this is not tested or supported.

repositories {
    mavenCentral()
}
 
dependencies {
    implementation('com.journeyapps:zxing-android-embedded:4.3.0') { transitive = false }
    implementation 'com.google.zxing:core:3.3.0'
}

启用硬件加速

Hardware acceleration is required since TextureView is used.

Make sure it is enabled in your manifest file:

    <application android:hardwareAccelerated="true" ... >

代码

Note: startActivityForResult is deprecated, so this example uses registerForActivityResult instead. See for details: https://developer.android.com/training/basics/intents/result

startActivityForResult can still be used via IntentIntegrator, but that is not recommended anymore.

// Register the launcher and result handler
private final ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
	result -> {
		if(result.getContents() == null) {
			Toast.makeText(MyActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
		} else {
			Toast.makeText(MyActivity.this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
		}
	});
 
// Launch
public void onButtonClick(View view) {
    barcodeLauncher.launch(new ScanOptions());
}

ScanOptions 的自定义选项 https://github.com/journeyapps/zxing-android-embedded/blob/master/zxing-android-embedded/src/com/journeyapps/barcodescanner/ScanOptions.java

ScanOptions options = new ScanOptions();
options.setDesiredBarcodeFormats(ScanOptions.ONE_D_CODE_TYPES);
options.setPrompt("Scan a barcode");
options.setCameraId(0);  // Use a specific camera of the device
options.setBeepEnabled(false);// beep声音
options.setBarcodeImageEnabled(true);
barcodeLauncher.launch(options);

自定义UI 显示区域?

布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.joinken.application.activity.ConfigActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <com.journeyapps.barcodescanner.DecoratedBarcodeView
            android:id="@+id/dbv_custom"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            app:zxing_preview_scaling_strategy="fitXY" />
        <WebView
            android:id="@+id/web_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
 
</android.support.constraint.ConstraintLayout>
 
 

扣源码 : 关键是 com.journeyapps.barcodescanner.CaptureManager 这个类, 它负责管理扫描, 和返回结果

  1. 继承扩展
public class CaptureManager extends com.journeyapps.barcodescanner.CaptureManager{
 
    private Consumer<BarcodeResult> callback;
    public CaptureManager(Activity activity, DecoratedBarcodeView barcodeView) {
        super(activity, barcodeView);
    }
    @Override
    protected void returnResult(BarcodeResult rawResult) {
        Intent intent = resultIntent(rawResult, null);
        callback.accept(rawResult);
    }
}
  1. 创建的时候给它一个显示( DecoratedBarcodeView 组件)区域
  2. 调用其decode(); 开始实时扫描
  3. 回调returnResult(BarcodeResult rawResult) 得到结果
 
 
public class QRCodeActivity extends AppCompatActivity {
    private CaptureManager capture;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrcode);
        // 摄像头画面绘制区域
        DecoratedBarcodeView barcodeView = findViewById(R.id.barcode_scanner);
        barcodeView.setStatusText("请扫描设备上的二维码");
        // 设置只识别 QR_CODE
        barcodeView.setDecoderFactory(new DefaultDecoderFactory(
                EnumSet.of(BarcodeFormat.QR_CODE)
        ));
        //管理生命周期
        capture = new CaptureManager(this, barcodeView);
        //回调结果
        capture.setCallback(barcodeResult -> {
            XLog.i("结果 ", barcodeResult);
            String text = barcodeResult.getResult().getText();
            finish();
        });
        //初始化
        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();
    }
 
}

踩坑指南

build.gradle(Module)

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    // https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
    compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.2'
 
}

另外有资源覆盖问题, 在android加入, packagingOptions 排除掉文件

apply plugin: 'com.android.application'
 
android {
   ...
    packagingOptions{
        exclude 'META-INF/NOTICE' // will not include NOTICE file
        exclude 'META-INF/LICENSE' // will not include LICENSE file
        // as noted by @Vishnuvathsan you may also need to include
        // variations on the file name. It depends on your dependencies.
        // Some other common variations on notice and license file names
        exclude 'META-INF/notice'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/DEPENDENCIES'
    }
}
configurations.all {
	resolutionStrategy.force 'com.android.support:support-annotations:27.1.1'
}

Error:Execution failed for task ‘:app:preDebugAndroidTestBuild’.

Conflict with dependency ‘com.android.support:support-annotations’ in project ‘:app’. Resolved versions for app (26.1.0) and test app (27.1.1) differ. See https://d.android.com/r/tools/test-apk-dependency-conflicts.html for details.

NBZxing

对于 Zxing 的扩展, 支持反相二维码, 解码能力更强

implementation ("com.github.ailiwean:NBZxing:0.2.5")  
implementation ("com.github.ailiwean:NBZxing-Scale:0.0.6")

Java

package cn.simae.tny.njzj.activity;
 
import androidx.activity.result.ActivityResultLauncher;
import androidx.appcompat.app.AppCompatActivity;
 
import android.app.Activity;
import android.bluetooth.le.ScanCallback;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.View;
 
import com.ailiwean.core.Result;
import com.ailiwean.core.helper.ScanHelper;
import com.elvishew.xlog.XLog;
 
 
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
 
import cn.simae.tny.njzj.R;
import cn.simae.tny.njzj.protocol.dto.MessageKey;
import cn.simae.tny.njzj.web.dto.InteractiveMessage;
import cn.simae.tny.njzj.web.js.WebInteractiveFactory;
 
 
public class QRCodeActivity extends AppCompatActivity implements Consumer<Result> {
    private CusScanView scanView;     // 扫描渲染界面
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrcode);
        scanView = findViewById(R.id.barcode_scanner);
        scanView.synchLifeStart(this);
        //输入序列号
        View input_serie = findViewById(R.id.input_serie);
        input_serie.setOnClickListener(v->{
            Map<String, String> payload = Map.of("action", "uniqueCode");
            InteractiveMessage interactiveMessage = InteractiveMessage.create("qr/scan",payload);
            WebInteractiveFactory.Companion.currentInstance().ifPresent(e->e.response(interactiveMessage));
            finish();
        });
        //返回
        View but_ret = findViewById(R.id.but_ret);
        but_ret.setOnClickListener(v->{
            finish();
        });
    }
 
    //回调结果
    @Override
    public void accept(Result result) {
        Map<String, String> payload = Map.of(MessageKey.MESSAGE_KEY_RESULT, result.getText());
        InteractiveMessage interactiveMessage = InteractiveMessage.create("qr/scan",payload);
        WebInteractiveFactory.Companion.currentInstance().ifPresent(e->e.response(interactiveMessage));
        finish();
    }
 
 
 
}

activity xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/ret"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.QRCodeActivity">
 
 
    <cn.simae.tny.njzj.activity.CusScanView
        android:id="@+id/barcode_scanner"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp" >
 
    </cn.simae.tny.njzj.activity.CusScanView>
 
    <ImageView
        android:id="@+id/but_ret"
        android:layout_width="60dp"
        android:layout_height="25dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/qr_code_activity_ret_but" />
 
    <ImageView
        android:id="@+id/input_serie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="32dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/qr_code_activity_series_num_but" />
 
</androidx.constraintlayout.widget.ConstraintLayout>