# Zxing-Embedded **Repository Path**: baijuncheng-open-source/zxing-embedded ## Basic Information - **Project Name**: Zxing-Embedded - **Description**: 集成zxing核心包,通过打开相机预览进行二维码、条形码等各种格式的码图进行解析并返回解析结果。 自定义了解码控件,也自定义了相机预览控件,可配置预览解码控件的布局 可以随时停止和恢复预览功能及解码功能。 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 4 - **Forks**: 2 - **Created**: 2021-02-18 - **Last Updated**: 2022-09-06 ## Categories & Tags **Categories**: harmonyos-barcode **Tags**: None ## README # ZXing Embedded Barcode scanning library for HarmonyOS, using [ZXing][2] for decoding. The project is loosely based on the [ZXing Barcode Scanner application][2], but is not affiliated with the official ZXing project. Features: 1. Can be used via Intents (little code required). 2. Can be embedded in an Ability, for advanced customization of UI and logic. 3. Scanning can be performed in landscape or portrait mode. 4. Camera is managed in a background thread, for fast startup time. A sample application is available [here](https://gitee.com/baijuncheng-open-source/zxing-embedded/tree/master/entry). ## Adding har dependency with Gradle Add the following to your `build.gradle` file: Solution 1: local source integration, users can be customized modifications 1. Copy zxing_embedded folder to project directory; 2. Modify Settings. Gradle under the project to add dependencies to this module as follows: ![settings.gradle](https://images.gitee.com/uploads/images/2021/0312/175823_b4e75b63_8230582.png "微信图片_20210312175637.png") 3. For example, we need to modify the build.gradle file under entry module to add dependencies: ![dependencies](https://images.gitee.com/uploads/images/2021/0312/175851_f23e28b6_8230582.png "微信图片_20210312175642.png") Solution 2: Local HAR package integration 1. Compile this project and copy the HAR package generated under the build directory of zxing_embedded folder into the project lib folder: ![har](https://images.gitee.com/uploads/images/2021/0312/175947_c9ec8296_8230582.png "微信图片_20210312175646.png") 2. Add the following code to gradle of Entry ```xml implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) ``` Solution 3: remote maven repo integration Add the following code in the entry's gradle ```xml implementation 'com.gitee.baijuncheng-open-source:Zxing-Embedded:1.0.0' ``` ## Usage with IntentIntegrator Launch the intent with the default options: ```java new IntentIntegrator(this).initiateScan(); // `this` is the current Ability // Get the results: @Override protected void onAbilityResult(int requestCode, int resultCode, Intent data) { IntentResult result = IntentIntegrator.parseAbilityResult(requestCode, resultCode, data); if (result != null) { String toast; if (result.getContents() == null) { toast = "Cancelled from fragment"; } else { toast = "Scanned from fragment: " + result.getContents(); } // At this point we may or may not have a reference to the activity displayToast(toast); } return; } ``` Use from a Fraction: ```java IntentIntegrator.forSupportFragment(ScanFraction.this).initiateScan();// `this` is the current Fraction // If you're using the support library, use IntentIntegrator.forSupportFragment(ScanFraction.this) instead. ``` Customize options: ```java IntentIntegrator integrator = new IntentIntegrator(this); integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES); integrator.setPrompt("Scan a barcode"); integrator.setCameraId(0); // Use a specific camera of the device integrator.setBeepEnabled(false); integrator.setBarcodeImageEnabled(true); integrator.initiateScan(); ``` See [IntentIntegrator][https://gitee.com/baijuncheng-open-source/zxing-embedded] for more options. ### Generate Barcode example While this is not the primary purpose of this library, it does include basic support for generating some barcode types: ```java if (source != null) { rawResult = decoder.decode(source); } if (rawResult != null) { if (resultHandler != null) { BarcodeResult barcodeResult = new BarcodeResult(rawResult, sourceData); InnerEventinnerEvent=InnerEvent.get(zxing_decode_succeeded,barcodeResult); PacMap pacMap = new PacMap(); innerEvent.setPacMap(pacMap); handler.sendEvent(innerEvent); } } else { if (resultHandler != null) { InnerEvent innerEvent = InnerEvent.get(zxing_decode_failed); handler.sendEvent(innerEvent); } } if (resultHandler != null) { List resultPoints = BarcodeResult.transformResultPoints(decoder.getPossibleResultPoints(), sourceData); InnerEvent innerEvent = InnerEvent.get(zxing_possible_result_points, resultPoints); handler.sendEvent(innerEvent); } requestNextPreview(); No customization of the image is currently supported, including changing colors or padding. If you require more customization, copy and modify the source for the encoder. ``` ### Changing the orientation To change the orientation, specify the orientation in your `config.json` and let the `ManifestMerger` to update the Ability's definition. Sample: ```xml "abilities": [ { "orientation": "unspecified", "name": "com.harmonyos.zxingembedded.journeyapps.barcodescanner.CaptureActivity", "icon": "$media:icon", "description": "$string:ability_description", "label": "zxing_harmonyos_embedded", "type": "page", "launchType": "standard" } ] ``` ```java IntentIntegrator integrator = new IntentIntegrator(this); integrator.setOrientationLocked(false); integrator.initiateScan(); ``` ### Customization and advanced options For more advanced options, look at the [Sample Application](https://gitee.com/baijuncheng-open-source/zxing-embedded/blob/master/entry/src/main/java/com/harmonyos/zxing/MainAbility.java), and browse the source code of the library. This is considered advanced usage, and is not well-documented or supported. ## HarmonyOS Permissions The camera permission is required for barcode scanning to function. It is automatically included as part of the library. It is requested at runtime when the barcode scanner is first opened. When using BarcodeView directly (instead of via IntentIntegrator / CaptureActivity), you have to request the permission manually before calling `BarcodeView#resume()`, otherwise the camera will fail to open. ## Building locally ./gradlew assemble To deploy the artifacts the your local Maven repository: ./gradlew publishToMavenLocal You can then use your local version by specifying in your `build.gradle` file: repositories { mavenLocal() } ## License Licensed under the [Apache License 2.0][7] Copyright (C) 2012-2018 ZXing authors, Journey Mobile Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. [1]: http://journeyapps.com [2]: https://github.com/zxing/zxing/ [3]: https://github.com/zxing/zxing/wiki/Scanning-Via-Intent [4]: https://github.com/journeyapps/zxing-android-embedded/blob/2.x/README.md [5]: zxing-android-embedded/src/com/google/zxing/integration/android/IntentIntegrator.java [7]: http://www.apache.org/licenses/LICENSE-2.0