npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@cordova-ohos/phonegap-plugin-barcodescanner

v8.1.1

Published

Cordova Barcodescanner Plugin

Readme

phonegap-plugin-barcodescanner

zh-CN en

本项目基于 [email protected] 开发,本文档重点阐述其在 OpenHarmony(OHOS)系统中的具体应用。

简介

phonegap-plugin-barcodescanner 是一款专为 Cordova/PhoneGap 混合移动应用打造的条码与二维码扫描插件,基于原生平台扫描能力开发,支持多码制识别、实时预览、闪光灯控制及相册图片解析等核心功能。具备识别速度快、准确率高、兼容性强、轻量化等特点,可广泛应用于商品溯源、移动支付、资产管理、票务验证等场景。

功能特性

  • 多码制全面支持:覆盖常见一维码(如 EAN-13、UPC-A、Code 128)和二维码(如 QR Code、Data Matrix、Aztec),支持自定义启用/禁用特定码制

  • 原生扫描引擎:基于 OHOS 原生扫描能力开发,识别速度 ≤300ms,准确率 ≥98%(清晰条码场景)

  • 实时预览与对焦:支持高清实时预览(最高 1080P),支持自动对焦、手动点击对焦及连续对焦模式,适配不同距离扫描需求

  • 辅助扫描功能:集成闪光灯控制(明暗环境自适应)、扫描框引导(提高识别效率)、震动/声音反馈(扫描成功提示)

  • 相册图片解析:支持从相册选择图片或传入图片路径,解析图片中的条码信息,适配多种图片格式(JPG、PNG、GIF 等)

  • 权限智能管理:自动检测并请求相机、相册权限,提供权限申请结果回调,支持权限拒绝后的引导处理

  • 离线全功能支持:所有扫描与解析功能均本地完成,无需依赖网络,满足无网环境使用需求

支持平台

  • Android:API 19 及以上(Android 4.4+)

  • iOS:10.0 及以上

  • OHOS:5.0+

前置准备

使用插件前需确保已创建 Cordova/PhoneGap 项目,若未创建,可执行以下命令创建:

# 方式1:使用 cordova 创建
cordova create barcodeApp com.example.barcodeapp BarcodeApp

# 方式2:使用 phonegap 创建
phonegap create barcodeApp

进入项目根目录后,即可进行插件的安装操作。

下载安装

从 npm 安装(推荐)

适用于大多数场景,直接从 npm 仓库安装稳定版,支持指定版本及 OHOS 平台安装:

# 安装 hcordova
npm install -g hcordova

# 安装最新稳定版扫描插件
hcordova plugin add phonegap-plugin-barcodescanner

# 安装指定 OHOS 平台
hcordova plugin add phonegap-plugin-barcodescanner --platform ohos

# 安装指定版本(示例:1.0.0 版本,OHOS 平台)
hcordova plugin add [email protected] --platform ohos

从 GitCode 仓库安装

适用于需要体验最新功能的开发者,从 GitCode 仓库直接安装开发版:

# 安装开发版插件(OHOS 平台)
hcordova plugin add https://gitcode.com/CPF-Cordova/phonegap-plugin-barcodescanner.git --platform ohos

# 指定标签/分支安装
hcordova plugin add https://gitcode.com/CPF-Cordova/phonegap-plugin-barcodescanner.git@develop --platform ohos

离线安装(本地包)

适用于无网络环境,先下载插件包到本地,再执行离线安装:

# 下载插件包到本地(示例路径:~/Downloads/phonegap-plugin-barcodescanner)
# 执行离线安装
hcordova plugin add ~/Downloads/phonegap-plugin-barcodescanner

安装后验证

安装完成后,可通过以下命令验证插件是否成功添加到项目中:

# 查看已安装的插件列表,若包含本插件 ID 则表示插件已成功安装
hcordova plugin list

卸载

进入项目根目录,执行以下命令卸载插件,卸载后建议重新构建项目以清理残留文件:

# 卸载扫描插件(全平台)
hcordova plugin remove phonegap-plugin-barcodescanner

# 指定 OpenHarmony 平台卸载
hcordova plugin remove phonegap-plugin-barcodescanner --platform ohos

约束与限制

兼容性

支持:

| 项目 | 版本/信息 | |-----|--------| | SDK | API12+ | | IDE | DevEco Studio: 5.0+ | | ROM | 5.1+ | | Emulator | OpenHarmony 6.0+ |

在以下版本中已测试通过:

| 项目 | 版本/信息 | |-----|--------| | @cordova-ohos/ohos | 14.0.1-ohos-14.0.1 | | SDK | 5.0.0(12) | | IDE | DevEco Studio: 6.0.13.200 | | ROM | 5.1.0.120 SP3 | | Emulator | OpenHarmony 6.0.1(21) |

使用示例

以下示例展示核心 API 的使用方法,涵盖实时扫描、结果处理等常见场景:

// 实时扫描条码/二维码示例
cordova.plugins.barcodeScanner.scan(
    function (result) {
        // 扫描取消时直接返回
        if(result.cancelled) {
            console.log("扫描已取消");
            return;
        }
        // 示例:仅允许识别 QR_CODE 二维码
        if(result.format !== "QR_CODE") {
            alert("请扫描正确的二维码");
            return;
        }
        // 扫描成功,处理返回结果
        console.log("扫描成功,条码内容:", result.text);
        console.log("条码格式:", result.format);
    },
    function (error) {
        // 扫描失败,打印错误信息
        console.log("Scanning failed: " + error);
    }
);

使用说明

核心概念

1. 插件命名空间

插件通过全局对象 cordova.plugins.barcodeScanner 暴露所有 API 方法,所有操作均需通过该命名空间调用。

2. 扫描模式

插件支持两种核心扫描模式,可根据业务场景选择:

  • 实时扫描模式:打开相机实时预览并识别条码,适用于直接扫描实体条码的场景(如商品扫码、票务验证),仅支持单次扫描

  • 图片解析模式:解析相册图片文件中的条码信息,适用于扫描已保存的条码图片场景(如相册中的电子票、截图中的条码)

3. 对焦模式

为适配不同扫描距离和场景,插件支持三种对焦模式:

  • 自动对焦:相机根据画面清晰度自动调整对焦,适用于扫描距离不固定的场景(默认模式)

  • 手动对焦:通过点击预览界面指定对焦区域,适用于条码较小或位于画面特定位置的场景

  • 连续对焦:相机持续保持对焦状态,适用于连续扫描多个条码的场景(如批量扫码入库)

API 文档

插件通过全局对象 cordova.plugins.barcodeScanner 暴露所有 API 方法,所有操作均为异步执行,通过回调函数处理结果。具体使用示例见「使用示例」章节。

实时扫描(核心 API)

/**
* 打开相机实时扫描条码/二维码
* @param {Function} successCallback - 成功回调(参数:result 扫描结果对象)
* @param {Function} errorCallback - 失败回调(参数:error 错误信息)
* @param {Object} options - 可选配置(可选)
* @description 扫描结果对象 result 属性:
* - text: string 条码内容
* - format: string 条码格式(如 QR_CODE、EAN_13)
* - cancelled: boolean 是否取消扫描(true 为取消,false 为成功)
*/
cordova.plugins.barcodeScanner.scan(
    successCallback,
    errorCallback,
    options
);

目录结构

phonegap-plugin-barcodescanner         # [根目录] 条形码/二维码扫描插件项目根目录
├── src                                # [源码目录] 存放原生平台代码
│   └── main                           # [主目录] 主代码目录
│       ├── cpp                        # [C++ 目录] C++ 原生代码目录
│       │   └── BarcodeScanner         # [C++ 模块] 扫描器核心逻辑文件夹
│       │       ├── BarcodeScanner.cpp # [C++ 实现] 扫描功能的 C++ 逻辑实现
│       │       └── BarcodeScanner.h   # [C++ 声明] 扫描功能的 C++ 头文件
│       └── ets                        # [ArkTS 目录] ArkTS/ETS 代码目录
│           └── components             # [组件目录] 存放 UI 组件
│               └── BarcodeScan        # [TS 模块] 扫描相关的 UI 组件文件夹
│                   └── BarcodeScan.ets # [ETS 文件] 扫描界面的 UI 实现(OHOS 系统的声明式 UI)
├── www                                # [前端目录] 存放 JavaScript 接口文件
│   └── barcodescanner.js              # [JS 文件] 提供给前端调用的 JavaScript 接口,负责桥接 Web 层与原生层
├── .gitignore                         # [配置] Git 版本控制忽略文件配置
├── LICENSE                            # [文本] 开源许可证文件
├── OAT.xml                            # [配置] 门禁配置文件
├── package.json                       # [配置] NPM 包配置文件,定义依赖和脚本
├── plugin.xml                         # [配置] Cordova 插件核心配置文件
└── README.md                          # [文档] 项目说明文档

贡献代码

使用过程中发现任何问题都可以提 Issue ,当然也非常欢迎发 PR 共建。

许可证

本插件基于 Apache License 2.0 开源,详见 LICENSE 文件。

官方资源