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