diff --git a/webcam-capture/src/example/java/com/github/sarxos/webcam/CustomResolutionExample.java b/webcam-capture/src/example/java/com/github/sarxos/webcam/CustomResolutionExample.java index 4d66e310..afc4c2f7 100644 --- a/webcam-capture/src/example/java/com/github/sarxos/webcam/CustomResolutionExample.java +++ b/webcam-capture/src/example/java/com/github/sarxos/webcam/CustomResolutionExample.java @@ -8,12 +8,19 @@ public class CustomResolutionExample { public static void main(String[] args) { + /** + * When you set custom resolutions you have to be sure that your webcam + * device will handle them! + */ + + //@formatter:off Dimension[] nonStandardResolutions = new Dimension[] { - WebcamResolution.PAL.getSize(), - WebcamResolution.HD720.getSize(), - new Dimension(2000, 1000), - new Dimension(1000, 500), + WebcamResolution.PAL.getSize(), + WebcamResolution.HD720.getSize(), + new Dimension(2000, 1000), + new Dimension(1000, 500), }; + //@formatter:on Webcam webcam = Webcam.getDefault(); webcam.setCustomViewSizes(nonStandardResolutions); diff --git a/webcam-capture/src/example/java/com/github/sarxos/webcam/TakePictureDifferentSizeExample.java b/webcam-capture/src/example/java/com/github/sarxos/webcam/TakePictureDifferentSizeExample.java index 2cad5c07..15fcf871 100644 --- a/webcam-capture/src/example/java/com/github/sarxos/webcam/TakePictureDifferentSizeExample.java +++ b/webcam-capture/src/example/java/com/github/sarxos/webcam/TakePictureDifferentSizeExample.java @@ -17,8 +17,8 @@ public class TakePictureDifferentSizeExample { public static void main(String[] args) throws IOException { Webcam webcam = Webcam.getDefault(); - webcam.open(); webcam.setViewSize(new Dimension(1024, 768)); + webcam.open(); ImageIO.write(webcam.getImage(), "PNG", new File("test.png")); webcam.close(); } diff --git a/webcam-capture/src/main/java/com/github/sarxos/webcam/Webcam.java b/webcam-capture/src/main/java/com/github/sarxos/webcam/Webcam.java index 87c67318..a014dd96 100644 --- a/webcam-capture/src/main/java/com/github/sarxos/webcam/Webcam.java +++ b/webcam-capture/src/main/java/com/github/sarxos/webcam/Webcam.java @@ -232,7 +232,9 @@ public boolean isOpen() { } /** - * @return Webcam view size (picture size) in pixels. + * Get current webcam resolution in pixels. + * + * @return Webcam resolution (picture size) in pixels. */ public synchronized Dimension getViewSize() { return device.getSize(); @@ -277,7 +279,10 @@ public Dimension[] getCustomViewSizes() { public synchronized void setViewSize(Dimension size) { if (size == null) { - throw new IllegalArgumentException("View size cannot be null!"); + throw new IllegalArgumentException("Resolution cannot be null!"); + } + if (open) { + throw new IllegalStateException("Cannot change resolution when webcam is open, please close it first"); } // check if dimension is valid diff --git a/webcam-capture/src/main/java/com/github/sarxos/webcam/ds/buildin/WebcamDefaultDevice.java b/webcam-capture/src/main/java/com/github/sarxos/webcam/ds/buildin/WebcamDefaultDevice.java index 4f6811ea..cfc26fe1 100644 --- a/webcam-capture/src/main/java/com/github/sarxos/webcam/ds/buildin/WebcamDefaultDevice.java +++ b/webcam-capture/src/main/java/com/github/sarxos/webcam/ds/buildin/WebcamDefaultDevice.java @@ -129,6 +129,9 @@ public Dimension getSize() { @Override public void setSize(Dimension size) { + if (open) { + throw new IllegalStateException("Cannot change resolution when webcam is open, please close it first"); + } this.size = size; }