Ana içeriğe geç

Fotoğraf Çekme

Bir CaptureMode değeriyle cihazın fotoğraf çekme oturumunu başlatın ve gerektiğinde durdurun.

Ön Koşullar​

  • Cihaz bağlıdır ve DeviceCameraAPI protokolüne uyar. Yerleşik depolaması olmayan cihazlar çekilen JPEG'i doğrudan uygulamaya gönderebilir.

API Referansı​

Protokol​

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?)
}
YöntemAmaç
requestPhotoTaking.camera veya .ai modunda fotoğraf çekimini başlatır.
requestStopPhotoTakingEtkin fotoğraf çekme oturumunu durdurur.

İstek callback'leri success, isteğe bağlı cihaz statusCode değeri ve isteğe bağlı NSError döndürür. Fotoğraf dosyası yolu döndürmez.

Anlık Fotoğraf Callback'i​

Depolaması olmayan gözlükler çekilen JPEG'i isteğe bağlı device(_:didReceiveInstantPhotoData:error:) callback'iyle iletir. Bunu DeviceDelegate veya SDKDelegate üzerinde uygulayın; önce error değerini işleyin ve eksik ya da boş photoData değerini başarısız aktarım olarak kabul edin.

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, bu özelliği destekleyen ve yerleşik depolama alanı bulunmayan gözlüklerde çekimden sonra bu callback'i çağırır. Fotoğrafları yerleşik depolamaya kaydeden cihazlarda bu callback'i beklemeyin; kullanıcı arayüzü veya Fotoğraflar arşiviyle ilgili işlemleri uygun kuyruğa yönlendirin.

Kullanım Örnekleri​

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