From abaf107a02501ee9a1105f7cf238286b04bbc206 Mon Sep 17 00:00:00 2001 From: Bartosz Firyn Date: Mon, 15 Jan 2018 17:50:12 +0100 Subject: [PATCH] Add BufferAccess interface to IpCamDevice, refs #607 --- .../sarxos/webcam/ds/ipcam/IpCamDevice.java | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/webcam-capture-drivers/driver-ipcam/src/main/java/com/github/sarxos/webcam/ds/ipcam/IpCamDevice.java b/webcam-capture-drivers/driver-ipcam/src/main/java/com/github/sarxos/webcam/ds/ipcam/IpCamDevice.java index b3e939d5..c9ef67c4 100644 --- a/webcam-capture-drivers/driver-ipcam/src/main/java/com/github/sarxos/webcam/ds/ipcam/IpCamDevice.java +++ b/webcam-capture-drivers/driver-ipcam/src/main/java/com/github/sarxos/webcam/ds/ipcam/IpCamDevice.java @@ -9,6 +9,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import java.nio.ByteBuffer; import javax.imageio.ImageIO; @@ -34,9 +35,11 @@ import org.slf4j.LoggerFactory; import com.github.sarxos.webcam.WebcamDevice; +import com.github.sarxos.webcam.WebcamDevice.BufferAccess; import com.github.sarxos.webcam.WebcamDevice.FPSSource; import com.github.sarxos.webcam.WebcamException; import com.github.sarxos.webcam.ds.ipcam.impl.IpCamMJPEGStream; +import com.github.sarxos.webcam.util.ImageUtils; /** @@ -44,7 +47,7 @@ * * @author Bartosz Firyn (sarxos) */ -public class IpCamDevice implements WebcamDevice, FPSSource { +public class IpCamDevice implements WebcamDevice, FPSSource, BufferAccess { /** * Logger. @@ -431,4 +434,31 @@ public boolean isOpen() { public double getFPS() { return reader.getFPS(); } + + /** + * Return image RGB data in form of {@link ByteBuffer}. Please note that {@link ByteBuffer} + * returned by this method does not contain original JPEG data bytes, but bytes representing RGB + * data of the image constructed from JPEG data. + */ + @Override + public synchronized ByteBuffer getImageBytes() { + final BufferedImage bi = getImage(); + if (bi == null) { + return null; + } + return ByteBuffer.wrap(ImageUtils.imageToBytes(bi)); + } + + /** + * Put image RGB data into the {@link ByteBuffer}. Please note that data from {@link ByteBuffer} + * consumed by this method does not contain original JPEG data bytes, but bytes representing RGB + * data of the image constructed from JPEG data. + */ + @Override + public void getImageBytes(ByteBuffer buffer) { + final BufferedImage bi = getImage(); + if (bi != null) { + buffer.put(ImageUtils.imageToBytes(bi)); + } + } }