Caprica Software

Picam 2.x Tutorial

Camera

Once you have a configuration object, the next step is to create a camera instance.


Camera

Camera creation is straightforward, using the configuration from the previous tutorial:

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.NativeLibraryException;
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);
// ... take picture ...
}
catch (CameraException e) {
e.printStackTrace();
}
}
}

This code simply creates a new Camera instance, passing the configuration via a constructor parameter.

You could implement this in a slightly more compact way:

MyCameraApplication.java
import uk.co.caprica.picam.Camera;
import uk.co.caprica.picam.CameraException;
import uk.co.caprica.picam.NativeLibraryException;
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();
try {
Camera camera = new Camera(cameraConfiguration()
.width(1920)
.height(1080)
.automaticWhiteBalance(AutomaticWhiteBalanceMode.AUTO)
.encoding(Encoding.JPEG)
.quality(85)
);
// ... take picture ...
}
catch (CameraException e) {
e.printStackTrace();
}
}
}

In the above code, the intermediate step of creating the camera configuration has been combined with creating the new camera instance.

Whichever implementation you choose makes no difference, it is simply a matter of which code style you prefer.

Clean-up

The camera instance should be closed when you no longer need it:

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.NativeLibraryException;
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);
// ... take picture ...
camera.close();
}
catch (CameraException e) {
e.printStackTrace();
}
}
}

You could use "try-with-resources" and the camera instance will be automatically closed:

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.NativeLibraryException;
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)) {
// ... take picture ...
}
catch (CameraException e) {
e.printStackTrace();
}
// Camera will be automatically closed
}
}

If your application is going to take more than one picture, it is recommended that you create the camera instance once and keep it open for as long as you need it - do not create a camera, take a picture, close the camera, create a new camera, take a picture, and so on...