我是靠谱客的博主 壮观马里奥,最近开发中收集的这篇文章主要介绍Android一分钟配置zxing扫码,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1:添加依赖

 implementation 'com.journeyapps:zxing-android-embedded:3.3.0'
 implementation 'com.google.zxing:core:3.3.0'

2:代码初始化:

 IntentIntegrator intentIntegrator = new IntentIntegrator(this);
    //自定义CustomCaptureActivity,默认的是横屏,自定义为竖屏
    intentIntegrator.setCaptureActivity(CustomCaptureActivity.class);
    //ONE_D_CODE_TYPES代表了一维码,QR_CODE_TYPES代表了二维码。
    intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
    //设置扫描界面的文字。
    intentIntegrator.setPrompt("扫描绑定");
    //设置打开照相机的类型,就是0后置摄像头,1前置摄像头
    intentIntegrator.setCameraId(0);
    //设置扫描完成之后是否会有声音。
    intentIntegrator.setBeepEnabled(false);
    //最后调用的一个方法,通过调用它来实现界面的跳转,它的内部通过调用startActivityForResult();来打开照相机的界面。
    intentIntegrator.setBarcodeImageEnabled(false);
    //方向锁定
    intentIntegrator.setOrientationLocked(true);
    //发起扫描
    intentIntegrator.initiateScan();

回调获取数据:

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent intent) {
   
    IntentIntegrator.parseActivityResult(requestCode,resultCode,intent);
    String scanResult = result.getContents();
    System.out.pringt(scanResult);
      }
  }

 

3:自定义扫描界面

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.KeyEvent;

import com.google.android.apps.authenticator2.R;
import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;

/**
 * 自定义扫描页
 */
public class CustomCaptureActivity extends Activity {
    private CaptureManager capture;
    private DecoratedBarcodeView barcodeScannerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.cap_activity);// 自定义布局

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

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

4;xml 文件 cap_activity

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal|center_vertical"

    android:orientation="vertical">

    <com.journeyapps.barcodescanner.DecoratedBarcodeView
        android:id="@+id/dbv_custom"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        app:zxing_preview_scaling_strategy="fitXY" />


</LinearLayout>

5:AndroidManifest.xml 配置

<activity android:name=".CustomCaptureActivity"
              android:configChanges="orientation|screenSize"
              android:theme="@style/AuthenticatorTheme.NoActionBar"/>

启动运行就行。。。。

最后

以上就是壮观马里奥为你收集整理的Android一分钟配置zxing扫码的全部内容,希望文章能够帮你解决Android一分钟配置zxing扫码所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(107)

评论列表共有 0 条评论

立即
投稿
返回
顶部