@capacitor/camera
カメラAPIは、カメラで写真を撮影したり、フォトアルバムから既存の写真を選択する機能を提供します。
Install
npm install @capacitor/camera
npx cap sync
iOS
iOSでは、Info.plistにアプリ用の以下の使用説明を追加して記入する必要があります:
NSCameraUsageDescription(Privacy - Camera Usage Description)NSPhotoLibraryAddUsageDescription(Privacy - Photo Library Additions Usage Description)NSPhotoLibraryUsageDescription(Privacy - 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.
| Param | Type |
|---|---|
options | |
Returns:
Promise<Photo>
Since: 1.0.0