6 Star 3 Fork 1

佰钧成开源官方组织 / barcodescanner

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or Download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

Introduction

HarmonyOS library projects that provides easy to use and extensible Barcode Scanner views based on ZXing and ZBar.

Screenshots

main_ability scan_result

ZXing

Installation

Add the following dependency to your build.gradle file.

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
    implementation 'com.google.zxing:core:3.4.0'
    compile project(path: ':zxing')
    compile project(path: ':core')
}

Simple Usage

1.) Add camera permission to your config.json file:

"reqPermissions": [
  {
    "name": "ohos.permission.CAMERA",
    "reason": "open camera",
    "usedScene":
    {
      "ability": ["me.dm7.barcodescanner.zxing.sample.MainAbility",],
      "when": "always"
    }
  }
]

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

public class SimpleScannerAbility extends Ability implements ZXingScannerView.ResultHandler {
    private ZXingScannerView mScannerView;

    @Override
    public void onStart(Bundle state) {
        super.onStart(state);
        // Dynamiclly add ZXingScannerView to content_frame which represents a StackLayout
        ComponentContainer contentFrame = (ComponentContainer) findComponentById(ResourceTable.Id_content_frame);
        mScannerView = new ZXingScannerView(this);
        contentFrame.addComponent(mScannerView);
    }

    @Override
    public void onActive() {
        super.onActive();
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();          // Start camera on active
    }

    @Override
    public void onInActive() {
        super.onInActive();
        mScannerView.stopCamera();           // Stop camera on inactive
    }

    @Override
    public void handleResult(Result rawResult) {
        // Do something with the result here
        HiLog.info(LOG_LABEL, "handleResult," + rawResult.getText());// Prints scan results
        HiLog.info(LOG_LABEL, "handleResult," + rawResult.getBarcodeFormat().toString());// Prints the scan format (qrcode, pdf417 etc.)

        // If you would like to resume scanning, call this method below:
        mScannerView.resumeCameraPreview(this);
    }
}

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

Advanced Usage

Take a look at the FullScannerAbility.java or FullScannerFraction.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 the void 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

ZBar

Installation

Add the following dependency to your build.gradle file.

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.har', '*.so'])
    testCompile'junit:junit:4.12'
    compile project(path: ':core')
}

Simple Usage

1.) Add camera permission to your config.json file:

"reqPermissions": [
  {
    "name": "ohos.permission.CAMERA",
    "reason": "open camera",
    "usedScene":
    {
      "ability": ["me.dm7.barcodescanner.zbar.sample.MainAbility",],
      "when": "always"
    }
  }
]

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

public class SimpleScannerAbility extends Ability implements ZbarScannerView.ResultHandler {
    private ZbarScannerView mScannerView;

    @Override
    public void onStart(Bundle state) {
        super.onStart(state);
        ComponentContainer contentFrame = (ComponentContainer) findComponentById(ResourceTable.Id_content_frame);
        mScannerView = new ZbarScannerView(this);
        contentFrame.addComponent(mScannerView);
    }

    @Override
    public void onActive() {
        super.onActive();
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();          // Start camera on active
    }

    @Override
    public void onInActive() {
        super.onInActive();
        mScannerView.stopCamera();           // Stop camera on inactive
    }

    @Override
    public void handleResult(Result rawResult) {
        // Do something with the result here
        HiLog.info(LOG_LABEL, "handleResult," + rawResult.getText());// Prints scan results
        HiLog.info(LOG_LABEL, "handleResult," + rawResult.getBarcodeFormat().toString());// Prints the scan format (qrcode, pdf417 etc.)

        // If you would like to resume scanning, call this method below:
        mScannerView.resumeCameraPreview(this);
    }
}

Please take a look at the zbar-sample project for a full working example.

Advanced Usage

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

Interesting methods on the ZBarScannerView include:

// Toggle flash:
void setFlash(boolean);

// Toogle autofocus:
void setAutoFocus(boolean);

// Specify interested barcode formats:
void setFormats(List<BarcodeFormat> formats);

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

Supported Formats:

BarcodeFormat.PARTIAL
BarcodeFormat.EAN8
BarcodeFormat.UPCE
BarcodeFormat.ISBN10
BarcodeFormat.UPCA
BarcodeFormat.EAN13
BarcodeFormat.ISBN13
BarcodeFormat.I25
BarcodeFormat.DATABAR
BarcodeFormat.DATABAR_EXP
BarcodeFormat.CODABAR
BarcodeFormat.CODE39
BarcodeFormat.PDF417
BarcodeFormat.QR_CODE
BarcodeFormat.CODE93
BarcodeFormat.CODE128

Include ZBar Libraries

zbar-so

.so library is not well supported on current HarmonyOS version, so we can't include zbar dependency by har/jar, instead we need to import zbar so and jar to entry's libs folder as above.

Credits

Almost all of the code for these library projects is based on:

  1. Camera Preview api from HarmonyOS SDK Documents
  2. The Original barcodescanner project:https://github.com/dm77/barcodescanner
  3. The ZXing project: https://github.com/zxing/zxing
  4. The ZBar SDK: https://github.com/ZBar/ZBar/tree/master/android (Previously: http://sourceforge.net/projects/zbar/files/AndroidSDK/)

License

License for code written in this project is: Apache License, Version 2.0

License for zxing and zbar projects is here:

Repository Comments ( 0 )

Sign in to post a comment

About

基于zxing和zbar提供易于使用的二维码扫描功能 expand collapse
Java
Cancel

Releases

No release

barcodescanner

Contributors

All

Activities

Load More
can not load any more
Java
1
https://gitee.com/baijuncheng-open-source/barcodescanner.git
git@gitee.com:baijuncheng-open-source/barcodescanner.git
baijuncheng-open-source
barcodescanner
barcodescanner
master

Search

161121 f78d6d6f 1850385 154831 86f8c370 1850385