メインコンテンツまでスキップ
バージョン: v8

@capacitor/camera

カメラAPIは、カメラで写真を撮影したり、フォトアルバムから既存の写真を選択する機能を提供します。

Install

npm install @capacitor/camera
npx cap sync

iOS

iOSでは、Info.plistにアプリ用の以下の使用説明を追加して記入する必要があります:

  • NSCameraUsageDescriptionPrivacy - Camera Usage Description
  • NSPhotoLibraryAddUsageDescriptionPrivacy - Photo Library Additions Usage Description
  • NSPhotoLibraryUsageDescriptionPrivacy - Photo Library Usage Description

XcodeでのiOSパーミッションの設定については、iOSガイドInfo.plistの設定を参照してください。

Android

デバイスギャラリーから既存の画像を選択する際、AndroidフォトピッカーコンポーネントがAndroidでは使用されるようになりました。フォトピッカーは以下の条件を満たすデバイスで利用できます:

  • Android 11(APIレベル30)以上を実行している
  • Google Systemアップデートを通じてモジュラーシステムコンポーネントの変更を受信している

Android 11または12を実行しているGoogle Playサービスをサポートする古いデバイスやAndroid Goデバイスでは、バックポートされたフォトピッカーをインストールできます。Google Playサービスを通じてバックポートされたフォトピッカーモジュールの自動インストールを有効にするには、AndroidManifest.xmlファイルの<application>タグに以下のエントリを追加します:

<!-- Trigger Google Play services to install the backported photo picker module. -->
<!--suppress AndroidDomInspection -->
<service android:name="com.google.android.gms.metadata.ModuleDependencies"
android:enabled="false"
android:exported="false"
tools:ignore="MissingClass">
<intent-filter>
<action android:name="com.google.android.gms.metadata.MODULE_DEPENDENCIES" />
</intent-filter>
<meta-data android:name="photopicker_activity:0:required" android:value="" />
</service>

そのエントリが追加されていない場合、フォトピッカーをサポートしていないデバイスでは、フォトピッカーコンポーネントはIntent.ACTION_OPEN_DOCUMENTにフォールバックします。

Cameraプラグインは、saveToGallery: trueを使用しない限りパーミッションは不要です。その場合、以下のパーミッションをAndroidManifest.xmlに追加する必要があります:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

これらのパーミッションは、リクエストされるAndroidバージョンに対してのみ指定することもできます:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="29"/>

ストレージパーミッションは、写真ファイルの読み取り/保存のためのものです。

Androidパーミッションの設定の詳細については、Androidガイドパーミッションの設定を参照してください。

また、Camera APIは写真を撮影するために別のActivityを起動するため、Activityの実行中にOSによってアプリが終了された場合に送信されたカメラデータを処理するために、AppプラグインのappRestoredResultをリッスンする必要があります。

Variables

このプラグインは以下のプロジェクト変数(アプリの variables.gradle ファイルで定義)を使用します:

  • androidxExifInterfaceVersion: androidx.exifinterface:exifinterface のバージョン(デフォルト: 1.4.1
  • androidxMaterialVersion: com.google.android.material:material のバージョン(デフォルト: 1.13.0

PWAに関する注意事項

Cameraプラグインが動作するにはPWA Elementsが必要です。

Example

import { Camera, CameraResultType } from '@capacitor/camera';

const takePicture = async () => {
const image = await Camera.getPhoto({
quality: 90,
allowEditing: true,
resultType: CameraResultType.Uri
});

// image.webPath will contain a path that can be set as an image src.
// You can access the original file using image.path, which can be
// passed to the Filesystem API to read the raw data of the image,
// if desired (or pass resultType: CameraResultType.Base64 to getPhoto)
var imageUrl = image.webPath;

// Can be set to the src of an image now
imageElement.src = imageUrl;
};

API

getPhoto(...)

getPhoto(options: ImageOptions) => Promise<Photo>

Prompt the user to pick a photo from an album, or take a new photo with the camera.

ParamType
options
ImageOptions

Returns:

Promise<Photo>

Since: 1.0.0


pickImages(...)

pickImages(options: GalleryImageOptions) => Promise<GalleryPhotos>

Allows the user to pick multiple pictures from the photo gallery.

ParamType
options
GalleryImageOptions

Returns:

Promise<GalleryPhotos>

Since: 1.2.0


pickLimitedLibraryPhotos()

pickLimitedLibraryPhotos() => Promise<GalleryPhotos>

Allows the user to update their limited photo library selection. Returns all the limited photos after the picker dismissal. If instead the user gave full access to the photos it returns an empty array.

Returns:

Promise<GalleryPhotos>

Since: 4.1.0


getLimitedLibraryPhotos()

getLimitedLibraryPhotos() => Promise<GalleryPhotos>

Return an array of photos selected from the limited photo library.

Returns:

Promise<GalleryPhotos>

Since: 4.1.0


checkPermissions()

checkPermissions() => Promise<PermissionStatus>

Check camera and photo album permissions

Returns:

Promise<PermissionStatus>

Since: 1.0.0


requestPermissions(...)

requestPermissions(permissions?: CameraPluginPermissions | undefined) => Promise<PermissionStatus>

Request camera and photo album permissions

ParamType
permissions
CameraPluginPermissions

Returns:

Promise<PermissionStatus>

Since: 1.0.0


Interfaces

Photo

PropTypeDescriptionSince
base64StringstringThe base64 encoded string representation of the image, if using CameraResultType.Base64.1.0.0
dataUrlstringThe url starting with 'data:image/jpeg;base64,' and the base64 encoded string representation of the image, if using CameraResultType.DataUrl. Note: On web, the file format could change depending on the browser.1.0.0
pathstringIf using CameraResultType.Uri, the path will contain a full, platform-specific file URL that can be read later using the Filesystem API.1.0.0
webPathstringwebPath returns a path that can be used to set the src attribute of an image for efficient loading and rendering.1.0.0
exifanyExif data, if any, retrieved from the image1.0.0
formatstringThe format of the image, ex: jpeg, png, gif. iOS and Android only support jpeg. Web supports jpeg, png and gif, but the exact availability may vary depending on the browser. gif is only supported if webUseInput is set to true or if source is set to Photos.1.0.0
savedbooleanWhether if the image was saved to the gallery or not. On Android and iOS, saving to the gallery can fail if the user didn't grant the required permissions. On Web there is no gallery, so always returns false.1.1.0

ImageOptions

PropTypeDescriptionDefaultSince
qualitynumberThe quality of image to return as JPEG, from 0-100 Note: This option is only supported on Android and iOS1.0.0
allowEditingbooleanWhether to allow the user to crop or make small edits (platform specific). On iOS it's only supported for CameraSource.Camera, but not for CameraSource.Photos.1.0.0
resultType
CameraResultType
How the data should be returned. Currently, only 'Base64', 'DataUrl' or 'Uri' is supported1.0.0
saveToGallerybooleanWhether to save the photo to the gallery. If the photo was picked from the gallery, it will only be saved if edited.: false1.0.0
widthnumberThe desired maximum width of the saved image. The aspect ratio is respected.1.0.0
heightnumberThe desired maximum height of the saved image. The aspect ratio is respected.1.0.0
correctOrientationbooleanWhether to automatically rotate the image "up" to correct for orientation in portrait mode: true1.0.0
source
CameraSource
The source to get the photo from. By default this prompts the user to select either the photo album or take a photo.: CameraSource.Prompt1.0.0
direction
CameraDirection
iOS and Web only: The camera direction.: CameraDirection.Rear1.0.0
presentationStyle'fullscreen' | 'popover'iOS only: The presentation style of the Camera.: 'fullscreen'1.0.0
webUseInputbooleanWeb only: Whether to use the PWA Element experience or file input. The default is to use PWA Elements if installed and fall back to file input. To always use file input, set this to true. Learn more about PWA Elements: https://capacitorjs.com/docs/web/pwa-elements1.0.0
promptLabelHeaderstringText value to use when displaying the prompt.: 'Photo'1.0.0
promptLabelCancelstringText value to use when displaying the prompt. iOS only: The label of the 'cancel' button.: 'Cancel'1.0.0
promptLabelPhotostringText value to use when displaying the prompt. The label of the button to select a saved image.: 'From Photos'1.0.0
promptLabelPicturestringText value to use when displaying the prompt. The label of the button to open the camera.: 'Take Picture'1.0.0

GalleryPhotos

PropTypeDescriptionSince
photosGalleryPhoto[]Array of all the picked photos.1.2.0

GalleryPhoto

PropTypeDescriptionSince
pathstringFull, platform-specific file URL that can be read later using the Filesystem API.1.2.0
webPathstringwebPath returns a path that can be used to set the src attribute of an image for efficient loading and rendering.1.2.0
exifanyExif data, if any, retrieved from the image1.2.0
formatstringThe format of the image, ex: jpeg, png, gif. iOS and Android only support jpeg. Web supports jpeg, png and gif.1.2.0

GalleryImageOptions

PropTypeDescriptionDefaultSince
qualitynumberThe quality of image to return as JPEG, from 0-100 Note: This option is only supported on Android and iOS.1.2.0
widthnumberThe desired maximum width of the saved image. The aspect ratio is respected.1.2.0
heightnumberThe desired maximum height of the saved image. The aspect ratio is respected.1.2.0
correctOrientationbooleanWhether to automatically rotate the image "up" to correct for orientation in portrait mode: true1.2.0
presentationStyle'fullscreen' | 'popover'iOS only: The presentation style of the Camera.: 'fullscreen'1.2.0
limitnumberMaximum number of pictures the user will be able to choose. Note: This option is only supported on Android 13+ and iOS.0 (unlimited)1.2.0

PermissionStatus

PropType
camera
CameraPermissionState
photos
CameraPermissionState

CameraPluginPermissions

PropType
permissionsCameraPermissionType[]

Type Aliases

CameraPermissionState

PermissionState | 'limited'

PermissionState

'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'

CameraPermissionType

'camera' | 'photos'

Enums

CameraResultType

MembersValue
Uri'uri'
Base64'base64'
DataUrl'dataUrl'

CameraSource

MembersValueDescription
Prompt'PROMPT'Prompts the user to select either the photo album or take a photo.
Camera'CAMERA'Take a new photo using the camera.
Photos'PHOTOS'Pick an existing photo from the gallery or photo album.

CameraDirection

MembersValue
Rear'REAR'
Front'FRONT'