@capacitor/camera
This project is developed based on @capacitor/camera@8.0.0.
Introduction
@capacitor/camera is a core plugin in the Capacitor ecosystem. It provides camera capture and photo selection from the album, and also supports permission checks and image parameter configuration. It provides device-specific adaptation capability for cross-platform app development. The plugin is compatible with Android, iOS, and browser environments. This document only describes usage in the OpenHarmony environment.
The API can: take photos with the camera, or select existing photos from the album.
Supported platforms
- OpenHarmony: 5.0+
Download and installation
Install quickly via command line or manual integration. The package is available from npm.
Command-line installation (recommended)
Install hionic CLI:
npm install -g hionic
Choose one of the following two methods; do not repeat operations:
npm install:
# Install plugin
npm install @capacitor/camera
# Sync plugin
hionic sync
hionic CLI install:
hionic plugin add @capacitor/camera
Manual integration
Integrate the plugin according to plugin.xml in the plugin source:
1. Add plugin configuration
According to the config-json item in plugin.xml, find capacitor.plugins.json in the entry module through the target field, then add config according to param:
{
"pkg": "@capacitor/camera",
"classpath": "Camera"
}
2. Update CMake configuration
According to the CMakeLists item in plugin.xml, find module capacitor through modules-name, then locate CMakeLists.txt by the target path. Add add_subdirectory and target_link_libraries according to the param tag:
#START_ADD_SUBDIRECTORY
// ...
add_subdirectory(Camera)
// ...
#END_ADD_SUBDIRECTORY
// ...
target_link_libraries(capacitor PUBLIC
"-Wl,--whole-archive"
// ...
Camera
// ...
"-Wl,--no-whole-archive"
)
3. Copy source files
According to source-file items in plugin.xml, find files from src, and locate destination module and directory through modules-name and target-dir:
Copy Camera.h, Camera.cpp, and CMakeLists.txt from src/main/cpp/Camera into src/main/cpp/Camera of the capacitor module.
Copy CameraAction.ets, ErrorCode.ets, ExifInfo.ets, ImageCompressEx.ets, and ImageInfo.ets from src/main/ets/components/ImageCompress into src/main/ets/components/ImageCompress of the capacitor module.
Copy CameraDefine.ets, CameraSelectDialog.ets, CheckCameraPermission.ets, and MediaAction.ets from src/main/ets/components/MediaAction into src/main/ets/components/MediaAction of the capacitor module.
4. Add ArkTS configuration
In build-profile.json5 of the capacitor module, add copied .ets file paths from step 3 to buildOption/arkOptions/runtimeOnly/sources:
{
"buildOption": {
"arkOptions": {
"runtimeOnly": {
"sources": [
"./src/main/ets/components/ImageCompress/CameraAction.ets",
"./src/main/ets/components/MediaAction/MediaAction.ets",
"./src/main/ets/components/MediaAction/CameraSelectDialog.ets",
"./src/main/ets/components/MediaAction/CheckCameraPermission.ets"
]
}
}
}
}
Uninstallation
# Uninstall camera plugin
hionic plugin remove @capacitor/camera
Constraints and limitations
Compatibility
Tested on:
- capacitor: 8.0.0-ohos-8.0.0; SDK: 5.0.5(17); IDE: DevEco Studio: 6.0.0; ROM: 5.1.0.150;
Permission requirements
For OpenHarmony permissions, refer to Request app permissions.
To use camera capture, add camera permission in the requestPermissions array of module.json5 in your main project:
{
"name": "ohos.permission.CAMERA",
"reason": "$string:camera",
"usedScene": {
"abilities": [
"EntryAbility"
],
"when": "always"
}
}
Usage examples
Example 1: Take a photo or select a single photo from album
This example demonstrates calling the camera API to show a selection dialog. Users can choose camera capture or pick one image from album, configure image quality, and return in URI format for direct rendering.
import { Camera, CameraResultType } from '@capacitor/camera';
const takePicture = async () => {
const image = await Camera.getPhoto({
quality: 90,
allowEditing: true,
resultType: CameraResultType.Uri
});
// image.webPath contains a path that can be used as image src.
// You can use image.path to access the original file and pass it to Filesystem API if needed,
// (or pass resultType: CameraResultType.Base64 to getPhoto)
var imageUrl = image.webPath;
// Set it as image src
imageElement.src = imageUrl;
};
Usage notes
Import and use the Camera module from @capacitor/camera. All APIs are Promise-based and support async calls. It is recommended to check and request permissions before use.
1. Get a single photo
Shows a picker allowing users to choose one photo from album or take a new photo with camera. Supports quality, size, and return format options.
Method signature
getPhoto(options: ImageOptions): Promise<Photo>
ImageOptions
| Parameter | Type | Description | Default |
|---|---|---|---|
quality |
number |
JPEG quality from 0 to 100 | |
allowEditing |
boolean |
Whether to allow crop or minor edit (platform-specific). On iOS, only supports CameraSource.Camera, not CameraSource.Photos. | |
resultType |
CameraResultType | Return format. Currently supports only 'Base64', 'DataUrl', or 'Uri' | |
saveToGallery |
boolean |
Temporarily unsupported | : false |
width |
number |
Maximum width of saved image, keeping aspect ratio | |
height |
number |
Maximum expected height of saved image, keeping aspect ratio | |
correctOrientation |
boolean |
Auto-rotate image upright to correct portrait orientation | : true |
source |
CameraSource | Photo source. PROMPT asks user to choose album or camera |
: CameraSource.Prompt |
direction |
CameraDirection | Camera direction | : CameraDirection.Rear |
presentationStyle |
'fullscreen' |
Camera style, full-screen by default | : 'fullscreen' |
webUseInput |
boolean |
Whether to use PWA element UX or file input, temporarily unsupported | |
promptLabelHeader |
string |
Text for PROMPT option header |
: 'Photo' |
promptLabelCancel |
string |
Text for PROMPT cancel button |
: 'Cancel' |
promptLabelPhoto |
string |
Text for PROMPT pick saved photo button |
: 'From Photos' |
promptLabelPicture |
string |
Text for PROMPT open camera button |
: 'Take Picture' |
Photo
| Parameter | Type | Description |
|---|---|---|
base64String |
string |
Base64-encoded image string when using CameraResultType.Base64 |
dataUrl |
string |
A URL starting with data:image/jpeg;base64,. When using CameraResultType.DataUrl, this is the base64 encoded image string. Note: on web, file format may differ by browser. |
path |
string |
When using CameraResultType.Uri, includes full platform-specific file URL for later reading via Filesystem API. |
webPath |
string |
Returns a path that can be used as image src for efficient loading and rendering. |
exif |
any |
EXIF metadata retrieved from the image, if available |
format |
string |
Image format, e.g. jpeg/png/gif. iOS and Android support jpeg only. Web supports jpeg/png/gif (browser dependent). GIF is supported only when webUseInput is true and source is Photos. |
saved |
boolean |
Whether image was saved to gallery. On Android/iOS save may fail without required permissions. On web there is no gallery, so always false. |
2. Select multiple images from album
Opens system album, allows selecting multiple images, and supports quality/size/max count options.
Method signature
pickImages(options: GalleryImageOptions): Promise<GalleryPhotos>
GalleryImageOptions
| Parameter | Type | Description | Default |
|---|---|---|---|
quality |
number |
JPEG image quality from 0 to 100 | |
width |
number |
Maximum width of saved image, keeping aspect ratio | |
height |
number |
Maximum expected height of saved image, keeping aspect ratio | |
correctOrientation |
boolean |
Auto-rotate image upright to correct portrait orientation | :true |
presentationStyle |
'fullscreen' |
Camera style, full-screen by default | : 'fullscreen' |
limit |
number |
Maximum number of images user can select | 0 (unlimited) |
GalleryPhotos
| Parameter | Type | Description |
|---|---|---|
photos |
GalleryPhoto[] | Selected photo list |
GalleryPhoto
| Parameter | Type | Description |
|---|---|---|
path |
string |
Full platform-specific file URL, readable via Filesystem API. |
webPath |
string |
Returns a path for setting image src for efficient loading and rendering. |
exif |
any |
EXIF data retrieved from the image, if available |
format |
string |
Image format, e.g. jpeg/png/gif. iOS and Android support jpeg only. Web supports jpeg/png/gif. |
3. Check permission status
Checks current grant status of camera and photos permissions.
Method signature
checkPermissions(): Promise<PermissionStatus>
4. Request permissions
Actively request camera and photo permissions from the system. Capture/select features can be used only after permissions are granted.
Method signature
requestPermissions(permissions?: CameraPluginPermissions): Promise<PermissionStatus>
CameraPluginPermissions
| Parameter | Type |
|---|---|
permissions |
CameraPermissionType[] |
CameraPermissionType
'camera' | 'photos'
5. Methods not supported yet
pickLimitedLibraryPhotos(): select a single photo from limited library, not supported yetgetLimitedLibraryPhotos(): select multiple photos from limited library, not supported yet
Data type definitions
PermissionStatus
| Parameter | Type |
|---|---|
| camera | CameraPermissionState |
| photos | CameraPermissionState |
Type aliases
CameraPermissionState
PermissionState | 'limited'
PermissionState
'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'
Enums
CameraResultType
| Member | Value |
|---|---|
| Uri | 'uri' |
| Base64 | 'base64' |
| DataUrl | 'dataUrl' |
CameraSource
| Member | Value | Description |
|---|---|---|
| Prompt | 'PROMPT' | Prompt to choose source |
| Camera | 'CAMERA' | Capture from camera |
| Photos | 'PHOTOS' | Pick from photos |
CameraDirection
| Member | Value |
|---|---|
| Rear | 'REAR' |
| Front | 'FRONT' |
Directory structure
|---- root
| |---- src/main # Plugin implementation code
| |---- cpp # C++ code
| |---- ets # ArkTS code
| |---- README.md # Documentation
| |---- package.json # Configuration file
| |---- plugin.xml # Plugin config file
Contributing
If you find any issues, feel free to submit an Issue. PRs are also very welcome: PR.
License
This plugin is open-sourced under the MIT License. See LICENSE for details.