Skip to content

Taking a Picture Into a Buffer

Andrew Dillon edited this page Nov 24, 2016 · 4 revisions

The takeStill() method is great for taking quick photos and easily saving them, however it is often useful to capture an image without saving it. In JRPiCam this can be done by calling the takeBufferedStill() method, which returns a BufferedImage:

RPiCamera piCamera = new RPiCamera();
BufferedImage buffImage = piCamera.takeBufferedStill(); //Take an image and load it into a BufferedImage object

The above code takes an image and loads it directly into a BufferedImage object in the calling Java application. Images taken this way are not saved to the Pi's memory which results in this method returning faster than takeStill, which does save the image.

The dimensions of an image may be specified by passing the desired width and height as arguments to takeBufferedStill():

BufferedImage buffImage = piCamera.takeBufferedStill(500, 500); //Takes and buffers a 500x500 image

The BufferedImage returned by takeBufferedStill() can easily be written to memory using Java's javax.imageio.ImageIO class:

BufferedImage buffImage = piCamera.takeBufferedStill(500, 500); //Take and buffer a 500x500 image
File saveFile = new File("/home/pi/Pictures/ACoolImage.jpg"); //Create a file to save image to
ImageIO.write(buffImage, "jpg", saveFile); //Write the buffered image to the file
Clone this wiki locally