Skip to content

Commit

Permalink
Disallow resolution change when webcam is open
Browse files Browse the repository at this point in the history
fixes #32
  • Loading branch information
sarxos committed Feb 5, 2013
1 parent 3cf7798 commit 0a2bb20
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
Expand Up @@ -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);
Expand Down
Expand Up @@ -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();
}
Expand Down
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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;
}

Expand Down

0 comments on commit 0a2bb20

Please sign in to comment.