Skip to content

feat(device): implement scanCode API with CameraX/ML Kit/ZXing support#244

Open
jovkchan wants to merge 1 commit into
didi:mainfrom
jovkchan:feat/scan-code
Open

feat(device): implement scanCode API with CameraX/ML Kit/ZXing support#244
jovkchan wants to merge 1 commit into
didi:mainfrom
jovkchan:feat/scan-code

Conversation

@jovkchan

@jovkchan jovkchan commented Jun 3, 2026

Copy link
Copy Markdown

feat(device): 实现 scanCode 扫码 API,集成 CameraX / ML Kit / ZXing
新增 ScanCodeActivity,基于 Jetpack Compose 构建扫码界面(相机预览、扫描框覆盖层、结果面板)
新增 ScanCodeHandler,统一管理相机权限申请和 Activity 结果回调
在 ScanApi 中注册 API,支持前端传入自定义 UI 参数(标题、颜色、文案等)
集成 CameraX 实现相机预览,Android 9+ 使用 ML Kit、低于 Android 9 使用 ZXing 进行条码解码
支持非连续模式(单次扫码 + 确认)和连续模式(去重 + 批量返回)
支持方形、圆形、SVG 自定义三种扫描框形状
在 AndroidManifest.xml 中补充 CAMERA 权限、uses-feature 声明和 Activity 注册
添加 CameraX、ML Kit Barcode Scanning、ZXing 三方依赖
fix(core): 修复 JSSDK 完整性校验并优化 WebView 资源加载

JSSDK 本地文件缺失时,即使版本号未变更也触发重新解压,修复文件丢失后无法恢复的问题
在 WebViewCacheManager 中显式设置 .js 文件的 MIME 类型为 text/javascript,解决部分设备 MimeTypeMap 不完整导致 ES Module 加载失败
在 shouldInterceptRequest 中增加兜底逻辑:WebViewAssetLoader 加载失败时,直接从本地存储提供资源
第一次提交PR,请 甄 别。

- Add ScanCodeActivity with Jetpack Compose UI (camera preview, scan frame overlay, result panel)
- Add ScanCodeHandler for camera permission and Activity result management
- Register scanCode API in ScanApi with support for UI customization params
- Integrate CameraX for camera preview, ML Kit (Android 9+) and ZXing (<9) for barcode decoding
- Support non-continuous (single scan + confirm) and continuous (dedup + batch return) modes
- Support square/circle/SVG custom scan frame shapes
- Add CAMERA permission, uses-feature, and Activity declaration to AndroidManifest
- Add CameraX, ML Kit Barcode, ZXing dependencies

fix(core): ensure JSSDK integrity and improve WebView resource loading
- Re-extract JSSDK if local files are missing even when version matches
- Set text/javascript MIME type for .js files in WebViewCacheManager
- Fallback to local storage if WebViewAssetLoader fails
config = uiConfig,
) { success, data ->
data.put("errMsg", if (success) "scanCode:ok" else data.optString("errMsg", "scanCode:fail"))
responseCallback(data.toString())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要参考其他 api 实现,走 ApiUtils.invokeSuccess/Fail/Complete

val oldVersionCode = VersionUtils.getJSVersion()
if (newVersionCode > oldVersionCode) {
// 检查本地存储的 JSSDK 文件是否实际存在
val jsSdkDir = File(context.filesDir, "jssdk/$oldVersionCode/main")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

发布模式下 App 版本没变时,即使 files/jssdk//main/pageFrame.html 被删,也会直接走 “Skipping JSSDK update check”,建议把文件存在性检查移到外层,或把 !jsSdkFilesExist 纳入外层 shouldCheckJssdk 条件。

@dos1in

dos1in commented Jun 3, 2026

Copy link
Copy Markdown
Member
image

扫码类型按微信标准,除了 wxCode。参照: wx.scanCode

另外需要根据 scanType 初始化扫码解析器,而不是全量初始化。

@jovkchan

jovkchan commented Jun 3, 2026

Copy link
Copy Markdown
Author
image 扫码类型按微信标准,除了 wxCode。参照: [wx.scanCode](https://developers.weixin.qq.com/miniprogram/dev/api/device/scan/wx.scanCode.html#%E5%8F%82%E6%95%B0)。

另外需要根据 scanType 初始化扫码解析器,而不是全量初始化。

好的,我试着弄下。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants