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-camera-preview

v0.14.1

Published

Cordova camera Plugin

Readme

cordova-plugin-camera-preview

zh-CN en

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

插件简介

cordova-plugin-camera-preview 是一款功能强大的 Cordova 相机预览插件,提供实时相机预览、拍照/录像/压缩等核心能力,在 OHOS 系统中,调用原生相机,原生相机支持闪光灯、前后摄像头切换,焦距调整、美颜、保存到相册功能原生已实现,JS 侧无需手动调用,当前版本 JS 侧主要兼容启动相机、获取照片路径、Uri、压缩功能。

核心优势

  • 全功能相机控制:支持拍照(静态图片)、录像(短视频)、闪光灯开关、焦距调节、曝光补偿

  • 高度可定制:自定义预览区域、裁剪比例、拍照质量、视频分辨率,支持前后摄像头切换

  • 跨平台一致性:统一 Android、iOS 和 OHOS 相机 API 调用,适配不同设备的相机硬件特性

  • 轻量高效:基于原生相机 API 封装,无冗余依赖,预览帧率稳定

  • 灵活交互:支持触摸对焦、双击放大、手势缩放,支持相机预览与页面元素交互

  • 安全可靠:自动处理相机权限申请,提供完善的错误回调和资源释放机制

支持平台

  • 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-camera-preview

指定平台安装 ohos

仅为 OHOS 平台安装插件:

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

从 GitCode 仓库安装

仅为 OHOS 平台安装插件:

# 仅安装到 OHOS 平台
hcordova plugin add https://gitcode.com/CPF-Cordova/cordova-plugin-camera-preview.git --platform ohos

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

安装指定版本

# 仅安装到 OHOS 平台指定版本
hcordova plugin add [email protected] --platform ohos

离线安装(本地包)

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

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

安装后验证

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

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

卸载

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

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

使用示例

以下示例涵盖插件核心功能的完整调用流程,可直接复制到项目中适配使用,需根据实际页面元素 ID 调整相关代码。

示例 1:调用相机拍照返回 URL

配置相机将拍摄的照片存储为文件,拍摄完成后获取文件 URL,可直接用于页面图片展示。

function startCamera1() {
    let options = {
        storeToFile: true, // 设置存储文件
    };

    CameraPreview.startCamera(options, function(){
        CameraPreview.takePicture({quality:80},function(filePath){
            // 获取文件 url,可以直接在 img 中引用
            document.getElementById("imgInfo1").src = filePath;
        });
    },function(){
        document.getElementById("startCamera").innerHTML = "初始化失败";
    });
}

示例 2:调用相机拍照返回 base64 照片数据

配置相机不存储文件,拍摄完成后直接返回 base64 格式照片数据,可直接用于图片渲染。

function startCamera2() {
    let options = {
        storeToFile: false,// 设置返回 base64 数据
    };

    CameraPreview.startCamera(options, function(){
        CameraPreview.takePicture({quality:50}, function(base64PictureData){
            // 直接引用 base64 显示照片
            document.getElementById("imgInfo2").src = 'data:image/jpeg;base64,' +base64PictureData;
        });
    },function(){
        document.getElementById("startCamera").innerHTML = "初始化失败";
    });
}

示例 3:压缩图片获取缩略图

拍摄照片并进行压缩处理,获取低质量缩略图的文件 URL,适用于预览、列表展示等场景。

function startCamera3() {
    let options = {
        storeToFile: true,// 设置存储文件
    };

    CameraPreview.startCamera(options, function(){
        // 设置图片质量参数,获取压缩后的图片
        CameraPreview.takeSnapshot({quality:30}, function(filePath){
            // 获取文件 url,可以直接在 img 中引用
            document.getElementById("imgInfo3").src = filePath;
        });
    },function(){
        document.getElementById("startCamera").innerHTML = "初始化失败";
    });
}

示例 4:打开相机录视频

启动后置相机进行录像,录像完成后获取视频文件路径,结合播放器实现视频播放功能。

// global_play 变量用于全局控制播放器的播放状态;例如:切换页面时,使用此变量暂停播放器播放
let global_play = null;
function startRecordVideo() {
    // 后置摄像头拍摄视频,打开原生相机,摄像头也可以切换
    const opts = {
        cameraDirection: CameraPreview.CAMERA_DIRECTION.BACK
    }

    CameraPreview.startRecordVideo(opts, function(filePath){
        // 返回的路径 key 直接 video 可以直接引用播放
        // Aliplayer 为示例用的第三方阿里云播放器库,需用户自行引入
        let player = new Aliplayer({
            id: "vodId", // 容器 id
            source: filePath,  // 视频 url 支持互联网可直接访问的视频地址
            autoplay: false,      // 自动播放
            width:"100%",       // 播放器宽度
            height:"200px",      // 播放器高度
            showBarTime:"2000",
            showBuffer:true,
            playsinline:true,
            preload:true,
            "controlBarVisibility": "click",
            skinLayout: [{"name":"bigPlayButton","align":"cc","x":0,"y":0},{"name":"H5Loading","align":"cc"},{"align":"blabs","x":0,"y":0,"name":"controlBar", "children":[{"align":"tlabs","x":0, "y":0,"name":"progress"},{"align":"tl","x":15,"y":26,"name":"playButton"},{"align":"tl", "x":10, "y":24, "name":"timeDisplay"},{ "align":"tr", "x":20, "y":25, "name":"fullScreenButton"},{ "align":"tr", "x":20, "y":25, "name":"setButton"},{ "align":"tr", "x":20, "y":23, "name":"streamButton"},{"align":"tr","x":20,"y":25, "name":"volume"}]}]
        });

        player.on("play", function() {
            // 监听播放
            global_play = this;
        });

        player.on("requestFullScreen", function() {
            // 全屏播放监听
        });

        player.on("cancelFullScreen", function() {
            // 取消全屏播放
        });
        player.play();
    });
}

使用说明

本插件在 OHOS 系统中调用原生相机能力,原生已实现闪光灯开关、前后摄像头切换、焦距调整、美颜、保存到相册等功能,JS 侧无需手动调用,仅需关注核心交互逻辑即可。

当前版本 JS 侧主要支持以下核心功能,所有 API 均挂载在全局对象 CameraPreview 下,使用前需确保 Cordova 设备就绪事件(deviceready)已触发,避免 API 调用失败。

  • 相机启动:通过 CameraPreview.startCamera() 方法初始化相机,可配置是否将拍摄内容存储为文件(storeToFile 参数)。

  • 拍照功能:通过 CameraPreview.takePicture() 方法拍摄照片,支持配置照片质量(quality 参数,0-100,数值越高质量越好),可返回文件路径或 base64 数据。

  • 图片压缩:通过 CameraPreview.takeSnapshot() 方法获取压缩后的图片缩略图,通过 quality 参数控制压缩比例,仅支持返回文件路径。

  • 录像功能:通过 CameraPreview.startRecordVideo() 方法启动录像,可配置相机方向(cameraDirection 参数),录像完成后返回视频文件路径,支持直接用于视频播放。

  • 回调机制:所有 API 均支持成功回调和失败回调,失败回调可获取错误信息,便于问题排查;成功回调可获取拍摄/录像后的文件路径、base64 数据等核心信息。

新增特性

该插件当前在 OHOS 平台的实现是基于系统相机实现的功能,对于闪光灯、焦距调整等接口基于原生实现,未提供 JS 侧接口。

目录结构

cordova-plugin-camera-preview          # [根目录] 相机预览插件项目根目录
├── src                                # [源码目录] 存放原生平台代码
│   └── main                           # [主目录] 主代码目录
│       ├── cpp                        # [C++ 目录] C++ 原生代码目录
│       │   └── camera                 # [C++ 模块] 相机核心逻辑文件夹
│       │       ├── CameraPreview.cpp  # [C++ 实现] 相机预览功能的 C++ 逻辑实现(打开相机、处理帧数据)
│       │       └── CameraPreview.h    # [C++ 声明] 相机预览功能的 C++ 头文件
│       └── ets                        # [ArkTS 目录] OHOS ArkTS/ETS 代码目录
│           └── components             # [组件目录] 存放 UI 组件
│               └── ImageCompress      # [TS 模块] 图像压缩相关组件文件夹(虽然路径叫 ImageCompress,但文件名为 CameraAction)
│                   └── CameraAction.ets # [ETS 文件] 处理相机动作(如拍照、切换镜头)的 OHOS 组件
├── www                                # [前端目录] 存放 JavaScript 接口文件
│   ├── CameraPreview.js               # [JS 文件] 暴露给前端调用的 JavaScript 接口,负责与原生层通信
│   └── LICENSE                        # [文本] 前端库接口的许可证文件
├── .gitignore                         # [配置] Git 版本控制忽略文件配置
├── LICENSE                            # [文本] 项目根目录的开源许可证文件
├── OAT.xml                            # [配置] OHOS 应用配置文件
├── package.json                       # [配置] NPM 包配置文件
├── plugin.xml                         # [配置] Cordova 插件核心配置文件
└── README.md                          # [文档] 项目说明文档

贡献代码

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

许可证

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

官方资源

OHOS Cordova:https://gitcode.com/CPF-Cordova/cordova-plugin-camera-preview

Android/iOS:https://npmjs.com/cordova-plugin-camera-preview