Caprica Software

Picam 1.x Tutorial

Camera

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


Camera Creation

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.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);
// ... take picture ...
}
}

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.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) {
Camera camera = new Camera(cameraConfiguration()
.width(1920)
.height(1080)
.automaticWhiteBalance(AutomaticWhiteBalanceMode.AUTO)
.encoding(Encoding.JPEG)
.quality(85)
);
// ... take picture ...
}
}

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.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);
// ... take picture ...
camera.close();
}
}

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.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);
try (Camera camera = new Camera(config)) {
// ... take picture ...
}
// 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...