public class RPiCamera
extends java.lang.Object
Although it provides additional features and support for Java specific operations, RPiCamera is essentially a wrapper class for executing the raspistill software from within a Java application. Thus it is essential that all Raspberry Pis using the JRPiCam library have properly configured and working raspistill software.
Usage Example:
// Directory to save pictures to
String saveDir = "/home/pi/Pictures";
// Create RPiCamera instance
RPiCamera camera = new RPiCamera(saveDir)
.setWidth(150) // Set width property of RPiCamera.
.setHeight(150); // Set height property of RPiCamera.
// Take a picture and save it as "/home/pi/Pictures/APicture.jpg"
camera.takeStill("APicture.jpg");
Constructor and Description |
---|
RPiCamera()
Creates new RPiCamera.
|
RPiCamera(java.lang.String saveDir)
Creates new RPiCamera and sets its save directory.
|
Modifier and Type | Method and Description |
---|---|
RPiCamera |
disableBurst()
Turns off burst mode for RPiCamera.
|
RPiCamera |
enableBurst()
Turns on burst mode for RPiCamera.
|
java.lang.String |
getPrevCommand()
Gets the raspistill command previously executed by the RPiCamera.
|
RPiCamera |
selectCamera(int camNumber)
Selects which camera to use (on a multicamera system).
|
RPiCamera |
setAddRawBayer(boolean add)
Appends raw Bayer data from the RPiCamera to the image's JPEG metadata.
|
RPiCamera |
setAWB(AWB awb)
Sets Automatic White Balance (AWB) mode.
|
RPiCamera |
setBrightness(int brightness)
Sets brightness of image (0 to 100), default value is 50.
|
RPiCamera |
setColourEffect(int U,
int V)
Sets colour effect of RPiCamera.
|
RPiCamera |
setContrast(int contrast)
Sets contrast of image (-100 to 100), default value is 0.
|
RPiCamera |
setDateTimeOff()
Disables effects of
setDateTimeOn() . |
RPiCamera |
setDateTimeOn()
Replaces output pattern (%d) in save file name with DateTime (MonthDayHourMinSec).
|
RPiCamera |
setDRC(DRC drc)
Sets the Dynamic Range Compression of image.
|
RPiCamera |
setEncoding(Encoding encoding)
Sets the encoding for images.
|
RPiCamera |
setExposure(Exposure exposure)
Sets exposure mode of RPiCamera.
|
RPiCamera |
setFullPreviewOff()
Turns off fullpreview mode.
|
RPiCamera |
setFullPreviewOn()
Runs the preview windows using the full resolution capture mode.
|
RPiCamera |
setHeight(int height)
Sets height of images taken by the RPiCamera.
|
RPiCamera |
setHorizontalFlipOff()
Turns off horizontal flip.
|
RPiCamera |
setHorizontalFlipOn()
Flips the preview and saved image horizontally.
|
RPiCamera |
setImageEffect(ImageEffect imageEffect)
Sets an effect to be applied to image.
|
RPiCamera |
setISO(int iso)
WARNING: OPERATION NOT YET SUPPORTED BY raspistill SOFTWARE.
|
RPiCamera |
setLinkLatestImage(boolean link,
java.lang.String fileName)
Links latest image to the specified file.
|
RPiCamera |
setMeteringMode(MeteringMode meteringMode)
Sets metering mode used for preview and capture.
|
RPiCamera |
setPreviewFullscreen(boolean fullscreen)
Turn fullscreen preview on or off.
|
RPiCamera |
setPreviewOpacity(int opacity)
Sets the preview window's opacity (0 to 255).
|
RPiCamera |
setQuality(int quality)
Sets quality of image (0 to 100), 75 is recommended for usual
purposes.
|
RPiCamera |
setRegionOfInterest(double x,
double y,
double w,
double d)
Allows the specification of the area of the sensor to be used as the source
for the preview and capture.
|
RPiCamera |
setRotation(int rotation)
Sets the rotation of the image in viewfinder and resulting image.
|
RPiCamera |
setSaturation(int saturation)
Sets colour saturation of image (-100 to 100), default is 0.
|
RPiCamera |
setSaveDir(java.lang.String saveDir)
Sets the RPiCamera's save directory.
|
RPiCamera |
setSharpness(int sharpness)
Sets sharpness of image (-100 to 100), default value is 0.
|
RPiCamera |
setShutter(int speed)
Set the shutter speed to the specified value (in microseconds).
|
RPiCamera |
setThumbnailParams(int x,
int y,
int quality)
Allows specification of the thumbnail image inserted in to the image file.
|
RPiCamera |
setTimeout(int time)
Sets the RPiCamera's timeout.
|
RPiCamera |
setTimestampOff()
Disables effects of
setTimestampOn() . |
RPiCamera |
setTimestampOn()
Replace output pattern (%d) with unix timestamp (seconds since 1970)
E.g.
|
RPiCamera |
setToDefaults()
Sets all RPiCamera options to their default settings, overriding any previously
set options.
|
RPiCamera |
setVerticalFlipOff()
Turns off vertical flip.
|
RPiCamera |
setVerticalFlipOn()
Flips the preview and saved image vertically.
|
RPiCamera |
setWidth(int width)
Sets width of images taken by RPiCamera.
|
void |
stop()
Stops any raspistill processes being run by RPiCamera.
|
java.awt.image.BufferedImage |
takeBufferedStill()
Takes an image and stores it in a BufferedImage object.
|
java.awt.image.BufferedImage |
takeBufferedStill(int width,
int height)
Takes an image of the specified width and height and stores it in a BufferedImage
object.
|
java.io.File |
takeStill(java.lang.String pictureName)
Takes an image and saves it under the specified name to the RPiCamera's save
directory ("/home/pi/Pictures" by default).
|
java.io.File |
takeStill(java.lang.String pictureName,
int width,
int height)
Takes an image of the specified width and height and saves it under the
specified name to the RPiCamera's save directory ("/home/pi/Pictures" by
default).
|
int[] |
takeStillAsRGB(boolean keepPadding)
Captures an image and returns the RGB values of that image.
|
int[] |
takeStillAsRGB(int width,
int height,
boolean keepPadding)
Captures an image and returns the RGB values of that image.
|
java.io.File |
timelapse(boolean wait,
java.lang.String pictureName,
int time)
Take a series of timelapsed photos for the specified time frame and save them under the
specified filename to the RPiCamera's save directory.
|
RPiCamera |
turnOffPreview()
Turns off image preview.
|
RPiCamera |
turnOffThumbnail()
Turns off image thumbnails.
|
RPiCamera |
turnOnPreview()
Turns on image preview.
|
RPiCamera |
turnOnPreview(int x,
int y,
int w,
int h)
Defines the size and location on the screen that the preview window will be placed.
|
public RPiCamera() throws FailedToRunRaspistillException
FailedToRunRaspistillException
public RPiCamera(java.lang.String saveDir) throws FailedToRunRaspistillException
saveDir
- A String specifying the directory for RPiCamera to save images.FailedToRunRaspistillException
public java.io.File takeStill(java.lang.String pictureName, int width, int height) throws java.io.IOException, java.lang.InterruptedException
Usage Example:
// Create an RPiCamera instance using default constructor, which sets the
// save directory to "/home/pi/Pictures".
RPiCamera piCamera = new RPiCamera()
.setEncoding(Encoding.PNG); // Change encoding from JPEG to PNG
// Take a 500x500 PNG image and save it as "/home/pi/Pictures/AStillImage.png"
piCamera.takeStill("AStillImage.png", 500, 500);
pictureName
- A String containing the name to save picture under.width
- An int specifying the width of the image to take.height
- An int specifying the height of the image to take.java.io.IOException
java.lang.InterruptedException
public java.io.File takeStill(java.lang.String pictureName) throws java.io.IOException, java.lang.InterruptedException
Usage Example:
// Create an RPiCamera instance using default constructor, which sets the
// save directory to "/home/pi/Pictures".
RPiCamera piCamera = new RPiCamera()
.setEncoding(Encoding.PNG); // Change encoding from JPEG to PNG
// Take a PNG image and save it as "/home/pi/Pictures/AStillImage.png"
piCamera.takeStill("AStillImage.png");
pictureName
- A String containing the name to save picture under.java.io.IOException
java.lang.InterruptedException
public java.awt.image.BufferedImage takeBufferedStill(int width, int height) throws java.io.IOException, java.lang.InterruptedException
Usage Example:
// Create an RPiCamera instance using default constructor, which sets the
// save directory to "/home/pi/Pictures".
RPiCamera piCamera = new RPiCamera()
.setEncoding(Encoding.PNG); // Change encoding from JPEG to PNG
// Take a 500x500 PNG image and store it in a BufferedImage
BufferedImage buffImg = piCamera.takeBufferedStill(500, 500);
width
- An int specifying width of image to take.height
- An int specifying height of image to take.java.io.IOException
java.lang.InterruptedException
public java.awt.image.BufferedImage takeBufferedStill() throws java.io.IOException, java.lang.InterruptedException
Usage Example:
// Create an RPiCamera instance using default constructor, which sets the
// save directory to "/home/pi/Pictures".
RPiCamera piCamera = new RPiCamera()
.setEncoding(Encoding.PNG); // Change encoding from JPEG to PNG
// Take a PNG image and store it in a BufferedImage
BufferedImage buffImg = piCamera.takeBufferedStill();
java.io.IOException
java.lang.InterruptedException
public int[] takeStillAsRGB(int width, int height, boolean keepPadding) throws java.io.IOException
If an image's dimensions are not multiples of 16, then black padding will be added onto the right and bottom sides of an image until both the width and height are multiples of 16 (e.g. a 500x500 image would have a black band 12 pixels thick added to the right and bottom sides to achieve dimensions of 512x512, the closest multiple of 16 to 500).
The boolean keepPadding parameter may be set to false to remove this padding. Note that this does require extra computations to be performed, so it has the potential to slightly reduce the fps a Pi is capable of. However, this has not yet been officially tested for. If the padding is kept, then it is the responsibility of the caller to determine what the dimensions of the resulting image will be, and adjust their processing as such (e.g. If the padding is kept on a 500x500 image, the array that is returned will contain 786,432 elements, as opposed to the 750,000 one would expect from a 500x500 image. This is because the image stored in the returned array is not 500x500, but 512x512 and the caller of this method must take that into account when processing the image).
Usage Example:
// Creating and setting up an RPiCamera
RPiCamera piCamera = new RPiCamera()
.turnOffPreview()
.setTimeout(1);
// Capture an image and store its RGB values, not preserving the border
int[] rgbVals = piCamera.takeStillAsRGB(500, 500, false);
// Create a BufferedImage from the stored RGB values
BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
WritableRaster raster = (WritableRaster) image.getData();
raster.setPixels(0, 0, 500, 500, rgbVals);
image.setData(raster);
// Write the BufferedImage to a file as a JPEG
File file = new File("/home/pi/Desktop/A Cool Image.jpg");
ImageIO.write(image, "jpg", file);
width
- An int specifying the width of the image to take (ideally a multiple of 16).height
- An int specifying the height of the image to take (ideally a multiple of 16).keepPadding
- A boolean indicating whether or not to preserve the padding on an image.java.io.IOException
public int[] takeStillAsRGB(boolean keepPadding) throws java.io.IOException
If an image's dimensions are not multiples of 16, then black padding will be added onto the right and bottom sides of an image until both the width and height are multiples of 16 (e.g. a 500x500 image would have a black band 12 pixels thick added to the right and bottom sides to achieve dimensions of 512x512, the closest multiple of 16 to 500).
The boolean keepPadding parameter may be set to false to remove this padding. Note that this does require extra computations to be performed, so it has the potential to slightly reduce the fps a Pi is capable of. However, this has not yet been officially tested for. If the padding is kept, then it is the responsibility of the caller to determine what the dimensions of the resulting image will be, and adjust their processing as such (e.g. If the padding is kept on a 500x500 image, the array that is returned will contain 786,432 elements, as opposed to the 750,000 one would expect from a 500x500 image. This is because the image stored in the returned array is not 500x500, but 512x512 and the caller of this method must take that into account when processing the image).
Usage Example:
// Creating and setting up an RPiCamera
RPiCamera piCamera = new RPiCamera()
.turnOffPreview()
.setTimeout(1);
// Capture an image and store its RGB values, not preserving the border
int[] rgbVals = piCamera.takeStillAsRGB(500, 500, false);
// Create a BufferedImage from the stored RGB values
BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
WritableRaster raster = (WritableRaster) image.getData();
raster.setPixels(0, 0, 500, 500, rgbVals);
image.setData(raster);
// Write the BufferedImage to a file as a JPEG
File file = new File("/home/pi/Desktop/A Cool Image.jpg");
ImageIO.write(image, "jpg", file);
keepPadding
- A boolean indicating whether or not to preserve the padding on an image.java.io.IOException
public java.io.File timelapse(boolean wait, java.lang.String pictureName, int time) throws java.io.IOException, java.lang.InterruptedException
setTimeout(int)
method.
THIS METHOD WILL BLOCK THE THREAD UNTIL THE TIMELAPSE IS COMPLETE IF boolean wait ARGUMENT IS SET TO true.
Most recent image in the series may be appended to the file specified in the
setLinkLatestImage(boolean, String)
method. If this property is set, timelapse() will return a File
object representing the full path to the file the timelapsed photos are being linked to.
Otherwise, will return null.
When passing in a String for the photo's name, it is important to include "%04d" in the name. This is where the frame count number will be included in the file name. If the name does not contain these characters, the frame count will be appended to the beginning of each file name.
Usage Example:
// Create an RPiCamera instance using default constructor, which sets the
// save directory to "/home/pi/Pictures".
RPiCamera piCamera = new RPiCamera()
.setEncoding(Encoding.PNG) // Change encoding from JPEG to PNG
.setTimeout(10000) // Set period for timelapse to run at 10 seconds
.setLinkLatestImage(true, "/home/pi/Pictures/linkfile.png"); // Set RPiCamera to link images to "linkFile.png"
// Begin timelapse
piCamera.timelapse(
true, // Block thread until timelapse is completed
"%04dTimelapsePics.png", // Save images under this name, with frame count added to beginning
1000 // Wait 1 second between capturing each image
);
wait
- A boolean indicating whether to block the thread until timelapse is complete or not.pictureName
- A String containing name for each captured image.time
- Period of time in milliseconds to wait between taking each image.java.io.IOException
java.lang.InterruptedException
public void stop()
public java.lang.String getPrevCommand()
public RPiCamera setSaveDir(java.lang.String saveDir)
saveDir
- String containing directory for RPiCamera to save images to.public RPiCamera setToDefaults()
public RPiCamera turnOffPreview()
public RPiCamera turnOnPreview()
public RPiCamera setFullPreviewOn()
public RPiCamera setFullPreviewOff()
public RPiCamera turnOnPreview(int x, int y, int w, int h)
x
- int coordinate for upper right corner of preview window.y
- int coordinate for upper right corner of preview window.w
- An int specifying width of preview window.h
- An int specifying height of preview window.public RPiCamera setPreviewFullscreen(boolean fullscreen)
fullscreen
- turn on/off fullscreen.public RPiCamera setPreviewOpacity(int opacity)
opacity
- An integer specifying the opacity.public RPiCamera setSharpness(int sharpness)
sharpness
- An int specifying the sharpness.public RPiCamera setContrast(int contrast)
contrast
- An int specifying the contrast.public RPiCamera setBrightness(int brightness)
brightness
- An int specifying the brightness.public RPiCamera setSaturation(int saturation)
saturation
- An int specifying the saturation.public RPiCamera setISO(int iso)
iso
- An int specifying the ISO.public RPiCamera setExposure(Exposure exposure)
exposure
- An Exposure enum specifying the desired mode.public RPiCamera setAWB(AWB awb)
awb
- An AWB enum specifying the desired AWB setting.public RPiCamera setImageEffect(ImageEffect imageEffect)
imageEffect
- An ImageEffect enum specifying the desired effect.public RPiCamera setColourEffect(int U, int V)
U
- V
- public RPiCamera setMeteringMode(MeteringMode meteringMode)
meteringMode
- a MeteringMode enum specifying the desired mode.public RPiCamera setRotation(int rotation)
rotation
- public RPiCamera setHorizontalFlipOn()
public RPiCamera setHorizontalFlipOff()
public RPiCamera setVerticalFlipOn()
public RPiCamera setVerticalFlipOff()
public RPiCamera setRegionOfInterest(double x, double y, double w, double d)
So to set a ROI at half way across and down the sensor, and a width and height of a
quarter of the sensor use: setRegionOfInterest(0.5, 0.5, 0.25, 0.25)
x
- y
- w
- d
- public RPiCamera setShutter(int speed)
speed
- public RPiCamera setDRC(DRC drc)
drc
- A DRC enum specifying the desired DRC level.public RPiCamera setWidth(int width)
takeStill(String, int, int)
and takeBufferedStill(int, int)
methods.width
- An int specifying the width.public RPiCamera setHeight(int height)
takeStill(String, int, int)
and takeBufferedStill(int, int)
methods.height
- An int specifying the height.public RPiCamera setQuality(int quality)
quality
- An int specifying the quality.public RPiCamera setAddRawBayer(boolean add)
add
- turn on/off bayer data.public RPiCamera setLinkLatestImage(boolean link, java.lang.String fileName)
link
- turn on/off linking.fileName
- fully qualified path to file to link images to.public RPiCamera setTimeout(int time)
time
- An int specifying the timeout in milliseconds.public RPiCamera setThumbnailParams(int x, int y, int quality)
x
- y
- quality
- public RPiCamera turnOffThumbnail()
public RPiCamera setEncoding(Encoding encoding)
encoding
- an Encoding enum specifying the desired image encoding.public RPiCamera selectCamera(int camNumber)
camNumber
- public RPiCamera enableBurst()
public RPiCamera disableBurst()
public RPiCamera setDateTimeOn()
RPiCamera camera = new RPiCamera()
.setDateTimeOn();
camera.takeStill("APicture%d.jpg", 500, 500);
Would produce a file named "APicture1223140756" if executed on Dec 23rd, at 14:07:56.public RPiCamera setDateTimeOff()
setDateTimeOn()
. This is default state of RPiCamera
.public RPiCamera setTimestampOn()
RPiCamera camera = new RPiCamera()
.setTimestampOn();
camera.takeStill("APicture%d.jpg", 500, 500);
Would produce a file named "APictureXXX", where XXX is the number of seconds since 1970.public RPiCamera setTimestampOff()
setTimestampOn()
. This is default state of RPiCamera
.