Skip to content

Commit

Permalink
Add possibility to support custom resolution
Browse files Browse the repository at this point in the history
fixes #7
  • Loading branch information
sarxos committed Dec 26, 2012
1 parent 0320c80 commit d53dbe4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
47 changes: 43 additions & 4 deletions webcam-capture/src/main/java/com/github/sarxos/webcam/Webcam.java
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.sarxos.webcam.ds.buildin.WebcamDefaultDevice;
import com.github.sarxos.webcam.ds.buildin.WebcamDefaultDriver;


Expand Down Expand Up @@ -58,6 +59,8 @@ public void run() {
*/
private List<WebcamListener> listeners = new ArrayList<WebcamListener>();

private List<Dimension> customSizes = new ArrayList<Dimension>();

private ShutdownHook hook = null;
private WebcamDevice device = null;
private boolean open = false;
Expand Down Expand Up @@ -192,6 +195,18 @@ public Dimension[] getViewSizes() {
return device.getSizes();
}

public void setCustomViewSizes(Dimension[] sizes) {
if (sizes == null) {
customSizes.clear();
return;
}
customSizes = Arrays.asList(sizes);
}

public Dimension[] getCustomViewSizes() {
return customSizes.toArray(new Dimension[customSizes.size()]);
}

public void setViewSize(Dimension size) {

if (size == null) {
Expand All @@ -200,20 +215,33 @@ public void setViewSize(Dimension size) {

// check if dimension is valid

Dimension[] predefined = getViewSizes();
Dimension[] custom = getCustomViewSizes();

boolean ok = false;
Dimension[] sizes = getViewSizes();
for (Dimension d : sizes) {
for (Dimension d : predefined) {
if (d.width == size.width && d.height == size.height) {
ok = true;
break;
}
}
if (!ok) {
for (Dimension d : custom) {
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) {
for (Dimension d : predefined) {
sb.append("[").append(d.width).append("x").append(d.height).append("] ");
}
for (Dimension d : custom) {
sb.append("[").append(d.width).append("x").append(d.height).append("] ");
}
throw new IllegalArgumentException(sb.toString());
Expand Down Expand Up @@ -415,10 +443,21 @@ public static void registerDriver(String clazzName) {
DRIVERS_LIST.add(clazzName);
}

protected WebcamDevice getDevice() {
/**
* Return underlying webcam device. Depending on the driver used to discover
* devices, this method can return instances of different class. By default
* {@link WebcamDefaultDevice} is returned when no external driver is used.
*
* @return Underlying webcam device instance
*/
public WebcamDevice getDevice() {
return device;
}

/**
* Completely dispose capture device. After this operation webcam cannot be
* used any more and reinstantiation is required.
*/
protected void dispose() {
device.close();
device.dispose();
Expand Down
Expand Up @@ -6,15 +6,18 @@
import javax.imageio.ImageIO;



public class WebcamUtils {

public static final void capture(Webcam webcam, String filename) {
public static final void capture(Webcam webcam, File file) {
try {
ImageIO.write(webcam.getImage(), "PNG", new File(filename + ".png"));
ImageIO.write(webcam.getImage(), "PNG", file);
} catch (IOException e) {
e.printStackTrace();
}
}

public static final void capture(Webcam webcam, String filename) {
capture(webcam, new File(filename + ".png"));
}

}
Expand Up @@ -32,7 +32,14 @@ public class WebcamDefaultDevice implements WebcamDevice {
public static final Dimension SIZE_CIF = new Dimension(352, 288);
public static final Dimension SIZE_HVGA = new Dimension(480, 400);
public static final Dimension SIZE_VGA = new Dimension(640, 480);
public static final Dimension SIZE_PAL = new Dimension(768, 576);
public static final Dimension SIZE_SVGA = new Dimension(800, 600);
public static final Dimension SIZE_XGA = new Dimension(1024, 768);
public static final Dimension SIZE_HD720 = new Dimension(1280, 720);
public static final Dimension SIZE_WXGA = new Dimension(1280, 768);
public static final Dimension SIZE_SXGA = new Dimension(1280, 1024);
public static final Dimension SIZE_UXGA = new Dimension(1600, 1200);
public static final Dimension SIZE_QXGA = new Dimension(2048, 1536);

/**
* Logger.
Expand Down Expand Up @@ -96,6 +103,7 @@ public class WebcamDefaultDevice implements WebcamDevice {
private Dimension size = null;
private ComponentSampleModel sampleModel = null;
private ColorModel colorModel = null;
private boolean failOnSizeMismatch = false;

private volatile boolean open = false;
private volatile boolean opening = false;
Expand Down Expand Up @@ -197,6 +205,11 @@ public void open() {
int h2 = size2.height;

if (w1 != w2 || h1 != h2) {

if (failOnSizeMismatch) {
throw new WebcamException(String.format("Different size obtained vs requested - [%dx%d] vs [%dx%d]", w1, h1, w2, h2));
}

LOG.warn("Different size obtained vs requested - [{}x{}] vs [{}x{}]. Setting correct one. New size is [{}x{}]", new Object[] { w1, h1, w2, h2, w2, h2 });
size = new Dimension(w2, h2);
}
Expand All @@ -222,7 +235,7 @@ public void open() {

} finally {

// notify all threads also waiting for open
// notify all threads which are also waiting for device to be open
synchronized (device) {
device.notifyAll();
}
Expand All @@ -247,4 +260,13 @@ public void dispose() {
disposed = true;
}

/**
* Determines if device should fail when requested image size is different
* than actually received.
*
* @param fail the fail on size mismatch flag, true or false
*/
public void setFailOnSizeMismatch(boolean fail) {
this.failOnSizeMismatch = fail;
}
}

0 comments on commit d53dbe4

Please sign in to comment.