From 126e25cd2152990cfaccc430eb9850d7ae720dbc Mon Sep 17 00:00:00 2001 From: Bartosz Firyn Date: Sun, 8 Jul 2012 17:47:59 +0200 Subject: [PATCH] Add possibility to set view size + example --- .../TakePictureDifferentSizeExample.java | 26 ++++++ .../sarxos/webcam/TakePictureExample.java | 4 +- .../webcam/TakePictureFromTwoCamsExample.java | 29 ++++++ .../java/com/github/sarxos/webcam/Webcam.java | 92 ++++++++++++++++++- 4 files changed, 143 insertions(+), 8 deletions(-) create mode 100644 src/example/java/com/github/sarxos/webcam/TakePictureDifferentSizeExample.java create mode 100644 src/example/java/com/github/sarxos/webcam/TakePictureFromTwoCamsExample.java diff --git a/src/example/java/com/github/sarxos/webcam/TakePictureDifferentSizeExample.java b/src/example/java/com/github/sarxos/webcam/TakePictureDifferentSizeExample.java new file mode 100644 index 00000000..9c4d1977 --- /dev/null +++ b/src/example/java/com/github/sarxos/webcam/TakePictureDifferentSizeExample.java @@ -0,0 +1,26 @@ +package com.github.sarxos.webcam; + +import java.awt.Dimension; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + + +/** + * Set view size before taking picture. It's worth to mention that size has to + * be set before opening camera. + * + * @author Bartosz Firyn (SarXos) + */ +public class TakePictureDifferentSizeExample { + + public static void main(String[] args) throws IOException { + Webcam webcam = Webcam.getDefault(); + webcam.setViewSize(new Dimension(176, 144)); + webcam.open(); + ImageIO.write(webcam.getImage(), "PNG", new File("test.png")); + webcam.close(); + } + +} diff --git a/src/example/java/com/github/sarxos/webcam/TakePictureExample.java b/src/example/java/com/github/sarxos/webcam/TakePictureExample.java index 13000c4e..3fc3ff5c 100644 --- a/src/example/java/com/github/sarxos/webcam/TakePictureExample.java +++ b/src/example/java/com/github/sarxos/webcam/TakePictureExample.java @@ -7,14 +7,12 @@ /** - * Proof of concept of how to handle webcam video stream from Java - * * @author Bartosz Firyn (SarXos) */ public class TakePictureExample { public static void main(String[] args) throws IOException { - Webcam webcam = Webcam.getWebcams().get(0); + Webcam webcam = Webcam.getDefault(); webcam.open(); ImageIO.write(webcam.getImage(), "PNG", new File("test.png")); webcam.close(); diff --git a/src/example/java/com/github/sarxos/webcam/TakePictureFromTwoCamsExample.java b/src/example/java/com/github/sarxos/webcam/TakePictureFromTwoCamsExample.java new file mode 100644 index 00000000..89793334 --- /dev/null +++ b/src/example/java/com/github/sarxos/webcam/TakePictureFromTwoCamsExample.java @@ -0,0 +1,29 @@ +package com.github.sarxos.webcam; + +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + + +/** + * @author Bartosz Firyn (SarXos) + */ +public class TakePictureFromTwoCamsExample { + + public static void main(String[] args) throws IOException { + + Webcam webcam1 = Webcam.getWebcams().get(0); + Webcam webcam2 = Webcam.getWebcams().get(1); + + webcam1.open(); + webcam2.open(); + + ImageIO.write(webcam1.getImage(), "PNG", new File("test1.png")); + ImageIO.write(webcam2.getImage(), "PNG", new File("test2.png")); + + webcam1.close(); + webcam2.close(); + } + +} diff --git a/src/main/java/com/github/sarxos/webcam/Webcam.java b/src/main/java/com/github/sarxos/webcam/Webcam.java index adda19cd..a8dcd557 100644 --- a/src/main/java/com/github/sarxos/webcam/Webcam.java +++ b/src/main/java/com/github/sarxos/webcam/Webcam.java @@ -136,6 +136,7 @@ public void controllerUpdate(ControllerEvent ce) { private PlayerStarter starter = null; private Semaphore semaphore = new Semaphore(0, true); + private Dimension viewSize = null; /** * Webcam class. @@ -146,6 +147,14 @@ public Webcam(CaptureDeviceInfo device) { this.device = device; } + private VideoFormat createFormat(Dimension size) { + if (size == null) { + return getLargestVideoFormat(); + } else { + return getSizedVideoFormat(size); + } + } + /** * Open webcam. */ @@ -159,7 +168,7 @@ public synchronized void open() { LOG.debug("Opening webcam"); locator = device.getLocator(); - format = getVideoFormat(device); + format = createFormat(viewSize); converter = new BufferToImage(format); try { @@ -254,10 +263,83 @@ private Control getControl(String name) { * @return Webcam view size (picture size) in pixels. */ public Dimension getViewSize() { - if (!isOpen()) { - throw new RuntimeException("Webcam has to be open to get video size"); + if (viewSize == null) { + return getLargestVideoFormat().getSize(); + } + return viewSize; + } + + public Dimension[] getViewSizes() { + + List dimensions = new ArrayList(); + + Format[] formats = device.getFormats(); + for (Format format : formats) { + if ("RGB".equalsIgnoreCase(format.getEncoding())) { + VideoFormat rgb = (VideoFormat) format; + dimensions.add(rgb.getSize()); + } } - return format.getSize(); + return dimensions.toArray(new Dimension[dimensions.size()]); + } + + public void setViewSize(Dimension size) { + + // check if dimension is valid one + boolean ok = false; + Dimension[] sizes = getViewSizes(); + for (Dimension d : sizes) { + if (d.width == size.width && d.height == size.height) { + ok = true; + break; + } + } + + if (!ok) { + StringBuilder sb = new StringBuilder("Incorrect dimension ["); + sb.append(size.width).append("x").append(size.height).append("] "); + sb.append("possible ones are "); + for (Dimension d : sizes) { + sb.append("[").append(d.width).append("x").append(d.height).append("] "); + } + throw new IllegalArgumentException(sb.toString()); + } + + viewSize = size; + } + + public static void main(String[] args) { + for (Dimension d : Webcam.getDefault().getViewSizes()) { + System.out.println(d); + } + } + + /** + * Get video format for size. + * + * @param device device to get format from + * @param size specific size to search + * @return VideoFormat + */ + private VideoFormat getSizedVideoFormat(Dimension size) { + + Format[] formats = device.getFormats(); + VideoFormat format = null; + + for (Format f : formats) { + + if (!"RGB".equalsIgnoreCase(f.getEncoding()) || !(f instanceof VideoFormat)) { + continue; + } + + Dimension d = ((VideoFormat) f).getSize(); + if (d.width == size.width && d.height == size.height) { + format = (VideoFormat) f; + break; + } + } + + return format; } /** @@ -267,7 +349,7 @@ public Dimension getViewSize() { * @param device device to get video format for * @return Suitable video format */ - protected VideoFormat getVideoFormat(CaptureDeviceInfo device) { + private VideoFormat getLargestVideoFormat() { Format[] formats = device.getFormats(); VideoFormat format = null;