易于使用Android的二维码扫描库:barcodescanner

dgbm 9年前

这个Android库提供一个易于使用和可扩展的二维码扫描views,基于ZXing和ZBar。

截图

Portrait

Landscape

ZXing

安装

Add the following dependency to your build.gradle file.

compile 'me.dm7.barcodescanner:zxing:1.8'

简单用法

1.) Add camera permission to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.CAMERA" />

2.) A very basic activity would look like this:

public class SimpleScannerActivity extends Activity implements ZXingScannerView.ResultHandler {      private ZXingScannerView mScannerView;        @Override      public void onCreate(Bundle state) {          super.onCreate(state);          mScannerView = new ZXingScannerView(this);   // Programmatically initialize the scanner view          setContentView(mScannerView);                // Set the scanner view as the content view      }        @Override      public void onResume() {          super.onResume();          mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.          mScannerView.startCamera();          // Start camera on resume      }        @Override      public void onPause() {          super.onPause();          mScannerView.stopCamera();           // Stop camera on pause      }        @Override      public void handleResult(Result rawResult) {          // Do something with the result here          Log.v(TAG, rawResult.getText()); // Prints scan results          Log.v(TAG, rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)      }  }

Please take a look at the zxing/sample project for a full working example.

高级用法

Take a look at the ScannerActivity.java or ScannerFragment.java classes to get an idea on advanced usage.

Interesting methods on the ZXingScannerView include:

// Toggle flash:  void setFlash(boolean);    // Toogle autofocus:  void setAutoFocus(boolean);    // Specify interested barcode formats:  void setFormats(List<BarcodeFormat> formats);    // Specify the cameraId to start with:  void startCamera(int cameraId);

Specify front-facing or rear-facing cameras by using thevoid startCamera(int cameraId);method.

Supported Formats:

BarcodeFormat.UPC_A  BarcodeFormat.UPC_E  BarcodeFormat.EAN_13  BarcodeFormat.EAN_8  BarcodeFormat.RSS_14  BarcodeFormat.CODE_39  BarcodeFormat.CODE_93  BarcodeFormat.CODE_128  BarcodeFormat.ITF  BarcodeFormat.CODABAR  BarcodeFormat.QR_CODE  BarcodeFormat.DATA_MATRIX  BarcodeFormat.PDF_417

项目主页:http://www.open-open.com/lib/view/home/1438308272363