スタートガイド
インストール手順と、このプラグインの完全なMarkdownガイドを含むセットアッププロンプトをコピーします。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/camera-preview`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/camera-preview/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
インストール
「インストール」セクションAIアシストによるセットアップでプラグインをインストールできます。以下のコマンドを使用して、お使いのAIツールに Capgo のスキルを追加してください:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins次に、以下のプロンプトを使用します:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/camera-preview` plugin in my project.手動セットアップを希望する場合は、以下のコマンドを実行してプラグインをインストールし、下記のプラットフォーム別の手順に従ってください:
bun add @capgo/camera-previewbunx cap syncインポート
「インポート」セクションimport { CameraPreview } from '@capgo/camera-preview';クイックスタート
「クイックスタート」セクションUIの背後で背面カメラのプレビューを開始し、静止画をキャプチャした後、停止してカメラを解放します。
import { CameraPreview } from '@capgo/camera-preview';
await CameraPreview.start({ position: 'rear', parent: 'cameraPreview', toBack: true,});
const { value } = await CameraPreview.capture({ quality: 90 });console.log('Photo base64', value);
await CameraPreview.stop();次の要素をフルスクリーンでマウントします: id="cameraPreview" HTMLで背景を透明に設定し、WebViewの背後にあるNative previewが見えるようにルートまたはアプリの背景を透過させてください(例: ion-content { --background: transparent; } (Ionicのルート上でも)。
例:バーコードのオーバーレイ
「Example: barcode overlay」というセクションawait CameraPreview.start({ position: 'rear', toBack: true });
await CameraPreview.startBarcodeScanner({ formats: ['qr_code', 'code_128'], detectionInterval: 400,});
let handled = false;const barcodeListener = await CameraPreview.addListener('barcodeScanned', async (event) => { if (handled) return; const code = event.barcodes[0]?.value; if (!code) return; handled = true; await CameraPreview.stopBarcodeScanner(); await handleScan(code);});
// When leaving the screen:await barcodeListener.remove();例:カメラの反転とフラッシュ
「Example: flip camera and flash」というセクションawait CameraPreview.flip();await CameraPreview.setFlashMode({ flashMode: 'torch' });よくある落とし穴
「よくある落とし穴」セクション- 権限:カメラ(動画を録画する場合はマイクも)の権限を事前にリクエスト
start. - ライフサイクル:呼び出し
stop()ルートを離れる際、または次の画面への遷移時にカメラの起動に失敗する可能性があります。 - Web: モバイルブラウザでのプレビューを利用する前に、プラグインのREADMEでサポートされているAPIを確認してください。
API の概要
「API 概要」セクションstart
「start」セクションカメラプレビューを開始します。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.start({ parent: 'parent' });console.log(result);カメラプレビューを停止します。
import { CameraPreview } from '@capgo/camera-preview';
await CameraPreview.stop();capture
「capture」セクションカメラから写真を撮影します。
もし storeToFile に設定されました true プレビュー開始時、返される
value はbase64文字列ではなく、デバイス上の絶対ファイルパスになります。ファイルパスからbase64文字列を取得するには、getBase64FromFilePathを使用してください。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.capture({ height: 1920 });console.log(result);captureSample
「captureSample」セクションカメラプレビューのストリームから1フレームをキャプチャします。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.captureSample({ quality: 85, mirrorFrontCamera: false,});console.log(result);getSupportedFlashModes
「getSupportedFlashModes」セクションアクティブなカメラがサポートしているフラッシュモードを取得します。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.getSupportedFlashModes();console.log(result);setAspectRatio
setAspectRatio セクションカメラプレビューのアスペクト比を設定します。
import { CameraPreview } from '@capgo/camera-preview';
await CameraPreview.setAspectRatio({} as { aspectRatio: '4:3' | '16:9'; x?: number; y?: number });getAspectRatio
getAspectRatio セクションカメラプレビューの現在のアスペクト比を取得します。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.getAspectRatio();console.log(result);setGridMode
setGridMode セクションカメラプレビューのオーバーレイのグリッドモードを設定します。
import { CameraPreview } from '@capgo/camera-preview';
await CameraPreview.setGridMode({ gridMode: 'none' });getGridMode
getGridMode セクションカメラプレビューのオーバーレイの現在のグリッドモードを取得します。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.getGridMode();console.log(result);checkPermissions
「checkPermissions」セクションシステムダイアログを表示せずに、現在のカメラ(および任意でマイク)の権限ステータスを確認します。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.checkPermissions();// The result holds sensitive values: use it without logging it.requestPermissions
「requestPermissions」セクションカメラ(および任意でマイク)の権限をリクエストします。すでに権限が許可または拒否されている場合は、ダイアログを表示せずに現在のステータスを返します。次の場合: showSettingsAlert 条件が true で権限が拒否されている場合、ユーザーをアプリの設定画面へ誘導するプラットフォーム固有のアラートが表示されます。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.requestPermissions();// The result holds sensitive values: use it without logging it.getHorizontalFov
セクション「getHorizontalFov」アクティブなカメラの水平視野角(FOV)を取得します。 注意: デバイスによっては推定値になる場合があります。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.getHorizontalFov();console.log(result);getSupportedPictureSizes
セクション「getSupportedPictureSizes」すべてのカメラでサポートされている画像サイズを取得します。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.getSupportedPictureSizes();console.log(result);setFlashMode
「setFlashMode」セクションアクティブなカメラのフラッシュモードを設定します。
import { CameraPreview } from '@capgo/camera-preview';
await CameraPreview.setFlashMode({ flashMode: 'off' });flip
「flip」セクションフロントカメラとリアカメラを切り替えます。
import { CameraPreview } from '@capgo/camera-preview';
await CameraPreview.flip();setOpacity
「setOpacity」セクションカメラプレビューの不透明度を設定します。
import { CameraPreview } from '@capgo/camera-preview';
await CameraPreview.setOpacity({ opacity: 1.0 });stopRecordVideo
「stopRecordVideo」セクション進行中のビデオ録画を停止します。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.stopRecordVideo();console.log(result);startRecordVideo
「startRecordVideo」セクションビデオの録画を開始します。
import { CameraPreview } from '@capgo/camera-preview';
await CameraPreview.startRecordVideo({ parent: 'parent' });isRunning
「isRunning」セクションカメラプレビューが現在実行中かどうかを確認します。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.isRunning();console.log(result);getAvailableDevices
「getAvailableDevices」セクション利用可能なすべてのカメラデバイスを取得します。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.getAvailableDevices();console.log(result);getZoom
「getZoom」セクション最小/最大ズーム値や現在のレンズ情報を含む、現在のズーム状態を取得します。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.getZoom();console.log(result);getZoomButtonValues
セクション「getZoomButtonValues」素早い切り替えのためのズームボタンの値を返します。
- iOS/Android: 超広角が利用可能な場合は0.5、広角が利用可能な場合は1と2、望遠が利用可能な場合は3が含まれます
- Web: 未対応
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.getZoomButtonValues();console.log(result);setZoom
セクション「setZoom」カメラのズームレベルを設定します。
import { CameraPreview } from '@capgo/camera-preview';
await CameraPreview.setZoom({ level: 1 });getFlashMode
セクション「getFlashMode」現在のフラッシュモードを取得します。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.getFlashMode();console.log(result);setDeviceId
「setDeviceId」セクションアクティブなカメラを指定したIDのものに切り替えます deviceId.
import { CameraPreview } from '@capgo/camera-preview';
await CameraPreview.setDeviceId({ deviceId: 'device-id-123' });getDeviceId
「getDeviceId」セクション現在バインドされているカメラデバイスのIDを取得します。 Androidで物理レンズのリクエストが論理カメラにフォールバックした場合、バインドされている論理カメラのIDが返されます。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.getDeviceId();console.log(result);getPreviewSize
「getPreviewSize」セクション現在のプレビューサイズと位置を取得します。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.getPreviewSize();console.log(result);setPreviewSize
「setPreviewSize」セクションプレビューのサイズと位置を設定します。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.setPreviewSize({ width: 1080, height: 1920,});console.log(result);setFocus
「setFocus」セクションプレビュー内の特定のポイントにカメラのフォーカスを設定します。
注意: このプラグインはネイティブのタップフォーカス用ジェスチャハンドラを一切登録しません。タップはHTML/JS側(例: オーバーレイUI上)で処理し、正規化した座標をここに渡してください。
import { CameraPreview } from '@capgo/camera-preview';
await CameraPreview.setFocus({ x: 1, y: 1,});deleteFile
「deleteFile」セクションDeletes a file at the given absolute path on the device.
Use this to quickly clean up temporary images created with storeToFile.
Webではサポートされておらず、エラーがスローされます。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.deleteFile({ path: 'path/to/file' });console.log(result);getSafeAreaInsets
「getSafeAreaInsets」セクションデバイスのセーフエリアインセットを取得します。 向きに応じたノッチ/カメラの切り欠きインセットと、現在の向きを返します。 ポートレート時: 上部のインセット(ノッチが上)を返します。 ランドスケープ時: 左側のインセット(ノッチが横に移動)を返します。 これは、最新のスマートフォンすべてにある切り欠きエリア(ノッチ、パンチホールなど)を対象としています。
Android: 値はdp(論理ピクセル)で返されます。 iOS: 値は物理ピクセルで返され、ステータスバーは含みません(純粋なノッチ/切り欠きのサイズのみ)。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.getSafeAreaInsets();console.log(result);getOrientation
「getOrientation」セクション現在のデバイスの向きをクロスプラットフォーム形式で取得します。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.getOrientation();console.log(result);getExposureModes
「getExposureModes」セクションアクティブなカメラがサポートしている露出モードを返します。 モードには 'locked', 'auto', 'continuous', 'custom' が含まれます。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.getExposureModes();console.log(result);getExposureMode
「getExposureMode」セクション現在の露出モードを返します。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.getExposureMode();console.log(result);setExposureMode
「setExposureMode」セクション露出モードを設定します。
import { CameraPreview } from '@capgo/camera-preview';
await CameraPreview.setExposureMode({ mode: 'AUTO' });getExposureCompensationRange
「getExposureCompensationRange」セクションサポートされている露出補正(EVバイアス)の範囲を返します。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.getExposureCompensationRange();console.log(result);getExposureCompensation
「getExposureCompensation」セクション現在の露出補正(EVバイアス)を返します。
import { CameraPreview } from '@capgo/camera-preview';
const result = await CameraPreview.getExposureCompensation();console.log(result);setExposureCompensation
「setExposureCompensation」セクション露出補正(EVバイアス)を設定します。値は範囲内にクランプされます。
import { CameraPreview } from '@capgo/camera-preview';
await CameraPreview.setExposureCompensation({ value: 1 });型リファレンス
「型リファレンス」セクションCameraPreviewOptions
「CameraPreviewOptions」セクションカメラプレビューを開始するための設定オプションを定義します。
export interface CameraPreviewOptions { /** * The parent element to attach the video preview to. * @platform web */ parent?: string; /** * A CSS class name to add to the preview element. * @platform web */ className?: string; /** * The width of the preview in pixels. Defaults to the screen width. * @platform android, ios, web */ width?: number; /** * The height of the preview in pixels. Defaults to the screen height. * @platform android, ios, web */ height?: number; /** * The horizontal origin of the preview, in pixels. * @platform android, ios */ x?: number; /** * The vertical origin of the preview, in pixels. * @platform android, ios */ y?: number; /** * The aspect ratio of the camera preview, '4:3' or '16:9' or 'fill'. * Cannot be set if width or height is provided, otherwise the call will be rejected. * Use setPreviewSize to adjust size after starting. * * @since 2.0.0 */ aspectRatio?: '4:3' | '16:9'; /** * Controls how the camera preview fills the available space. * - 'contain': Fits the entire preview within the space, may show letterboxing (default). * - 'cover': Fills the entire space, may crop edges of the preview. * @default "contain" * @platform android, ios, web */ aspectMode?: 'cover' | 'contain'; /** * The grid overlay to display on the camera preview. * @default "none" * @since 2.1.0 */ gridMode?: GridMode; /** * Adjusts the y-position to account for safe areas (e.g., notches). * @platform ios * @default false */ includeSafeAreaInsets?: boolean; /** * If true, places the preview behind the webview. * @platform android * @default true */ toBack?: boolean; /** * Bottom padding for the preview, in pixels. * @platform android, ios */ paddingBottom?: number; /** * Whether to rotate the preview when the device orientation changes. * @platform ios * @default true */ rotateWhenOrientationChanged?: boolean; /** * The camera to use. * @default "rear" */ position?: CameraPosition | string; /** * If true, saves the captured image to a file and returns the file path. * If false, returns a base64 encoded string. * @default false */ storeToFile?: boolean; /** * If true, prevents the plugin from rotating the image based on EXIF data. * @platform android * @default false */ disableExifHeaderStripping?: boolean; /** * If true, disables the audio stream, preventing audio permission requests. * @default true */ disableAudio?: boolean; /** * If true, locks the device orientation while the camera is active. * @platform android * @default false */ lockAndroidOrientation?: boolean; /** * If true, allows the camera preview's opacity to be changed. * @platform android, web * @default false */ enableOpacity?: boolean;
/** * If true, disables the visual focus indicator when tapping to focus. * @platform android, ios * @default false */ disableFocusIndicator?: boolean; /** * The `deviceId` of the camera to use. If provided, `position` is ignored. * @platform ios */ deviceId?: string; /** * On Android, attempts to bind a physical camera directly when `deviceId` refers to a physical lens. * Disabled by default because OEM support is inconsistent; when false, Android keeps the current logical-camera fallback behavior. * @default false * @platform android */ enablePhysicalDeviceSelection?: boolean; /** * The initial zoom level when starting the camera preview. * If the requested zoom level is not available, the native plugin will reject. * @default 1.0 * @platform android, ios * @since 2.2.0 */ initialZoomLevel?: number; /** * The vertical positioning of the camera preview. * @default "center" * @platform android, ios, web * @since 2.3.0 */ positioning?: CameraPositioning; /** * If true, enables video capture capabilities when the camera starts. * @default false * @platform android * @since 7.11.0 */ enableVideoMode?: boolean; /** * If true, forces the camera to start/restart even if it's already running or busy. * This will kill the current camera session and start a new one, ignoring all state checks. * @default false * @platform android, ios, web */ force?: boolean; /** * Sets the quality of video for recording. * Options: 'low', 'medium', 'high' * @note On Android requires 'enableVideoMode' to be true * @note Will affect the entire preview stream for iOS * @platform ios, android * @default "high" */ videoQuality?: 'low' | 'medium' | 'high';}CameraPreviewPictureOptions
「CameraPreviewPictureOptions」セクション写真を撮影するためのオプションを定義します。
export interface CameraPreviewPictureOptions { /** * The maximum height of the picture in pixels. The image will be resized to fit within this height while maintaining aspect ratio. * If not specified the captured image will match the preview's visible area. */ height?: number; /** * The maximum width of the picture in pixels. The image will be resized to fit within this width while maintaining aspect ratio. * If not specified the captured image will match the preview's visible area. */ width?: number; /** * The quality of the captured image, from 0 to 100. * Does not apply to `.png` format. * @default 85 */ quality?: number; /** * The format of the captured image. * @default "jpeg" */ format?: PictureFormat; /** * If true, the captured image will be saved to the user's gallery. * @default false * @since 7.5.0 */ saveToGallery?: boolean; /** * If true, the plugin will attempt to add GPS location data to the image's EXIF metadata. * This may prompt the user for location permissions. * @default false * @since 7.6.0 */ withExifLocation?: boolean; /** * If true, the plugin will embed a timestamp in the top-right corner of the image. * @default false * @since 7.17.0 */ embedTimestamp?: boolean; /** * If true, the plugin will embed the current location in the top-right corner of the image. * Requires `withExifLocation` to be enabled. * @default false * @since 7.18.0 */ embedLocation?: boolean; /** * Sets the priority for photo quality vs. capture speed. * - "speed": Prioritizes faster capture times, may reduce image quality. * - "balanced": Aims for a balance between quality and speed. * - "quality": Prioritizes image quality, may reduce capture speed. * See https://developer.apple.com/documentation/avfoundation/avcapturephotosettings/photoqualityprioritization for details. * * @since 7.21.0 * @platform ios * @default "speed" */ photoQualityPrioritization?: 'speed' | 'balanced' | 'quality';}ExifData
「ExifData」セクション画像から抽出されたEXIFデータを表します。
export interface ExifData { [key: string]: any;}CameraSampleOptions
「CameraSampleOptions」セクションカメラプレビューからサンプルフレームをキャプチャするためのオプションを定義します。
export interface CameraSampleOptions { /** * The quality of the captured sample, from 0 to 100. * @default 85 */ quality?: number;}CameraPreviewFlashMode
「CameraPreviewFlashMode」セクションカメラで使用可能なフラッシュモード。‘torch’は常時点灯モードです。
export type CameraPreviewFlashMode = 'off' | 'on' | 'auto' | 'torch';GridMode
「GridMode」セクションexport type GridMode = 'none' | '3x3' | '4x4';PermissionRequestOptions
「PermissionRequestOptions」セクションexport interface PermissionRequestOptions { disableAudio?: boolean; showSettingsAlert?: boolean; title?: string; message?: string; openSettingsButtonTitle?: string; cancelButtonTitle?: string;}CameraPermissionStatus
「CameraPermissionStatus」セクションexport interface CameraPermissionStatus { camera: PermissionState; microphone?: PermissionState;}SupportedPictureSizes
「SupportedPictureSizes」セクション特定の方向を向いたカメラでサポートされている画像サイズを表します。
export interface SupportedPictureSizes { /** The camera direction ("front" or "rear"). */ facing: string; /** A list of supported picture sizes for this camera. */ supportedPictureSizes: PictureSize[];}CameraOpacityOptions
セクション「CameraOpacityOptions」カメラプレビューの不透明度を設定するためのオプションを定義します。
export interface CameraOpacityOptions { /** * The opacity percentage, from 0.0 (fully transparent) to 1.0 (fully opaque). * @default 1.0 */ opacity?: number;}CameraDevice
セクション「CameraDevice」デバイス上の物理カメラ(フロントカメラなど)を表します。
export interface CameraDevice { /** A unique identifier for the camera device. */ deviceId: string; /** A human-readable name for the camera device. */ label: string; /** The physical position of the camera on the device. */ position: CameraPosition; /** A list of all available lenses for this camera device. */ lenses: CameraLens[]; /** The overall minimum zoom factor available across all lenses on this device. */ minZoom: number; /** The overall maximum zoom factor available across all lenses on this device. */ maxZoom: number; /** Identifies whether the device is a logical camera (composed of multiple physical lenses). */ isLogical: boolean;}LensInfo
セクション「LensInfo」現在アクティブなレンズの詳細情報を表します。
export interface LensInfo { /** The focal length of the active lens in millimeters. */ focalLength: number; /** The device type of the active lens. */ deviceType: DeviceType; /** The base zoom ratio of the active lens (e.g., 0.5x, 1.0x). */ baseZoomRatio: number; /** The current digital zoom factor applied on top of the base zoom. */ digitalZoom: number;}Source of Truth
「Source Of Truth」セクションこのページはプラグインの src/definitions.tsから生成されています。upstream で公開 API が変更されたら、再度 sync を実行してください。
Getting Startedの続きを進める
「Getting Started からの続き」セクション使用している場合 スタートガイド ネイティブメディアとインターフェースの挙動を計画するには、次と連携させてください @capgo/camera-preview の使い方 @capgo/camera-preview を使用して Native Build の機能を利用する場合、 @capgo/capacitor-live-activities の使用 @capgo/capacitor-live-activities の使用におけるネイティブ機能については、 @capgo/capacitor-live-activities @capgo/capacitor-live-activities の実装の詳細については、 @capgo/capacitor-video-playerの使用方法 @capgo/capacitor-video-player を使用する際のネイティブ機能については、 @capgo/capacitor-video-player @capgo/capacitor-video-player. の実装の詳細については