फ़ोटो लें
CaptureMode के साथ device photo-taking session शुरू करें और सही समय पर session बंद करें।
आवश्यक शर्तें
- डिवाइस कनेक्ट है और
DeviceCameraAPIका अनुपालन करता है। बिना अंतर्निहित स्टोरेज वाले डिवाइस, खींची गई JPEG इमेज सीधे ऐप को भेज सकते हैं।
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| Method | उपयोग |
|---|---|
requestPhotoTaking | .camera या .ai mode में photo taking शुरू करें। |
requestStopPhotoTaking | Active photo-taking session बंद करें। |
अनुरोध के कॉलबैक success, डिवाइस का वैकल्पिक statusCode और वैकल्पिक NSError लौटाते हैं। वे फ़ोटो फ़ाइल का पथ नहीं लौटाते।
इंस्टेंट फ़ोटो कॉलबैक
बिना अंतर्निहित स्टोरेज वाले चश्मे, खींची गई JPEG इमेज वैकल्पिक device(_:didReceiveInstantPhotoData:error:) कॉलबैक से देते हैं। इसे 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);
}];
}