メインコンテンツまでスキップ

写真を撮影

CaptureMode を指定してデバイスの写真撮影セッションを開始し、適切なタイミングで停止します。

前提条件​

  • デバイスが接続済みで、DeviceCameraAPI に準拠していること。オンボードストレージを持たないデバイスは、撮影した JPEG をアプリへ直接プッシュできます。

API Reference​

プロトコル​

Swift
/// The protocol for device API that supports camera.
protocol DeviceCameraAPI: DeviceAPI {
    /// Requests to take a photo with the specified capture mode.
    /// - Parameters:
    ///   - mode: The capture mode for photo taking.
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: `true` if the operation was successful; otherwise `false`.
    ///     - statusCode: The status code returned by the device. `nil` if the operation failed.
    ///     - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
    func requestPhotoTaking(
        withCaptureMode mode: CaptureMode,
        completion: AIBudsStatusCodeCompletionHandler?
    )

    /// Requests to stop the current photo taking session.
    /// - Parameters:
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: `true` if the operation was successful; otherwise `false`.
    ///     - statusCode: The status code returned by the device. `nil` if the operation failed.
    ///     - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
    func requestStopPhotoTaking(_ completion: AIBudsStatusCodeCompletionHandler?)
}
メソッド用途
requestPhotoTaking.camera または .ai モードで写真撮影を開始。
requestStopPhotoTaking実行中の写真撮影セッションを停止。

リクエストのコールバックは success、オプションのデバイス statusCode、オプションの NSError を返します。写真ファイルのパスは返しません。

インスタント写真コールバック​

内蔵ストレージのないメガネは、任意実装の device(_:didReceiveInstantPhotoData:error:) コールバックで撮影した JPEG を渡します。DeviceDelegate または SDKDelegate のどちらかに実装し、まず error を確認してください。photoData がない場合や空の場合は転送失敗として扱います。

Swift
func device(
    _ device: DeviceConvertible,
    didReceiveInstantPhotoData photoData: Data?,
    error: NSError?
) {
    guard error == nil, let photoData, !photoData.isEmpty else { return }
    // Decode or persist the JPEG away from the callback path.
}

対応する内蔵ストレージのないメガネでは、撮影後に SDK がこのコールバックを呼び出します。写真を内蔵ストレージへ保存するデバイスではこのコールバックを待機しないでください。UI や写真ライブラリの処理は適切なキューで実行します。

使用例​

Swift
guard let device = device as? DeviceCameraAPI else { return }

device.requestPhotoTaking(withCaptureMode: .camera) { success, statusCode, error in
    guard success else {
        print(error?.localizedDescription ?? "Photo request failed")
        return
    }
    print("Photo taking started: \(statusCode?.stringValue ?? "Unavailable")")
}

device.requestStopPhotoTaking { success, _, error in
    if !success { print(error?.localizedDescription ?? "Stop failed") }
}