1、环境依赖
IDE:Android Studio
版本管理:gradle
依赖android
implementation 'com.journeyapps:zxing-android-embedded:3.3.0' implementation 'com.google.zxing:core:3.2.1'
2、构建扫码Activity
建立个Activity数组
package com.tmri.license.activity; import android.app.Activity; import android.content.pm.PackageManager; import android.os.Bundle; import android.os.PersistableBundle; import android.view.KeyEvent; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.Toast; import com.journeyapps.barcodescanner.CaptureManager; import com.journeyapps.barcodescanner.DecoratedBarcodeView; import com.tmri.license.R; /** * 摄像头 */ public class CustomScanActivity extends Activity implements DecoratedBarcodeView.TorchListener { Button swichLight; DecoratedBarcodeView mDBV; private CaptureManager captureManager; private boolean isLightOn = false; @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 mDBV.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature( Window.FEATURE_NO_TITLE);//去掉标题栏 setContentView( R.layout.activity_custom_scan); swichLight = (Button) findViewById( R.id.btn_switch ); mDBV = (DecoratedBarcodeView) findViewById( R.id.dbv_custom ); mDBV.setTorchListener(this); // 若是没有闪光灯功能,就去掉相关按钮 if(!hasFlash()) { swichLight.setVisibility(View.GONE); }else{ swichLight.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { if(isLightOn){ mDBV.setTorchOff(); }else{ mDBV.setTorchOn(); } } } ); } //重要代码,初始化捕获 captureManager = new CaptureManager(this,mDBV); captureManager.initializeFromIntent(getIntent(),savedInstanceState); captureManager.decode(); } // torch 手电筒 @Override public void onTorchOn() { Toast.makeText(this,"torch on",Toast.LENGTH_LONG).show(); isLightOn = true; } @Override public void onTorchOff() { Toast.makeText(this,"torch off", Toast.LENGTH_LONG).show(); isLightOn = false; } // 判断是否有闪光灯功能 private boolean hasFlash() { return getApplicationContext().getPackageManager() .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); } }
扫码界面代码app
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal" android:background="#05000000" tools:context="com.tmri.license.activity.CustomScanActivity"> <!-- 我这里只是在大局下修改了一些样式,不过其实 扫描框中的 各类激光条,边框均可以改变,有兴趣的同窗能够本身去搜一下 --> <!-- 这个控件就是扫描的窗口了 --> <com.journeyapps.barcodescanner.DecoratedBarcodeView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/dbv_custom" android:layout_gravity="center" android:gravity="center" app:zxing_framing_rect_width="250dp" app:zxing_framing_rect_height="250dp" app:zxing_preview_scaling_strategy="fitXY" app:zxing_use_texture_view="true" > </com.journeyapps.barcodescanner.DecoratedBarcodeView> <GridLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:columnCount="1" android:focusable="true" android:focusableInTouchMode="true" android:orientation="horizontal"> <Button android:id="@+id/btn_switch" android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center" android:layout_marginTop="350dp" android:background="@drawable/sdt" /> </GridLayout> </RelativeLayout>
3、调用
调用出扫码界面ide
public void customScan(){ new IntentIntegrator(MainActivity.this) .setOrientationLocked(false) .setCaptureActivity(CustomScanActivity.class) // 设置自定义的activity是CustomActivity .initiateScan(); // 初始化扫描 }
返回扫码结果gradle
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); try { // 扫描二维码/条码回传 requestCode == REQUEST_CODE_SCAN && if (resultCode == RESULT_OK) { //若是二维码是byte数组格式,这样获取 byte[] bytes = data.getByteArrayExtra("SCAN_RESULT_BYTE_SEGMENTS_0"); //若是是文本格式以下获取 IntentResult intentResult = IntentIntegrator.parseActivityResult( requestCode, resultCode, data ); if(intentResult != null) { if(intentResult.getContents() == null) { Toast.makeText(MainActivity.this,"内容为空",Toast.LENGTH_LONG).show(); } else { // ScanResult 为 获取到的字符串 String scanResult = intentResult.getContents(); } } } } catch (Exception e){ Toast.makeText(MainActivity.this,e.getMessage(),Toast.LENGTH_LONG).show(); } }