Caprica Software

Picam 2.x Tutorial

Taking Pictures

Once you have a camera instance, it is simple to capture a picture.


Take Picture

Invoke the takePicture method to capture a picture from the camera.

This method takes a PictureCaptureHandler parameter - in this example the picture will be saved to disk:

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.FilePictureCaptureHandler;
import uk.co.caprica.picam.NativeLibraryException;
import uk.co.caprica.picam.enums.AutomaticWhiteBalanceMode;
import uk.co.caprica.picam.enums.Encoding;

import java.io.File;

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)) {
camera.takePicture(new FilePictureCaptureHandler(new File("photo.jpg")));
}
catch (CameraException e) {
e.printStackTrace();
}
catch (CaptureFailedException e) {
e.printStackTrace();
}
// Camera will be automatically closed
}
}

An alternate takePicture is also provided, allowing you to specify a capture delay before taking the picture. This can useful, especially when taking the first picture after opening the camera, to give the camera sensor some time to "settle" before taking the picture. Use whatever delay values work for you.

Picture capture handlers will be explained in the next tutorial.

A camera must be "opened" before taking any pictures. When the camera is first created it is also automatically opened so ordinarily you do not need to consider it. This becomes important when dealing with capture failures as you can close and reopen the camera to try and recover from the error. This will be explained further in a later tutorial.