# code-scanner **Repository Path**: openneusoft/code-scanner ## Basic Information - **Project Name**: code-scanner - **Description**: 实现了相机的一些功能和条形码扫描的功能 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-04-25 - **Last Updated**: 2024-07-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ![Code Scanner](code_scanner/src/main/resources/base/media/logo_wide_512.png) [Code scanner](https://gitee.com/openneusoft/code-scanner) library for [harmonyos](https://developer.harmonyos.com), based on [ZXing](https://github.com/zxing/zxing) ### Project introduction * Project name:code-scanner * Series:Third party component adaptation and transplantation of ohos * function:code-scanner mainly supports barcode scanning and some basic functions of camera * status:complete * Original project version:v2.1.0, sha1:cdec21fc1fa2a15f342eb98d93c38167458ea026 * programing language:Java * External library dependencies:zxing ### Features * Auto focus and flash light control * Portrait and screen orientations * Back and front facing cameras landscape * Customizable viewfinder * Touch focus ### Supported formats | 1D product | 1D industrial | 2D | ---------- | ------------- | -------------- | UPC-A | Code 39 | QR Code | UPC-E | Code 93 | Data Matrix | EAN-8 | Code 128 | Aztec | EAN-13 | Codabar | PDF 417 | | ITF | MaxiCode | | RSS-14 | | | RSS-Expanded | ### Usage ##### Method 1 Import the compiled har package into the project directory "entry->libs", and add a dependency in the build.gradle file: ```gradle dependencies { implementation project(":code_scanner") } ``` ##### Method 2. In allprojects of build.gradle of the project, add the Maven address where HAR is located: ```gradle repositories { maven { url 'https://s01.oss.sonatype.org/content/repositories/releases/' } } ``` In the dependencies closure of the build.gradle of the application module, add the following code: ```gradle dependencies { implementation ' io.github.dzsf:code-scanner:1.0.1 ' } ``` Add camera permission to config.json (Don't forget about dynamic permissions): ```json { "reqPermissions": [ { "name": "ohos.permission.CAMERA", "reason": "Need camera permission", "usedScene": { "ability": [ "com.budiyev.ohos.libdemoapp.codescanner.MainAbility", "com.budiyev.ohos.libdemoapp.codescanner.CodeScannerAbility" ], "when": "always" } } ] } ``` Define a view in your layout file: ```xml ``` And add following code to your ability and ability slice: Java ```java public class CodeScannerAbility extends Ability { @Override public void onStart(Intent intent) { super.onStart(intent); super.setMainRoute(CodeScannerSlice.class.getName()); } } ``` ```java public class CodeScannerSlice extends AbilitySlice { private CodeScanner mCodeScanner; private CodeScannerView mCodeScannerView; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_code_scanner); mCodeScannerView = (CodeScannerView) findComponentById(ResourceTable.Id_scanner_view); mCodeScanner = new CodeScanner(this, mCodeScannerView); mCodeScanner.setDecodeCallback(result -> getUITaskDispatcher().syncDispatch(() -> new CommonDialog(getContext()) .setTitleText("Scan result") .setTitleSubText("Format: " + result.getBarcodeFormat()) .setContentText(result.getText()) .setAlignment(LayoutAlignment.CENTER) .show())); mCodeScanner.setErrorCallback(error -> getUITaskDispatcher().syncDispatch(() -> new ToastDialog(getContext()) .setText("Error:" + error) .setAlignment(LayoutAlignment.CENTER) .show())); } @Override public void onActive() { super.onActive(); mCodeScanner.startPreview(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } public void onBackground() { super.onBackground(); } public void onStop() { mCodeScanner.stopPreview(); WindowManager.getInstance().getTopWindow().get().setTransparent(false); super.onStop(); } @Override public void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrientation) { super.onOrientationChanged(displayOrientation); mCodeScannerView.setDisplayOrientation(displayOrientation); } } ``` ### Version * v1.0.1 ### Preview ![](code-scanner.gif)