Photo start and stop#
Camera 1#
When can I start or stop photo capture ?#
Photo capture can be started only when all of the the following conditions are met:
camera is active:
camera.isActive == true
camera mode is photo:
camera.modeSetting.mode == .photo
photo state is stopped:
camera.photoState.functionState == .stopped
Photo capture can be stopped when the following condition is true:
photo state is started:
camera.photoState.functionState == .started
The Camera API provides helper properties telling whether photo capture can be started are stopped:
camera.canStartPhotoCapture
: tells whether photo capture can be startedcamera.canStopPhotoCapture
: tells whether photo capture can be stopped
Sample code to monitor whether photo capture can be started or stopped :
/// Keep reference on MainCamera peripheral to get notified of changes.
private var cameraRef: Ref<MainCamera>?
/// Monitors and prints whether photo capture can be started or stopped.
func monitorCanStartStopPhotoCapture(drone: Drone) {
cameraRef = drone.getPeripheral(Peripherals.mainCamera) { camera in
// called on main thread when the camera peripheral changes
if let camera = camera {
if camera.canStartPhotoCapture {
print("Photo capture can be started")
} else if camera.canStopPhotoCapture {
print("Photo capture can be stopped")
} else {
print("Photo capture can't be started or stopped")
}
}
}
}
How to start or stop photo capture ?#
Sample code to start and stop photo capture:
/// Starts or stops photo capture.
func toggleStartStopPhotoCapture(drone: Drone) {
if let camera = drone.getPeripheral(Peripherals.mainCamera) {
if camera.canStartPhotoCapture {
// photo capture can be started
camera.startPhotoCapture()
} else if camera.canStopPhotoCapture {
// photo capture can be stopped
camera.stopPhotoCapture()
} else {
// photo capture can't be started or stopped
print("Photo capture can't be started or stopped")
}
}
}
Camera 2#
When can I start or stop photo capture ?#
Photo capture can be started only when all of the following conditions are met:
camera is active:
camera.isActive == true
camera mode is photo:
camera.config[Camera2Params.mode]?.value == .photo
photo capture state is stopped:
camera.getComponent(Camera2Components.photoCapture)?.state.canStart == true
Photo capture can be stopped when the following condition is true:
photo capture state is started or starting:
camera.getComponent(Camera2Components.photoCapture)?.state.canStop == true
Sample code to monitor whether photo capture can be started or stopped :
/// Keep reference on Camera2PhotoCapture component to get notified of changes.
private var photoCaptureRef: Ref<Camera2PhotoCapture>?
/// Monitors and prints whether photo capture can be started or stopped.
func monitorCanStartStopPhotoCapture(drone: Drone) {
if let camera = drone.getPeripheral(Peripherals.mainCamera2) {
// get photoCapture component reference and register listener
photoCaptureRef = camera.getComponent(Camera2Components.photoCapture) { photoCapture in
if photoCapture?.state.canStart == true {
print("Photo capture can be started")
} else if photoCapture?.state.canStop == true {
print("Photo capture can be stopped")
} else {
print("Photo capture can't be started or stopped")
}
}
}
}
How to start or stop photo capture ?#
Sample code to start and stop photo capture:
/// Starts or stops photo capture.
func toggleStartStopPhotoCapture(drone: Drone) {
if let camera = drone.getPeripheral(Peripherals.mainCamera2),
let photoCapture = camera.getComponent(Camera2Components.photoCapture) {
if photoCapture.state.canStart {
// photo capture can be started
photoCapture.start()
} else if photoCapture.state.canStop {
// photo capture can be stopped
photoCapture.stop()
} else {
// photo capture state don't allow to start or stop
print("Photo capture can't be started or stopped")
}
} else {
// camera peripheral or photo capture component not available at present
print("Photo capture not available")
}
}