跳到主要内容

拍照

使用 CaptureMode 启动设备拍照会话,并在适当时停止。

前置条件

API 参考

协议

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,不返回照片文件路径。

使用示例

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") }
}