├── .circleci └── config.yml ├── .editorconfig ├── .github └── workflows │ ├── build.yml │ ├── docs.yml │ └── issues-stale.yml ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── GIF.gif ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro ├── release │ ├── app-release.apk │ └── output-metadata.json └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── king │ │ └── zxing │ │ └── app │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── ic_launcher-web.png │ ├── java │ │ └── com │ │ │ └── king │ │ │ └── zxing │ │ │ └── app │ │ │ ├── CodeActivity.java │ │ │ ├── FullScreenQRCodeScanActivity.kt │ │ │ ├── MainActivity.java │ │ │ ├── MultiFormatScanActivity.kt │ │ │ └── QRCodeScanActivity.java │ └── res │ │ ├── anim │ │ ├── in.xml │ │ └── out.xml │ │ ├── drawable-xxhdpi │ │ ├── btn_back_normal.png │ │ ├── btn_back_pressed.png │ │ ├── btn_none.png │ │ └── logo.png │ │ ├── drawable │ │ ├── btn_back_selector.xml │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── activity_qrcode_scan.xml │ │ ├── code_activity.xml │ │ ├── toolbar.xml │ │ ├── toolbar_capture.xml │ │ └── top_title_back_bar.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── ic_launcher_background.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── king │ └── zxing │ └── app │ └── ExampleUnitTest.java ├── build.gradle ├── build_docs.sh ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── jitpack.yml ├── mkdocs.yml ├── settings.gradle ├── versions.gradle └── zxing-lite ├── .gitignore ├── bintray.gradle ├── build.gradle ├── consumer-rules.pro ├── gradle.properties ├── proguard-rules.pro └── src ├── androidTest └── java │ └── com │ └── king │ └── zxing │ └── ExampleInstrumentedTest.java ├── main ├── AndroidManifest.xml ├── java │ └── com │ │ └── king │ │ └── zxing │ │ ├── BarcodeCameraScanActivity.java │ │ ├── BarcodeCameraScanFragment.java │ │ ├── DecodeConfig.java │ │ ├── DecodeFormatManager.java │ │ ├── analyze │ │ ├── AreaRectAnalyzer.java │ │ ├── BarcodeFormatAnalyzer.java │ │ ├── ImageAnalyzer.java │ │ ├── MultiFormatAnalyzer.java │ │ └── QRCodeAnalyzer.java │ │ └── util │ │ └── CodeUtils.java └── res │ └── layout │ └── zxl_camera_scan.xml └── test └── java └── com └── king └── zxing └── ExampleUnitTest.java /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | 3 | orbs: 4 | android: circleci/android@3.0.0 5 | 6 | jobs: 7 | build: 8 | docker: 9 | - image: cimg/android:2023.08 10 | steps: 11 | - checkout 12 | - run: 13 | command: ./gradlew build 14 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.{yml,yaml}] 11 | indent_size = 2 12 | 13 | [*.{kt, kts}] 14 | ij_kotlin_imports_layout = * -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | env: 10 | JAVA_VERSION: 17 11 | 12 | jobs: 13 | build: 14 | runs-on: ubuntu-latest 15 | 16 | steps: 17 | - name: Checkout 18 | uses: actions/checkout@v4 19 | with: 20 | fetch-depth: 0 21 | 22 | - name: Configure JDK 23 | uses: actions/setup-java@v4 24 | with: 25 | distribution: 'zulu' 26 | java-version: ${{ env.JAVA_VERSION }} 27 | - name: Build with Gradle 28 | run: ./gradlew build 29 | -------------------------------------------------------------------------------- /.github/workflows/docs.yml: -------------------------------------------------------------------------------- 1 | name: docs 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | 8 | env: 9 | JAVA_VERSION: 17 10 | PYTHON_VERSION: 3.x 11 | GRADLE_OPTS: "-Dorg.gradle.jvmargs=-Xmx4g -Dorg.gradle.daemon=false -Dkotlin.incremental=false" 12 | 13 | permissions: 14 | contents: write 15 | id-token: write 16 | pages: write 17 | 18 | jobs: 19 | docs: 20 | environment: 21 | name: github-pages 22 | url: ${{ steps.deployment.outputs.page_url }} 23 | runs-on: ubuntu-latest 24 | if: github.ref == 'refs/heads/master' 25 | 26 | steps: 27 | - name: Checkout 28 | uses: actions/checkout@v4 29 | with: 30 | fetch-depth: 0 31 | 32 | - name: Configure JDK 33 | uses: actions/setup-java@v4 34 | with: 35 | distribution: 'zulu' 36 | java-version: ${{ env.JAVA_VERSION }} 37 | 38 | - name: Install Python 39 | uses: actions/setup-python@v5 40 | with: 41 | python-version: ${{ env.PYTHON_VERSION }} 42 | 43 | - name: Install MkDocs Material 44 | run: pip install mkdocs-material 45 | 46 | - name: Generate Docs 47 | run: ./build_docs.sh 48 | 49 | - name: Upload to GitHub Pages 50 | uses: actions/upload-pages-artifact@v3 51 | with: 52 | path: site 53 | 54 | - name: Deploy to GitHub Pages 55 | id: deployment 56 | uses: actions/deploy-pages@v4 57 | -------------------------------------------------------------------------------- /.github/workflows/issues-stale.yml: -------------------------------------------------------------------------------- 1 | name: 'Close stale issues and PRs' 2 | on: 3 | schedule: 4 | - cron: '0 16 * * *' 5 | 6 | jobs: 7 | stale: 8 | runs-on: ubuntu-latest 9 | permissions: 10 | issues: write 11 | pull-requests: write 12 | steps: 13 | - uses: actions/stale@v5 14 | with: 15 | stale-issue-message: 'This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.' 16 | days-before-stale: 30 17 | days-before-close: 5 18 | exempt-all-pr-milestones: true 19 | exempt-issue-labels: 'help wanted' -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | jdk: oraclejdk8 3 | before_install: 4 | - yes | sdkmanager "platforms;android-28" 5 | 6 | env: 7 | global: 8 | - ANDROID_API_LEVEL=28 9 | - ANDROID_BUILD_TOOLS_VERSION=28.0.3 10 | - TRAVIS_SECURE_ENV_VARS=true 11 | 12 | android: 13 | components: 14 | # The BuildTools version used by your project 15 | - tools 16 | - platform-tools 17 | - build-tools-$ANDROID_BUILD_TOOLS_VERSION 18 | - extra-android-m2repository 19 | - extra-google-android-support 20 | 21 | # The SDK version used to compile your project 22 | - android-$ANDROID_API_LEVEL 23 | licenses: 24 | - '.+' 25 | 26 | script: 27 | - ./gradlew clean 28 | - ./gradlew assembleDebug -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 版本日志 2 | 3 | #### v3.2.0:2024-07-16 4 | * 更新CameraScan至v1.2.0 5 | * 更新ViewfinderView至v1.2.0 6 | * 优化细节 7 | 8 | #### v3.1.1:2024-04-29 9 | * 更新CameraScan至v1.1.1 10 | * 更新zxing至v3.5.3 11 | 12 | #### v3.1.0:2023-12-31 13 | * 更新CameraScan至v1.1.0 14 | * 更新zxing至v3.5.2 15 | * 更新compileSdkVersion至34 16 | * 更新Gradle至v8.0 17 | 18 | #### v3.0.1:2023-9-13 19 | * 更新CameraScan至v1.0.1 20 | * 更新ViewfinderView至v1.1.0 21 | 22 | #### v3.0.0:2023-8-23 23 | * 将通用基础类拆分移除并进行重构,后续维护更便捷 24 | * 移除 **CameraScan** 相关核心类,改为依赖[CameraScan](https://github.com/jenly1314/CameraScan) 25 | * 移除扫码取景视图 **ViewfinderView**,改为依赖[ViewfinderView](https://github.com/jenly1314/ViewfinderView) 26 | * 移除**CaptureActivity**和****CaptureFragment**,新增**BarcodeCameraScanActivity**和****BarcodeCameraScanFragment**来替代 27 | * 优化扫描分析过程的性能体验(优化帧数据分析过程) 28 | 29 | #### v2.4.0:2023-4-15 30 | * 优化CameraScan的缺省配置(CameraConfig相关配置) 31 | * 优化ViewfinderView自定义属性(新增laserDrawableRatio) 32 | * 优化ImageAnalyzer中YUV数据的处理 33 | * 更新CameraX至v1.2.2 34 | 35 | #### v2.3.1:2023-3-4 36 | * 更新CameraX至v1.2.1 37 | * 更新Gradle至v7.5 38 | * 优化细节 39 | 40 | #### v2.3.0:2022-12-11 41 | * 更新CameraX至v1.2.0 42 | * 更新zxing至v3.5.1 43 | * 更新compileSdkVersion至33 44 | 45 | #### v2.2.1:2022-6-22 46 | * 更新CameraX至v1.1.0-rc02 47 | 48 | #### v2.2.0:2022-5-31 49 | * 更新CameraX至v1.1.0-rc01 50 | * 更新compileSdkVersion至31 51 | * 更新Gradle至v7.2 52 | 53 | #### v2.1.1:2021-8-4 54 | * 更新CameraX至v1.0.1 55 | * 优化CameraConfig的一些默认配置 56 | 57 | #### v2.1.0:2021-6-30 (从v2.1.0开始不再发布至JCenter) 58 | * 更新CameraX至v1.0.0 59 | * 优化细节 60 | * 发布至MavenCentral 61 | 62 | #### v2.0.3:2021-3-26 63 | * 更新CameraX至v1.0.0-rc03 64 | * 优化一些默认配置 65 | 66 | #### v2.0.2:2021-1-14 67 | * **ViewfinderView** 新增 **labelTextWidth** 属性 68 | 69 | #### v2.0.1:2020-12-30 70 | * 更新CameraX至v1.0.0-rc01 71 | * 新增支持点击预览区域对焦目标 72 | * 修改一些默认配置 73 | * 优化细节 74 | 75 | #### v2.0.0:2020-12-24 76 | * 基于CameraX进行重构 77 | * 抽象整体流程,可扩展性更高 78 | * 从2.x开始只支持AndroidX 79 | * minSdk要求从 **16+** 改为 **21+** 80 | 81 | #### v1.1.9:2020-4-28 82 | * 修复1.1.8版本优化细节时,不小心改出个Bug(fix #86) 83 | 84 | #### v1.1.8:2020-4-27 85 | * 统一日志管理 86 | * 优化细节 87 | 88 | #### v1.1.7:2020-3-29 89 | * 优化一些默认参数配置 90 | * 修复扫码界面开启闪光灯并切到后台时,手电筒按钮状态未同步问题(fix #81) 91 | 92 | #### v1.1.6:2019-12-27 93 | * 生成条形码/二维码时支持自定义配置颜色 94 | * 支持识别反色码(增强识别率,默认不支持,需通过 **CaptureHelper.supportLuminanceInvert(true)** 开启) 95 | 96 | #### v1.1.5:2019-12-16 97 | * 优化Camera初始化相关策略,减少出现卡顿的可能性 98 | 99 | #### v1.1.4:2019-11-18 100 | * 内置手电筒按钮,当光线太暗时,自动显示手电筒 (fix #58) 101 | * 生成二维码时Logo支持自定义大小 (fix #62) 102 | 103 | #### v1.1.3:2019-9-24 104 | * 支持真实识别区域比例和识别区域偏移量可配置 105 | * 对外暴露更多可配置参数 106 | 107 | #### v1.1.2:2019-6-27 108 | * 优化部分细节,为迁移至AndroidX做准备 109 | * 支持AndroidX对应版本 110 | 111 | #### v1.1.1:2019-5-20 112 | * 支持扫二维码过小时,自动缩放 113 | * 支持识别垂直条形码(增强条形码识别,默认不支持,需通过 **CaptureHelper.supportVerticalCode(true)** 开启) 114 | 115 | #### v1.1.0:2019-4-19 116 | * 将扫码相关逻辑与界面分离,ZXingLite使用更容易扩展 117 | * 新增CaptureFragment 118 | 119 | #### v1.0.7:2019-4-9 120 | * 新增网格样式的扫描激光(类似支付宝扫码样式) 121 | * 升级Gradle至v4.6 122 | 123 | #### v1.0.6:2019-1-16 124 | * 支持连续扫码 125 | * 支持横屏扫码(主要为了支持Pad) 126 | 127 | #### v1.0.5:2018-12-29 128 | * 支持自定义扫码框宽高 129 | 130 | #### v1.0.4:2018-12-19 131 | * 修改text相关自定义属性,如:text->labelText 132 | 133 | #### v1.0.3:2018-11-20 134 | * 支持触摸缩放变焦 135 | 136 | #### v1.0.2:2018-9-12 137 | * 支持条形码下方显示显示code 138 | * 优化相机预览尺寸遍历策略,从而降低预览变形的可能性 139 | 140 | #### v1.0.1:2018-8-23 141 | * 优化扫码识别速度 142 | 143 | #### v1.0.0:2018-8-9 144 | * ZXingLite初始版本 145 | -------------------------------------------------------------------------------- /GIF.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jenly1314/ZXingLite/c4fb1e12d4ed575b3d41879828ee8a1d17428fe7/GIF.gif -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ZXingLite 2 | 3 | ![Image](app/src/main/ic_launcher-web.png) 4 | 5 | [![MavenCentral](https://img.shields.io/maven-central/v/com.github.jenly1314/zxing-lite?logo=sonatype)](https://repo1.maven.org/maven2/com/github/jenly1314/ZXingLite) 6 | [![JitPack](https://img.shields.io/jitpack/v/github/jenly1314/ZXingLite?logo=jitpack)](https://jitpack.io/#jenly1314/ZXingLite) 7 | [![CI](https://img.shields.io/github/actions/workflow/status/jenly1314/ZXingLite/build.yml?logo=github)](https://github.com/jenly1314/ZXingLite/actions/workflows/build.yml) 8 | [![Download](https://img.shields.io/badge/download-APK-brightgreen?logo=github)](https://raw.githubusercontent.com/jenly1314/ZXingLite/master/app/release/app-release.apk) 9 | [![API](https://img.shields.io/badge/API-21%2B-brightgreen?logo=android)](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) 10 | [![License](https://img.shields.io/github/license/jenly1314/ZXingLite?logo=open-source-initiative)](https://opensource.org/licenses/apache-2-0) 11 | 12 | 13 | ZXingLite for Android 是ZXing的精简极速版,基于ZXing库优化扫码和生成二维码/条形码功能,扫码界面完全支持自定义;使用ZXingLite可快速实现扫码识别相关功能。 14 | 15 | > 简单如斯,你不试试? 16 | 17 | ## 效果展示 18 | ![Image](GIF.gif) 19 | 20 | > 你也可以直接下载 [演示App](https://raw.githubusercontent.com/jenly1314/ZXingLite/master/app/release/app-release.apk) 体验效果 21 | 22 | ## 引入 23 | 24 | ### Gradle: 25 | 26 | 1. 在Project的 **build.gradle** 或 **setting.gradle** 中添加远程仓库 27 | 28 | ```gradle 29 | repositories { 30 | //... 31 | mavenCentral() 32 | } 33 | ``` 34 | 35 | 2. 在Module的 **build.gradle** 中添加依赖项 36 | 37 | ```gradle 38 | implementation 'com.github.jenly1314:zxing-lite:3.2.0' 39 | 40 | ``` 41 | 42 | ### 温馨提示 43 | 44 | #### 关于ZXingLite版本与编译的SDK版本要求 45 | 46 | > 使用 **v3.1.x** 以上版本时,要求 **compileSdkVersion >= 34** 47 | 48 | > 使用 **v3.0.x** 以上版本时,要求 **compileSdkVersion >= 33** 49 | 50 | > 如果 **compileSdkVersion < 33** 请使用 [**v2.x版本**](https://github.com/jenly1314/ZXingLite/tree/2.x/) 51 | 52 | ## 使用 53 | 54 | ### 版本变化说明 55 | 56 | #### 3.x版本的变化 57 | 58 | 从 **2.x** 到 **3.x** 主要变化如下: 59 | 60 | * 2.x版本中的 **CameraScan** 相关核心类被移除了; 61 | > 从3.0.0版本开始改为依赖 [CameraScan](https://github.com/jenly1314/CameraScan);([CameraScan](https://github.com/jenly1314/CameraScan)是一个独立的库,单独进行维护) 62 | 63 | * 2.x版本中的 **ViewfinderView** 被移除了; 64 | > 从3.0.0版本开始改为依赖 [ViewfinderView](https://github.com/jenly1314/ViewfinderView);([ViewfinderView](https://github.com/jenly1314/ViewfinderView)是一个独立的库,单独进行维护) 65 | 66 | * 2.x版本中的 **CaptureActivity** 和 **CaptureFragment** 相关基类被移除了; 67 | > 从3.0.0版本开始改为 **BarcodeCameraActivity** 和 **BarcodeCameraFragment** 68 | 69 | 除了以上几点主要差异变化,3.x版本的整体使用方式和2.x基本类似;3.x版本在2.x版本的基础上再次进行重构,将 **CameraScan** 相关的公共基础类从 **ZXingLite** 中移除后,维护起来更方便了。 70 | 71 | > 如果你是从 **2.x** 版本升级至 **3.x** 版本,那么你需要知道上面所说的主要差异;特别是独立出去单独维护的库,其包名都有所变化,这一点需要特别注意;请谨慎升级。 72 | 73 | > 如果你使用的是2.x版本的话请直接[查看v2.x分支版本](https://github.com/jenly1314/ZXingLite/tree/2.x/) 74 | 75 | #### 3.x版本的使用 76 | 77 | 3.x的实现主要是以 [CameraScan](https://github.com/jenly1314/CameraScan)作为基础库去实现具体的分析检测功能,所以你可以先去看下 [CameraScan](https://github.com/jenly1314/CameraScan)的使用说明;在了解了 [CameraScan](https://github.com/jenly1314/CameraScan)的基本使用方式后,然后再结合当前的使用说明就可以轻松的集成并使用 **ZXingLite** 了。 78 | 79 | ### 主要类说明 80 | 81 | #### 关于Analyzer的实现类 82 | 83 | 内部提供了Analyzer对应的实现,都是为快速实现扫码识别而提供的分析器。 84 | 85 | 内部提供的分析器有多个;一般情况下,你只需要知道最终实现的 [**MultiFormatAnalyzer**](zxing-lite/src/main/java/com/king/zxing/analyze/MultiFormatAnalyzer.java) 和 [**QRCodeAnalyzer**](zxing-lite/src/main/java/com/king/zxing/analyze/QRCodeAnalyzer.java) 即可: 86 | 87 | **MultiFormatAnalyzer** 和 **QRCodeAnalyzer** 的主要区别,从名字大概就能看的出来;一个是可识别多种格式,一个是只识别二维码(具体需要支持识别哪些格式的条码,其实还要看提供的 **DecodeConfig** 是怎么配置的)。 88 | 89 | > 本可以不需要 **QRCodeAnalyzer**,之所以提供一个 **QRCodeAnalyzer** 是因为有很多需求是只需要识别二维码就行;如果你有连续扫码的需求或不知道怎么选时,推荐直接选择 **MultiFormatAnalyzer** 。 90 | 91 | #### 关于DecodeConfig 92 | 93 | DecodeConfig:解码配置;主要用于在扫码识别时,提供一些配置,便于扩展。通过配置可决定内置分析器的能力,从而间接的控制并简化扫码识别的流程。一般在使用 **Analyzer** 的实现类时,你可能会用到。 94 | 95 | #### 关于DecodeFormatManager 96 | 97 | DecodeConfig:解码格式管理器;主要将多种条码格式进行划分与归类,便于提供快捷配置。 98 | 99 | #### 关于CodeUtils 100 | 101 | 工具类 **CodeUtils** 中主要提供;解析条形码/二维码、生成条形码/二维码相关的能力。 102 | 103 | CodeUtils的使用示例 104 | 105 | ```Java 106 | 107 | // 生成二维码 108 | CodeUtils.createQRCode(content,600,logo); 109 | // 生成条形码 110 | CodeUtils.createBarCode(content, BarcodeFormat.CODE_128,800,200); 111 | // 解析条形码/二维码 112 | CodeUtils.parseCode(bitmap); 113 | // 解析二维码 114 | CodeUtils.parseQRCode(bitmap); 115 | ``` 116 | 117 | #### 关于BarcodeCameraScanActivity 118 | 119 | 通过继承BarcodeCameraScanActivity实现扫二维码完整示例 120 | 121 | ```java 122 | public class QRCodeScanActivity extends BarcodeCameraScanActivity { 123 | 124 | @Override 125 | public void initCameraScan(@NonNull CameraScan cameraScan) { 126 | super.initCameraScan(cameraScan); 127 | // 根据需要设置CameraScan相关配置 128 | cameraScan.setPlayBeep(true); 129 | } 130 | 131 | @Nullable 132 | @Override 133 | public Analyzer createAnalyzer() { 134 | // 初始化解码配置 135 | DecodeConfig decodeConfig = new DecodeConfig(); 136 | decodeConfig.setHints(DecodeFormatManager.QR_CODE_HINTS)//如果只有识别二维码的需求,这样设置效率会更高,不设置默认为DecodeFormatManager.DEFAULT_HINTS 137 | .setFullAreaScan(false)//设置是否全区域识别,默认false 138 | .setAreaRectRatio(0.8f)//设置识别区域比例,默认0.8,设置的比例最终会在预览区域裁剪基于此比例的一个矩形进行扫码识别 139 | .setAreaRectVerticalOffset(0)//设置识别区域垂直方向偏移量,默认为0,为0表示居中,可以为负数 140 | .setAreaRectHorizontalOffset(0);//设置识别区域水平方向偏移量,默认为0,为0表示居中,可以为负数 141 | // BarcodeCameraScanActivity默认使用的MultiFormatAnalyzer,如果只识别二维码,这里可以改为使用QRCodeAnalyzer 142 | return new MultiFormatAnalyzer(decodeConfig); 143 | } 144 | 145 | /** 146 | * 布局ID;通过覆写此方法可以自定义布局 147 | * 148 | * @return 布局ID 149 | */ 150 | @Override 151 | public int getLayoutId() { 152 | return R.layout.activity_qrcode_scan; 153 | } 154 | 155 | @Override 156 | public void onScanResultCallback(@NonNull AnalyzeResult result) { 157 | // 停止分析 158 | getCameraScan().setAnalyzeImage(false); 159 | // 返回结果 160 | Intent intent = new Intent(); 161 | intent.putExtra(CameraScan.SCAN_RESULT, result.getResult().getText()); 162 | setResult(Activity.RESULT_OK, intent); 163 | finish(); 164 | } 165 | } 166 | 167 | ``` 168 | 169 | > **BarcodeCameraScanFragment** 的使用方式与之类似。 170 | 171 | 更多使用详情,请查看[app](app)中的源码使用示例或直接查看[API帮助文档](https://jenly1314.github.io/ZXingLite/api/) 172 | 173 | ### 其他 174 | 175 | #### JDK版本与API脱糖 176 | 177 | 当使用ZXingLite为 **v2.3.0 ~ v3.0.1** 之间版本时,(即:使用的zxing为v3.5.1版本时);如果要兼容Android 7.0 (N) 以下版本(即:minSdk<24),可通过脱糖获得 Java 8 及更高版本 API。 178 | 179 | ```gradle 180 | compileOptions { 181 | // Flag to enable support for the new language APIs 182 | coreLibraryDesugaringEnabled true 183 | // Sets Java compatibility to Java 11 184 | targetCompatibility JavaVersion.VERSION_11 185 | sourceCompatibility JavaVersion.VERSION_11 186 | } 187 | 188 | ``` 189 | 190 | ```gradle 191 | dependencies { 192 | coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.3' 193 | } 194 | ``` 195 | 196 | > ZXingLite **v3.1.0** 以后版本(无需脱糖),因为zxing **v3.5.2** 又恢复了与旧版 JDK 的兼容性;详情见:[zxing-3.5.2](https://github.com/zxing/zxing/releases/tag/zxing-3.5.2) 197 | 198 | ## 相关推荐 199 | 200 | - [MLKit](https://github.com/jenly1314/MLKit) 一个强大易用的工具包。通过ML Kit您可以很轻松的实现文字识别、条码识别、图像标记、人脸检测、对象检测等功能。 201 | - [WeChatQRCode](https://github.com/jenly1314/WeChatQRCode) 基于OpenCV开源的微信二维码引擎移植的扫码识别库。 202 | - [CameraScan](https://github.com/jenly1314/CameraScan) 一个简化扫描识别流程的通用基础库。 203 | - [ViewfinderView](https://github.com/jenly1314/ViewfinderView) ViewfinderView一个取景视图:主要用于渲染扫描相关的动画效果。 204 | - [LibYuv](https://github.com/jenly1314/libyuv) 基于Google的libyuv编译封装的YUV转换工具库,主要用途是在各种YUV与RGB之间进行相互转换、裁减、旋转、缩放、镜像等。 205 | - [LogX](https://github.com/jenly1314/LogX) 一个轻量而强大的日志框架;好用不解释。 206 | 207 | 208 | 209 | ## 版本日志 210 | 211 | #### v3.2.0:2024-07-16 212 | * 更新CameraScan至v1.2.0 213 | * 更新ViewfinderView至v1.2.0 214 | * 优化细节 215 | 216 | #### [查看更多版本日志](CHANGELOG.md) 217 | 218 | --- 219 | 220 | ![footer](https://jenly1314.github.io/page/footer.svg) 221 | 222 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'org.jetbrains.kotlin.android' 4 | } 5 | 6 | android { 7 | namespace 'com.king.zxing.app' 8 | compileSdk build_versions.compileSdk 9 | 10 | defaultConfig { 11 | applicationId "com.king.zxing.app" 12 | minSdk build_versions.minSdk 13 | targetSdk build_versions.targetSdk 14 | versionCode app_version.versionCode 15 | versionName app_version.versionName 16 | 17 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 18 | } 19 | 20 | buildTypes { 21 | release { 22 | minifyEnabled false 23 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 24 | } 25 | } 26 | compileOptions { 27 | coreLibraryDesugaringEnabled true 28 | sourceCompatibility JavaVersion.VERSION_1_8 29 | targetCompatibility JavaVersion.VERSION_1_8 30 | } 31 | kotlinOptions { 32 | jvmTarget = JavaVersion.VERSION_1_8.toString() 33 | } 34 | lintOptions { 35 | abortOnError false 36 | } 37 | } 38 | 39 | dependencies { 40 | testImplementation deps.test.junit 41 | androidTestImplementation deps.test.android_ext_junit 42 | androidTestImplementation deps.test.espresso 43 | 44 | implementation deps.androidx.design 45 | implementation deps.androidx.appcompat 46 | implementation deps.androidx.constraintlayout 47 | 48 | coreLibraryDesugaring deps.desugar_jdk 49 | 50 | implementation project(':zxing-lite') 51 | } 52 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/release/app-release.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jenly1314/ZXingLite/c4fb1e12d4ed575b3d41879828ee8a1d17428fe7/app/release/app-release.apk -------------------------------------------------------------------------------- /app/release/output-metadata.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 3, 3 | "artifactType": { 4 | "type": "APK", 5 | "kind": "Directory" 6 | }, 7 | "applicationId": "com.king.zxing.app", 8 | "variantName": "release", 9 | "elements": [ 10 | { 11 | "type": "SINGLE", 12 | "filters": [], 13 | "attributes": [], 14 | "versionCode": 42, 15 | "versionName": "3.2.0", 16 | "outputFile": "app-release.apk" 17 | } 18 | ], 19 | "elementType": "File" 20 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/com/king/zxing/app/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.king.zxing.app; 2 | 3 | import android.content.Context; 4 | 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | 8 | import androidx.test.InstrumentationRegistry; 9 | import androidx.test.runner.AndroidJUnit4; 10 | 11 | import static org.junit.Assert.assertEquals; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getTargetContext(); 24 | 25 | assertEquals("com.king.zxing.app", appContext.getPackageName()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 20 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 34 | 35 | 39 | 40 | 44 | 45 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /app/src/main/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jenly1314/ZXingLite/c4fb1e12d4ed575b3d41879828ee8a1d17428fe7/app/src/main/ic_launcher-web.png -------------------------------------------------------------------------------- /app/src/main/java/com/king/zxing/app/CodeActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 Jenly Yu 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.king.zxing.app; 17 | 18 | import android.graphics.Bitmap; 19 | import android.graphics.BitmapFactory; 20 | import android.os.Bundle; 21 | import android.view.View; 22 | import android.widget.ImageView; 23 | import android.widget.TextView; 24 | 25 | import com.google.zxing.BarcodeFormat; 26 | import com.king.zxing.util.CodeUtils; 27 | 28 | import java.util.concurrent.ExecutorService; 29 | import java.util.concurrent.Executors; 30 | 31 | import androidx.annotation.Nullable; 32 | import androidx.appcompat.app.AppCompatActivity; 33 | 34 | /** 35 | * 生成条形码/二维码示例 36 | * 37 | * @author Jenly 38 | *

39 | * Follow me 40 | */ 41 | public class CodeActivity extends AppCompatActivity { 42 | 43 | private TextView tvTitle; 44 | 45 | private TextView tvBarcodeFormat; 46 | private ImageView ivCode; 47 | 48 | private ExecutorService executor = Executors.newSingleThreadExecutor(); 49 | @Override 50 | protected void onCreate(@Nullable Bundle savedInstanceState) { 51 | super.onCreate(savedInstanceState); 52 | setContentView(R.layout.code_activity); 53 | ivCode = findViewById(R.id.ivCode); 54 | tvTitle = findViewById(R.id.tvTitle); 55 | tvBarcodeFormat = findViewById(R.id.tvBarcodeFormat); 56 | tvTitle.setText(getIntent().getStringExtra(MainActivity.KEY_TITLE)); 57 | boolean isQRCode = getIntent().getBooleanExtra(MainActivity.KEY_IS_QR_CODE,false); 58 | 59 | if(isQRCode){ 60 | tvBarcodeFormat.setText("BarcodeFormat: QR_CODE"); 61 | createQRCode(getString(R.string.app_name)); 62 | }else{ 63 | tvBarcodeFormat.setText("BarcodeFormat: CODE_128"); 64 | createBarCode("1234567890"); 65 | } 66 | } 67 | 68 | /** 69 | * 生成二维码 70 | * @param content 71 | */ 72 | private void createQRCode(String content){ 73 | executor.execute(() -> { 74 | //生成二维码相关放在子线程里面 75 | Bitmap logo = BitmapFactory.decodeResource(getResources(),R.drawable.logo); 76 | Bitmap bitmap = CodeUtils.createQRCode(content,600,logo); 77 | runOnUiThread(()->{ 78 | //显示二维码 79 | ivCode.setImageBitmap(bitmap); 80 | }); 81 | }); 82 | 83 | } 84 | 85 | /** 86 | * 生成条形码 87 | * @param content 88 | */ 89 | private void createBarCode(String content){ 90 | executor.execute(() -> { 91 | //生成条形码相关放在子线程里面 92 | Bitmap bitmap = CodeUtils.createBarCode(content, BarcodeFormat.CODE_128,800,200,null,true); 93 | runOnUiThread(()->{ 94 | //显示条形码 95 | ivCode.setImageBitmap(bitmap); 96 | }); 97 | }); 98 | } 99 | 100 | 101 | public void onClick(View v){ 102 | switch (v.getId()){ 103 | case R.id.ivLeft: 104 | finish(); 105 | break; 106 | } 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /app/src/main/java/com/king/zxing/app/FullScreenQRCodeScanActivity.kt: -------------------------------------------------------------------------------- 1 | package com.king.zxing.app 2 | 3 | import android.app.Activity 4 | import android.content.Intent 5 | import android.widget.Toast 6 | import com.google.zxing.Result 7 | import com.king.camera.scan.AnalyzeResult 8 | import com.king.camera.scan.CameraScan 9 | import com.king.camera.scan.analyze.Analyzer 10 | import com.king.camera.scan.util.PointUtils 11 | import com.king.view.viewfinderview.ViewfinderView.ViewfinderStyle 12 | import com.king.zxing.DecodeConfig 13 | import com.king.zxing.DecodeFormatManager 14 | import com.king.zxing.BarcodeCameraScanActivity 15 | import com.king.zxing.analyze.QRCodeAnalyzer 16 | 17 | /** 18 | * 扫二维码全屏识别示例 19 | * 20 | * @author Jenly 21 | *

22 | * Follow me 23 | */ 24 | class FullScreenQRCodeScanActivity : BarcodeCameraScanActivity() { 25 | 26 | override fun initUI() { 27 | super.initUI() 28 | 29 | // 设置取景框样式 30 | viewfinderView.setViewfinderStyle(ViewfinderStyle.POPULAR) 31 | 32 | } 33 | 34 | 35 | override fun initCameraScan(cameraScan: CameraScan) { 36 | super.initCameraScan(cameraScan) 37 | // 根据需要设置CameraScan相关配置 38 | cameraScan.setPlayBeep(true) 39 | } 40 | 41 | override fun createAnalyzer(): Analyzer? { 42 | // 初始化解码配置 43 | val decodeConfig = DecodeConfig().apply { 44 | // 如果只有识别二维码的需求,这样设置效率会更高,不设置默认为DecodeFormatManager.DEFAULT_HINTS 45 | hints = DecodeFormatManager.QR_CODE_HINTS 46 | // 设置是否全区域识别,默认false 47 | isFullAreaScan = true 48 | } 49 | // BarcodeCameraScanActivity默认使用的MultiFormatAnalyzer,这里可以改为使用QRCodeAnalyzer 50 | return QRCodeAnalyzer(decodeConfig) 51 | } 52 | 53 | /** 54 | * 布局ID;通过覆写此方法可以自定义布局 55 | * 56 | * @return 布局ID 57 | */ 58 | override fun getLayoutId(): Int { 59 | return super.getLayoutId() 60 | } 61 | 62 | override fun onScanResultCallback(result: AnalyzeResult) { 63 | // 停止分析 64 | cameraScan.setAnalyzeImage(false) 65 | // 显示结果点 66 | displayResultPoint(result) 67 | 68 | // 返回结果 69 | val intent = Intent() 70 | intent.putExtra(CameraScan.SCAN_RESULT, result.result.text) 71 | setResult(Activity.RESULT_OK, intent) 72 | finish() 73 | } 74 | 75 | /** 76 | * 显示结果点 77 | */ 78 | private fun displayResultPoint(result: AnalyzeResult) { 79 | var width = result.imageWidth 80 | var height = result.imageHeight 81 | 82 | val resultPoints = result.result.resultPoints 83 | val size = resultPoints.size 84 | if (size > 0) { 85 | var x = 0f 86 | var y = 0f 87 | resultPoints.forEach { 88 | x += it.x 89 | y += it.y 90 | } 91 | var centerX = x / size 92 | var centerY = y / size 93 | //将实际的结果中心点坐标转换成界面预览的坐标 94 | val point = PointUtils.transform( 95 | centerX.toInt(), 96 | centerY.toInt(), 97 | width, 98 | height, 99 | viewfinderView.width, 100 | viewfinderView.height 101 | ) 102 | //显示结果点信息 103 | viewfinderView.showResultPoints(listOf(point)) 104 | } 105 | } 106 | 107 | } -------------------------------------------------------------------------------- /app/src/main/java/com/king/zxing/app/MainActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 Jenly Yu 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.king.zxing.app; 17 | 18 | import android.content.Intent; 19 | import android.graphics.Bitmap; 20 | import android.os.Bundle; 21 | import android.provider.MediaStore; 22 | import android.util.Log; 23 | import android.view.View; 24 | import android.widget.Button; 25 | import android.widget.Toast; 26 | 27 | import androidx.appcompat.app.AppCompatActivity; 28 | import androidx.core.app.ActivityCompat; 29 | import androidx.core.app.ActivityOptionsCompat; 30 | 31 | import com.king.camera.scan.CameraScan; 32 | import com.king.zxing.util.CodeUtils; 33 | 34 | import java.util.concurrent.ExecutorService; 35 | import java.util.concurrent.Executors; 36 | 37 | /** 38 | * 扫码示例 39 | * 40 | * @author Jenly 41 | *

42 | * Follow me 43 | */ 44 | public class MainActivity extends AppCompatActivity { 45 | 46 | private static final String TAG = "MainActivity"; 47 | public static final String KEY_TITLE = "key_title"; 48 | public static final String KEY_IS_QR_CODE = "key_code"; 49 | 50 | public static final int REQUEST_CODE_SCAN = 0x01; 51 | public static final int REQUEST_CODE_PHOTO = 0x02; 52 | 53 | private Toast toast; 54 | 55 | private ExecutorService executor = Executors.newSingleThreadExecutor(); 56 | 57 | @Override 58 | protected void onCreate(Bundle savedInstanceState) { 59 | super.onCreate(savedInstanceState); 60 | setContentView(R.layout.activity_main); 61 | 62 | } 63 | 64 | @Override 65 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 66 | super.onActivityResult(requestCode, resultCode, data); 67 | if (resultCode == RESULT_OK && data != null) { 68 | switch (requestCode) { 69 | case REQUEST_CODE_SCAN: 70 | String result = CameraScan.parseScanResult(data); 71 | showToast(result); 72 | break; 73 | case REQUEST_CODE_PHOTO: 74 | parsePhoto(data); 75 | break; 76 | } 77 | 78 | } 79 | } 80 | 81 | private void showToast(String text) { 82 | if (toast != null) { 83 | toast.cancel(); 84 | } 85 | 86 | toast = Toast.makeText(this, String.valueOf(text), Toast.LENGTH_SHORT); 87 | toast.show(); 88 | } 89 | 90 | private void parsePhoto(Intent data) { 91 | try { 92 | Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData()); 93 | //异步解析 94 | asyncThread(() -> { 95 | final String result = CodeUtils.parseCode(bitmap); 96 | // 如果只需识别二维码,建议使用:parseQRCode;(因为识别的格式越明确,误识别率越低。) 97 | // final String result = CodeUtils.parseQRCode(bitmap); 98 | runOnUiThread(() -> { 99 | Log.d(TAG, "result:" + result); 100 | showToast(result); 101 | }); 102 | 103 | }); 104 | 105 | } catch (Exception e) { 106 | e.printStackTrace(); 107 | } 108 | 109 | } 110 | 111 | 112 | private void asyncThread(Runnable runnable) { 113 | executor.execute(runnable); 114 | } 115 | 116 | /** 117 | * 扫码 118 | * 119 | * @param cls 120 | */ 121 | private void startScan(Class cls) { 122 | ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeCustomAnimation(this, R.anim.in, R.anim.out); 123 | Intent intent = new Intent(this, cls); 124 | ActivityCompat.startActivityForResult(this, intent, REQUEST_CODE_SCAN, optionsCompat.toBundle()); 125 | } 126 | 127 | /** 128 | * 生成二维码/条形码 129 | * 130 | * @param isQRCode 131 | */ 132 | private void startGenerateCodeActivity(boolean isQRCode, String title) { 133 | Intent intent = new Intent(this, CodeActivity.class); 134 | intent.putExtra(KEY_IS_QR_CODE, isQRCode); 135 | intent.putExtra(KEY_TITLE, title); 136 | startActivity(intent); 137 | } 138 | 139 | /** 140 | * 开始选择图片 141 | */ 142 | private void startPickPhoto() { 143 | Intent pickIntent = new Intent(Intent.ACTION_PICK); 144 | pickIntent.setType("image/*"); 145 | startActivityForResult(pickIntent, REQUEST_CODE_PHOTO); 146 | } 147 | 148 | 149 | public void onClick(View v) { 150 | switch (v.getId()) { 151 | case R.id.btnMultiFormat: 152 | startScan(MultiFormatScanActivity.class); 153 | break; 154 | case R.id.btnQRCode: 155 | startScan(QRCodeScanActivity.class); 156 | break; 157 | case R.id.btnFullQRCode: 158 | startScan(FullScreenQRCodeScanActivity.class); 159 | break; 160 | case R.id.btnPickPhoto: 161 | startPickPhoto(); 162 | break; 163 | case R.id.btnGenerateQrCode: 164 | startGenerateCodeActivity(true, ((Button) v).getText().toString()); 165 | break; 166 | case R.id.btnGenerateBarcode: 167 | startGenerateCodeActivity(false, ((Button) v).getText().toString()); 168 | break; 169 | } 170 | 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /app/src/main/java/com/king/zxing/app/MultiFormatScanActivity.kt: -------------------------------------------------------------------------------- 1 | package com.king.zxing.app 2 | 3 | import android.widget.Toast 4 | import com.google.zxing.Result 5 | import com.king.camera.scan.AnalyzeResult 6 | import com.king.camera.scan.CameraScan 7 | import com.king.camera.scan.analyze.Analyzer 8 | import com.king.zxing.DecodeConfig 9 | import com.king.zxing.BarcodeCameraScanActivity 10 | import com.king.zxing.analyze.MultiFormatAnalyzer 11 | 12 | /** 13 | * 连续扫码(识别多种格式)示例 14 | * 15 | * @author Jenly 16 | *

17 | * Follow me 18 | */ 19 | class MultiFormatScanActivity : BarcodeCameraScanActivity() { 20 | 21 | private var toast: Toast? = null 22 | 23 | override fun initCameraScan(cameraScan: CameraScan) { 24 | super.initCameraScan(cameraScan) 25 | // 根据需要设置CameraScan相关配置 26 | cameraScan.setPlayBeep(true) 27 | } 28 | 29 | override fun createAnalyzer(): Analyzer? { 30 | // 初始化解码配置 31 | val decodeConfig = DecodeConfig().apply { 32 | // 设置是否支持扫垂直的条码 33 | isSupportVerticalCode = true 34 | // 设置是否支持识别反色码,黑白颜色反转 35 | isSupportLuminanceInvert = true 36 | } 37 | // 多格式分析器(支持的条码格式主要包含:一维码和二维码) 38 | return MultiFormatAnalyzer(decodeConfig) 39 | } 40 | 41 | /** 42 | * 布局ID;通过覆写此方法可以自定义布局 43 | * 44 | * @return 布局ID 45 | */ 46 | override fun getLayoutId(): Int { 47 | return super.getLayoutId() 48 | } 49 | 50 | override fun onScanResultCallback(result: AnalyzeResult) { 51 | // 停止分析 52 | cameraScan.setAnalyzeImage(false) 53 | // 处理扫码结果相关逻辑(此处弹Toast只是为了演示) 54 | showToast(result.result.text) 55 | // 继续分析 56 | cameraScan.setAnalyzeImage(true) 57 | } 58 | 59 | private fun showToast(text: String) { 60 | toast?.cancel() 61 | toast = Toast.makeText(this, text, Toast.LENGTH_SHORT) 62 | toast?.show() 63 | } 64 | } -------------------------------------------------------------------------------- /app/src/main/java/com/king/zxing/app/QRCodeScanActivity.java: -------------------------------------------------------------------------------- 1 | package com.king.zxing.app; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | 6 | import com.google.zxing.Result; 7 | import com.king.camera.scan.AnalyzeResult; 8 | import com.king.camera.scan.CameraScan; 9 | import com.king.camera.scan.analyze.Analyzer; 10 | import com.king.zxing.DecodeConfig; 11 | import com.king.zxing.DecodeFormatManager; 12 | import com.king.zxing.BarcodeCameraScanActivity; 13 | import com.king.zxing.analyze.MultiFormatAnalyzer; 14 | import com.king.zxing.analyze.QRCodeAnalyzer; 15 | 16 | import androidx.annotation.NonNull; 17 | import androidx.annotation.Nullable; 18 | 19 | /** 20 | * 扫二维码识别示例 21 | * 22 | * @author Jenly 23 | *

24 | * Follow me 25 | */ 26 | public class QRCodeScanActivity extends BarcodeCameraScanActivity { 27 | 28 | @Override 29 | public void initCameraScan(@NonNull CameraScan cameraScan) { 30 | super.initCameraScan(cameraScan); 31 | // 根据需要设置CameraScan相关配置 32 | cameraScan.setPlayBeep(true); 33 | } 34 | 35 | @Nullable 36 | @Override 37 | public Analyzer createAnalyzer() { 38 | //初始化解码配置 39 | DecodeConfig decodeConfig = new DecodeConfig(); 40 | decodeConfig.setHints(DecodeFormatManager.QR_CODE_HINTS)//如果只有识别二维码的需求,这样设置效率会更高,不设置默认为DecodeFormatManager.DEFAULT_HINTS 41 | .setFullAreaScan(false)//设置是否全区域识别,默认false 42 | .setAreaRectRatio(0.8f)//设置识别区域比例,默认0.8,设置的比例最终会在预览区域裁剪基于此比例的一个矩形进行扫码识别 43 | .setAreaRectVerticalOffset(0)//设置识别区域垂直方向偏移量,默认为0,为0表示居中,可以为负数 44 | .setAreaRectHorizontalOffset(0);//设置识别区域水平方向偏移量,默认为0,为0表示居中,可以为负数 45 | // BarcodeCameraScanActivity默认使用的MultiFormatAnalyzer,这里也可以改为使用QRCodeAnalyzer 46 | return new MultiFormatAnalyzer(decodeConfig); 47 | } 48 | 49 | /** 50 | * 布局ID;通过覆写此方法可以自定义布局 51 | * 52 | * @return 布局ID 53 | */ 54 | @Override 55 | public int getLayoutId() { 56 | return R.layout.activity_qrcode_scan; 57 | } 58 | 59 | @Override 60 | public void onScanResultCallback(@NonNull AnalyzeResult result) { 61 | // 停止分析 62 | getCameraScan().setAnalyzeImage(false); 63 | // 返回结果 64 | Intent intent = new Intent(); 65 | intent.putExtra(CameraScan.SCAN_RESULT, result.getResult().getText()); 66 | setResult(Activity.RESULT_OK, intent); 67 | finish(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /app/src/main/res/anim/in.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /app/src/main/res/anim/out.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/btn_back_normal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jenly1314/ZXingLite/c4fb1e12d4ed575b3d41879828ee8a1d17428fe7/app/src/main/res/drawable-xxhdpi/btn_back_normal.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/btn_back_pressed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jenly1314/ZXingLite/c4fb1e12d4ed575b3d41879828ee8a1d17428fe7/app/src/main/res/drawable-xxhdpi/btn_back_pressed.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/btn_none.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jenly1314/ZXingLite/c4fb1e12d4ed575b3d41879828ee8a1d17428fe7/app/src/main/res/drawable-xxhdpi/btn_none.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jenly1314/ZXingLite/c4fb1e12d4ed575b3d41879828ee8a1d17428fe7/app/src/main/res/drawable-xxhdpi/logo.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/btn_back_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 11 | 16 | 21 | 26 | 31 | 36 | 41 | 46 | 51 | 56 | 61 | 66 | 71 | 76 | 81 | 86 | 91 | 96 | 101 | 106 | 111 | 116 | 121 | 126 | 131 | 136 | 141 | 146 | 151 | 156 | 161 | 166 | 171 | 172 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 18 | 28 | 29 | 30 |