Caprica Software

Picam 1.x Tutorial

Capture Failures

It is unlikely for a picture capture to fail, but it is possible.


Handling Capture Failures

The takePicture method may throw a CaptureFailedException.

If this exception is thrown you should close the camera, throw away the camera instance and create a new camera instance to use instead.

A subsequent capture with the new camera instance should work.

MyCameraApplication.java
import uk.co.caprica.picam.Camera;
import uk.co.caprica.picam.CameraConfiguration;
import uk.co.caprica.picam.CaptureFailedException;
import uk.co.caprica.picam.PictureCaptureHandler;
import uk.co.caprica.picam.enums.AutomaticWhiteBalanceMode;
import uk.co.caprica.picam.enums.Encoding;

import static uk.co.caprica.picam.CameraConfiguration.cameraConfiguration;

public class MyCameraApplication {

public static void main(String[] args) {
CameraConfiguration config = cameraConfiguration()
.width(1920)
.height(1080)
.automaticWhiteBalance(AutomaticWhiteBalanceMode.AUTO)
.encoding(Encoding.JPEG)
.quality(85);
Camera camera = new Camera(config);
PictureCaptureHandler<?> pictureCaptureHandler = new MyPictureCaptureHandler();
for (;;) {
try {
Thread.sleep(5000);
camera.takePicture(pictureCaptureHandler);
}
catch (InterruptedException e) {
System.out.println("Thread interrupted while sleeping: " + e.getMessage());
break;
}
catch (CaptureFailedException e) {
System.out.println("Capture failed, replacing camera: " + e.getMessage());
camera.close();
camera = new Camera(config);
}
}
camera.close();
}
}

This is just an example, implement whatever error-handling and recovery mechanism you need.