拍照
使用 CaptureMode 启动设备拍照会话,并在适当时停止。
前置条件
- 设备已连接,并实现
DeviceCameraAPI。无机身存储空间的设备可将拍摄的 JPEG 直接发送到 App。
API 参考
协议
- Swift
- Objective-C
/// 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?)
}/// The protocol for device API that supports camera.
@protocol AIBudsDeviceCameraAPI <AIBudsDeviceAPI>
/// 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.
- (void)requestPhotoTakingWithCaptureMode:(enum AIBudsCaptureMode)mode
completion:(AIBudsStatusCodeCompletionHandler _Nullable)completion;
/// 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.
- (void)requestStopPhotoTakingWithCompletion:
(AIBudsStatusCodeCompletionHandler _Nullable)completion;
@end| 方法 | 用途 |
|---|---|
requestPhotoTaking | 在 .camera 或 .ai 模式下开始拍照。 |
requestStopPhotoTaking | 停止活动的拍照会话。 |
请求回调返回 success、可选的设备 statusCode 和可选的 NSError,不返回照片文件路径。
即时照片回调
无机身存储空间的眼镜通过可选的 device(_:didReceiveInstantPhotoData:error:) 回调传送拍摄的 JPEG。可在 DeviceDelegate 或 SDKDelegate 中实现该回调;请先处理 error,并将缺失或为空的 photoData 视为传输失败。
- Swift
- Objective-C
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.
}- (void)device:(id<AIBudsDeviceConvertible>)device
didReceiveInstantPhotoData:(NSData *_Nullable)photoData
error:(NSError *_Nullable)error {
if (error || photoData.length == 0)
return;
// Decode or persist the JPEG away from the callback path.
}SDK 会在支持的无机身存储眼镜完成拍摄后调用此回调。对于将照片保存到机身存储空间的设备,请勿等待该回调;UI 或照片图库操作应调度到适当的队列。
使用示例
- Swift
- Objective-C
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") }
}id<AIBudsDeviceCameraAPI> device = (id<AIBudsDeviceCameraAPI>)self.device;
if ([device conformsToProtocol:@protocol(AIBudsDeviceCameraAPI)]) {
[device requestPhotoTakingWithCaptureMode:AIBudsCaptureModeCamera
completion:^(BOOL success,
NSNumber *_Nullable statusCode,
NSError *_Nullable error) {
if (!success)
NSLog(@"Photo request failed: %@",
error.localizedDescription);
}];
[device requestStopPhotoTakingWithCompletion:^(
BOOL success, NSNumber *_Nullable statusCode, NSError *_Nullable error) {
if (!success)
NSLog(@"Stop failed: %@", error.localizedDescription);
}];
}