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/cordova-plugin-imagepicker

v1.2.3

Published

Cordova Image Picker Plugin

Readme

cordova-plugin-imagepicker

zh-CN en

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

简介

cordova-plugin-imagepicker 是 Harmony Cordova 生态系统中的一款功能强大的图片选择插件,支持从设备相册中批量选择图片、预览图片、限制选择数量及尺寸筛选等功能,适配 Android、iOS 和 OHOS 三平台,为混合式移动应用提供高效的图片选择解决方案。

  • 批量选择:支持一次性选择多张图片,可自定义最大选择数量

  • 图片预览:选择前可预览图片缩略图及原图,提升用户体验

  • 尺寸筛选:可根据图片宽度、高度筛选符合要求的图片

  • 格式支持:兼容 JPG、PNG 等主流图片格式,适配不同拍摄场景

  • 返回信息丰富:返回图片路径、尺寸、大小等完整元数据

支持平台

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

  • iOS:10.0 及以上

  • OHOS:5.0+

下载安装

通过 hcordova CLI 即可快速安装插件,支持从 npm 仓库或 GitCode 仓库获取。

从 npm 安装(推荐)

# 使用 hcordova CLI 安装

# 安装 hcordova 命令化工具
npm install -g hcordova

# 全平台安装插件
hcordova plugin add cordova-plugin-imagepicker

指定平台安装 ohos

仅为 OHOS 平台安装插件:

# 仅安装到 OHOS 平台
hcordova plugin add cordova-plugin-imagepicker --platform ohos

从 GitCode 仓库安装

仅为 OHOS 平台安装插件:

# 仅安装到 OHOS 平台

# 默认分支安装
hcordova plugin add https://gitcode.com/CPF-Cordova/cordova-plugin-imagepicker.git --platform ohos

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

离线安装(本地包)

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

# 下载插件包到本地(示例路径:../Downloads/cordova-plugin-imagepicker )
# 执行离线安装
hcordova plugin add ../Downloads/cordova-plugin-imagepicker  --platform ohos

安装后验证

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

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

卸载

# Cordova CLI 全平台卸载
hcordova plugin remove cordova-plugin-imagepicker 

# 指定平台卸载
hcordova plugin remove cordova-plugin-imagepicker --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) |

使用示例

以下示例基于 OHOS 平台,展示插件核心功能的完整使用流程,包含图片选择、结果处理及错误捕获,可直接复制到项目中使用(需确保页面存在 id 为 imgInfo 的容器用于展示图片)。

// 页面加载完成后执行,确保插件已初始化
document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    console.log("插件初始化完成,可调用图片选择功能");
    // 绑定图片选择按钮点击事件
    document.getElementById("selectImgBtn").addEventListener("click", selectImages);
}

// 批量选择图片函数
function selectImages() {
    // 调用插件核心接口,配置选择参数
    window.imagePicker.getPictures(
        function (results) {
            // 成功回调:处理选择的图片结果
            if (results.length <= 0) {
                alert("未选择任何图片");
                return;
            }
            // 拼接图片标签,展示到页面
            var src = "";
            for (var i = 0; i < results.length; i++) {
                // 拼接图片路径,适配 OHOS 本地路径展示规则
                src += "<img src='https://localhost" + results[i] + "' width='100%' style='margin:10px 0;' />"
            }
            document.getElementById("imgInfo").innerHTML = src;
            // 打印图片元数据(路径、尺寸等,可根据需求扩展)
            console.log("选择的图片路径:", results);
        }, 
        function (error) {
            console.log('图片选择失败: ' + error);
        }, 
        {
            maximumImagesCount: 9, // 最大选择数量,可自定义(1-9)
            width: 800, // 图片宽度限制,单位 px,超出会自动压缩
            height: 800, // 可选,图片高度限制
            quality: 80 // 可选,图片质量(0-100),默认 80
        }
    );
}

示例说明:

  • 需先监听 deviceready 事件,确保 Cordova 环境及插件初始化完成后再调用接口;

  • 页面需添加按钮(id 为 selectImgBtn)和图片展示容器(id 为 imgInfo),用于触发选择和展示结果;

使用说明

本插件适配 OHOS 5.0+、Android 4.4+、iOS 10.0+ 平台,核心用于从设备相册批量选择图片,使用前需了解以下注意事项、配置参数及常见问题,确保功能正常运行。

配置参数(options)

调用 window.imagePicker.getPictures 方法时,第三个参数为配置对象(可选),用于自定义图片选择规则,所有参数均为可选,默认值如下:

| 参数名 | 类型 | 默认值 | 说明 | |---|---|---|---| | maximumImagesCount | Number | 9 | 最大图片选择数量,取值范围 1-9,超出范围会自动取边界值 | | width | Number | 800 | 图片宽度限制,单位 px,图片超出该宽度会自动压缩,不设置则不限制 | | height | Number | 无 | 图片高度限制,单位 px,与 width 同时设置时,按比例压缩图片 | | quality | Number | 80 | 图片质量,取值范围 0-100,值越高质量越好,文件体积越大 | | allowEdit | Boolean | false | 是否允许选择后编辑图片(仅支持简单裁剪) | | outputType | String | file_path | 图片返回格式,可选值:file_path(文件路径,默认)、base64(base64 编码) |

使用注意事项

  • 权限说明:OHOS 无需权限相册权限申请

  • 路径适配:OHOS 平台中,图片本地路径需拼接 https://localhost 前缀才能在页面中正常展示,示例中已包含该处理;

  • 压缩说明:当设置 width/height 或 quality 时,插件会自动对图片进行压缩,避免图片体积过大导致应用卡顿;

  • 兼容性:设置 allowEdit: true,仅支持简单裁剪;

常见问题

  1. 问题:调用接口无响应? 解决:确认已监听 deviceready 事件,插件已成功安装;

  2. 问题:图片无法展示? 解决:检查图片路径是否拼接 https://localhost 前缀,OHOS 平台本地路径需添加该前缀才能正常加载;

  3. 问题:选择图片数量超出设置的 maximumImagesCount? 解决:检查参数取值是否在 1-9 范围内,超出范围会自动取边界值(如设置 10 则按 9 处理);

  4. 问题:图片压缩后质量不佳? 解决:提高 quality 参数值(建议 80-100),同时避免过度限制 width/height。

核心 API

插件通过全局对象 window.imagePicker 暴露所有接口,所有方法均为异步执行,通过成功回调和错误回调处理结果。

选择多张照片

window.imagePicker.getPictures(
    function (results) {
        if (results.length <= 0) {
            return;
        }
        var src = "";
        for (var i = 0; i < results.length; i++) {
            src += "<img src='https://localhost" + results[i] + "' width='100%' />"
        }
        document.getElementById("imgInfo").innerHTML = src;
    }, function (error) {
        console.log('Error: ' + error);
    }, {
        maximumImagesCount: 9,
        width: 800
    }
);

目录结构

cordova-plugin-imagepicker/
├── src/                          # 源代码目录
│   └── main/                     # 主要源代码
│       └── cpp/                  # C++ 原生代码
│           └── ImagePicker/      # 图片选择模块
│               ├── ImagePicker.cpp  # 图片选择功能的 C++ 实现
│               └── ImagePicker.h    # 图片选择功能的头文件
├── www/                          # Web 资源目录
│   └── imagepicker.js            # JavaScript 图片选择接口(Cordova 桥接层)
├── .gitignore                    # Git 忽略文件配置
├── LICENSE                       # 项目开源许可证
├── OAT.xml                       # OpenHarmony 审核配置文件
├── package.json                  # Node.js 包配置
├── plugin.xml                    # Cordova 插件描述文件(核心配置)
└── README.md                     # 项目说明文档

贡献代码

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

许可证

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

官方资源