# barcodescanner **Repository Path**: baijuncheng-open-source/barcodescanner ## Basic Information - **Project Name**: barcodescanner - **Description**: 基于zxing和zbar提供易于使用的二维码扫描功能 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 7 - **Forks**: 1 - **Created**: 2021-02-22 - **Last Updated**: 2024-09-23 ## Categories & Tags **Categories**: harmonyos-barcode **Tags**: None ## README Introduction ============ HarmonyOS library projects that provides easy to use and extensible Barcode Scanner views based on ZXing and ZBar. Screenshots =========== ![main_ability](https://images.gitee.com/uploads/images/2021/0309/112257_c20e9dc6_8230582.jpeg "main_ability.jpg") ![scan_result](https://images.gitee.com/uploads/images/2021/0309/112315_53975e26_8230582.jpeg "scan_result.jpg") 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: ```xml "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: ```java 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](https://gitee.com/baijuncheng-open-source/barcodescanner/tree/master/zxing-sample) project for a full working example. Advanced Usage -------------- Take a look at the [FullScannerAbility.java](https://gitee.com/baijuncheng-open-source/barcodescanner/blob/master/zxing-sample/src/main/java/me/dm7/barcodescanner/zxing/sample/FullScannerAbility.java) or [FullScannerFraction.java](https://gitee.com/baijuncheng-open-source/barcodescanner/blob/master/zxing-sample/src/main/java/me/dm7/barcodescanner/zxing/sample/FullScannerFraction.java) classes to get an idea on advanced usage. Interesting methods on the ZXingScannerView include: ```java // Toggle flash: void setFlash(boolean); // Toogle autofocus: void setAutoFocus(boolean); // Specify interested barcode formats: void setFormats(List 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: ```java 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: ```xml "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: ```java 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](https://gitee.com/baijuncheng-open-source/barcodescanner/tree/master/zbar-sample) project for a full working example. Advanced Usage -------------- Take a look at the [FullScannerAbility.java](https://gitee.com/baijuncheng-open-source/barcodescanner/blob/master/zbar-sample/src/main/java/me/dm7/barcodescanner/zbar/sample/FullScannerAbility.java) or [FullScannerFraction.java](https://gitee.com/baijuncheng-open-source/barcodescanner/blob/master/zbar-sample/src/main/java/me/dm7/barcodescanner/zbar/sample/FullScannerFraction.java) classes to get an idea on advanced usage. Interesting methods on the ZBarScannerView include: ```java // Toggle flash: void setFlash(boolean); // Toogle autofocus: void setAutoFocus(boolean); // Specify interested barcode formats: void setFormats(List 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](https://images.gitee.com/uploads/images/2021/0309/112917_69ddcd76_8230582.png "zbar-so.png") .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: * https://github.com/zxing/zxing/blob/master/LICENSE * https://github.com/ZBar/ZBar/tree/master/android