android基於ZXing和ZBar的二維碼掃描項目

android基於ZXing和ZBar的二維碼掃描項目。

基本上我們的實現方法有兩大途徑:ZBar、ZXing。(蘋果原生提供AVFoundation支持二維碼掃描)

ZBar在掃描的靈敏度上和內存的使用上相對於ZXing上都是較優的,但是對於 “圓角二維碼” 的掃描確很困難。

ZXing 是 Google Code上的一個開源的條形碼掃描庫,是用Java設計的,連Google Glass 都在使用的。

Android庫項目,提供基於ZXing和ZBar易於使用,可擴展的條碼掃描器的用法。

ZXing


安裝:
添加下面的依賴關系到你的build.gradle文件。

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

簡單的用法:

1)添加攝像頭權限進AndroidManifest.xml文件:


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

請看看一個完整的工作示例zxing/sample項目。

ZBar


安裝

添加下面的依賴關系到你的build.gradle文件。

compile 'me.dm7.barcodescanner:zbar:1.7.2'

簡單用法
1) Add camera permission to your AndroidManifest.xml file:

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

public class SimpleScannerActivity extends Activity implements ZBarScannerView.ResultHandler {
    private ZBarScannerView mScannerView;

    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);
        mScannerView = new ZBarScannerView(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.getContents()); // Prints scan results
        Log.v(TAG, rawResult.getBarcodeFormat().getName()); // Prints the scan format (qrcode, pdf417 etc.)
    }
}

You May Also Like