Caprica Software

Picam 2.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.CameraException;
import uk.co.caprica.picam.CaptureFailedException;
import uk.co.caprica.picam.NativeLibraryException;
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;
import static uk.co.caprica.picam.PicamNativeLibrary.installTempLibrary;

public class MyCameraApplication {

public static void main(String[] args) throws NativeLibraryException {
installTempLibrary();
CameraConfiguration config = cameraConfiguration()
.width(1920)
.height(1080)
.automaticWhiteBalance(AutomaticWhiteBalanceMode.AUTO)
.encoding(Encoding.JPEG)
.quality(85);
try {
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());
System.exit(-1);
}
catch (CaptureFailedException e) {
System.out.println("Capture failed, reopening camera: " + e.getMessage());
camera.close();
if (!camera.open()) {
System.out.println("Failed to reopen the camera");
System.exit(-1);
}
}
}
}
catch (CameraException e) {
e.printStackTrace();
}
}
}

You could create a new camera instance rather than attempting to reopen the existing camera.

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