Android集成Zxing
1.在build文件中添加依赖
dependencies {
//ZXing
implementation 'com.google.zxing:core:3.3.3'
implementation('com.journeyapps:zxing-android-embedded:3.6.0') { transitive = false }
2.之后只需要一行代码就可以调用扫码功能
new IntentIntegrator(this).initiateScan();
然后在当前的Activity中重写onActivityResult去获取结果就ok了
3.自定义扫码界面
因为扫码界面的代码非常少,所以可以新建一个Activity复制它的代码。
- 新建一个Activity
package com.onepilltest.index;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.onepilltest.R;
public class ZxingActivity extends AppCompatActivity {
private CaptureManager capture;
private DecoratedBarcodeView bv_barcode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(0xffffffff);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
setContentView(R.layout.zxing);
bv_barcode = (DecoratedBarcodeView) findViewById(R.id.bv_barcode);
capture = new CaptureManager(this, bv_barcode);
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);
}
@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 bv_barcode.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
}
- 然后是View
<?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"
tools:context=".Activity.ScanQRCodeActivity">
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="@+id/bv_barcode"
app:zxing_use_texture_view="false"
app:zxing_preview_scaling_strategy="centerCrop"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
其中DecoratedBarcodeView,它就是扫描界面的核心。
- 接下来更改跳转代码
new IntentIntegrator(this).setCaptureActivity(ScanQRCodeActivity.class).initiateScan();
上面的方法中多了一个setCaptureActivity()方法,这个方法看源码非常简单了,调用这个方法可以把默认的CaptureActivity替换为我们自定义的ScanQRCodeActivity。
- 重写onActivityResult()方法,获取返回的参数
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
Log.e("扫码结果分析", "Cancelled");
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Toast.makeText(this, "扫描结果为空", Toast.LENGTH_LONG).show();
} else {
String str = result.getContents();
Toast.makeText(this, str, Toast.LENGTH_LONG).show();
}
}
}
4.如果想停留在当前Activity
可以注释capture.decode();
然后添加
//回调
private BarcodeCallback barcodeCallback = new BarcodeCallback() {
@Override
public void barcodeResult(BarcodeResult result) {
if (result != null){
Log.e(getClass().getName(), "获取到的扫描结果是:" + result.getText());
Toast.makeText(getApplicationContext(),result.getText(),Toast.LENGTH_SHORT).show();
textView.setText(result.getText());
}
}
@Override
public void possibleResultPoints(List<ResultPoint> resultPoints) {
}
};
然后再添加代码bv_barcode.decodeSingle(this.barcodeCallback);
设置回调函数。
5.如果想连续扫描
将代码替换
// bv_barcode.decodeSingle(this.barcodeCallback);//单次扫描
bv_barcode.decodeContinuous(barcodeCallback);//连续扫描
然后在回调方法和处理方法中添加以下分别调用以下函数
//回调
private BarcodeCallback barcodeCallback = new BarcodeCallback() {
@Override
public void barcodeResult(BarcodeResult result) {
bv_barcode.resume();
if (result != null){
Log.e(getClass().getName(), "获取到的扫描结果是:" + result.getText());
String myresult = result.getText();
if (!results.equals(myresult)){
results = myresult;
Toast.makeText(getApplicationContext(),result.getText(),Toast.LENGTH_SHORT).show();
textView.setText(result.getText());
}
bv_barcode.resume();
}
}
参考:生成二维码和保存二维码
本文来自博客园,作者:两小无猜,转载请注明原文链接:https://www.cnblogs.com/charlottepl/p/12720818.html