Skip to content

Commit

Permalink
Add more null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Nov 18, 2012
1 parent 087fc07 commit 840ba46
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
78 changes: 64 additions & 14 deletions webcam-capture/src/main/java/com/github/sarxos/webcam/Webcam.java
Expand Up @@ -67,9 +67,33 @@ public void run() {
* @param device - device to be used as webcam
*/
protected Webcam(WebcamDevice device) {
if (device == null) {
throw new IllegalArgumentException("Webcam device cannot be null");
}
this.device = device;
}

/**
* Check if size is set up.
*/
private void ensureSize() {

Dimension size = device.getSize();
if (size == null) {

Dimension[] sizes = device.getSizes();

if (sizes == null) {
throw new WebcamException("Sizes array from driver cannot be null!");
}
if (sizes.length == 0) {
throw new WebcamException("Sizes array from driver is empty, cannot choose image size");
}

device.setSize(sizes[0]);
}
}

/**
* Open webcam.
*/
Expand All @@ -83,9 +107,7 @@ public synchronized void open() {
LOG.info("Opening webcam " + getName());
}

if (device.getSize() == null) {
device.setSize(device.getSizes()[0]);
}
ensureSize();

device.open();
open = true;
Expand All @@ -99,7 +121,7 @@ public synchronized void open() {
try {
l.webcamOpen(we);
} catch (Exception e) {
e.printStackTrace();
LOG.error("Notify webcam open, exception when calling {}", WebcamListener.class.getSimpleName(), e);
}
}
}
Expand Down Expand Up @@ -127,7 +149,7 @@ private void close0() {
}

if (LOG.isInfoEnabled()) {
LOG.info("Closing webcam " + getName());
LOG.info("Closing {}", getName());
}

device.close();
Expand All @@ -138,7 +160,7 @@ private void close0() {
try {
l.webcamClosed(we);
} catch (Exception e) {
e.printStackTrace();
LOG.error("Notify webcam closed, exception when calling {}", WebcamListener.class.getSimpleName(), e);
}
}
}
Expand Down Expand Up @@ -171,7 +193,12 @@ public Dimension[] getViewSizes() {

public void setViewSize(Dimension size) {

// check if dimension is valid one
if (size == null) {
throw new IllegalArgumentException("View size cannot be null!");
}

// check if dimension is valid

boolean ok = false;
Dimension[] sizes = getViewSizes();
for (Dimension d : sizes) {
Expand All @@ -192,7 +219,7 @@ public void setViewSize(Dimension size) {
}

if (LOG.isDebugEnabled()) {
LOG.debug("Setting new view size " + size);
LOG.debug("Setting new view size {} x {}", size.width, size.height);
}

device.setSize(size);
Expand All @@ -205,6 +232,7 @@ public void setViewSize(Dimension size) {
*/
public synchronized BufferedImage getImage() {
if (!open) {
LOG.debug("Try to get image on closed webcam, opening it automatically");
open();
}
return device.getImage();
Expand All @@ -229,8 +257,7 @@ public static List<Webcam> getWebcams() {
}

for (WebcamDevice device : driver.getDevices()) {
Webcam webcam = new Webcam(device);
webcams.add(webcam);
webcams.add(new Webcam(device));
}

if (LOG.isInfoEnabled()) {
Expand Down Expand Up @@ -271,17 +298,20 @@ public String getName() {

@Override
public String toString() {
return "webcam:" + getName();
return String.format("Webcam %s", getName());
}

/**
* Add webcam listener.
*
* @param l a listener to add
* @param l the listener to be added
*/
public void addWebcamListener(WebcamListener l) {
public boolean addWebcamListener(WebcamListener l) {
if (l == null) {
throw new IllegalArgumentException("Webcam listener cannot be null!");
}
synchronized (listeners) {
listeners.add(l);
return listeners.add(l);
}
}

Expand All @@ -294,6 +324,12 @@ public WebcamListener[] getWebcamListeners() {
}
}

public boolean removeWebcamListener(WebcamListener l) {
synchronized (listeners) {
return listeners.remove(l);
}
}

/**
* @return Data source currently used by webcam
*/
Expand All @@ -307,6 +343,9 @@ public static WebcamDriver getDriver() {
* @param driver new video driver to use (e.g. Civil, JFM, FMJ, QTJ, etc)
*/
public static void setDriver(WebcamDriver driver) {
if (driver == null) {
throw new IllegalArgumentException("Webcam driver cannot be null!");
}
resetDriver();
Webcam.driver = driver;
}
Expand All @@ -322,13 +361,18 @@ public static void setDriver(Class<? extends WebcamDriver> driverClass) {

resetDriver();

if (driverClass == null) {
throw new IllegalArgumentException("Webcam driver class cannot be null!");
}

try {
driver = driverClass.newInstance();
} catch (InstantiationException e) {
throw new WebcamException(e);
} catch (IllegalAccessException e) {
throw new WebcamException(e);
}

}

public static void resetDriver() {
Expand All @@ -351,6 +395,9 @@ public static void resetDriver() {
* @param clazz webcam video driver class
*/
public static void registerDriver(Class<? extends WebcamDriver> clazz) {
if (clazz == null) {
throw new IllegalArgumentException("Webcam driver class to register cannot be null!");
}
DRIVERS_CLASS_LIST.add(clazz);
registerDriver(clazz.getCanonicalName());
}
Expand All @@ -361,6 +408,9 @@ public static void registerDriver(Class<? extends WebcamDriver> clazz) {
* @param clazzName webcam video driver class name
*/
public static void registerDriver(String clazzName) {
if (clazzName == null) {
throw new IllegalArgumentException("Webcam driver class name to register cannot be null!");
}
DRIVERS_LIST.add(clazzName);
}

Expand Down
Expand Up @@ -31,7 +31,7 @@ public class DefaultDriver implements WebcamDriver {
public List<WebcamDevice> getDevices() {

if (grabber == null) {
LOG.debug("Creating grabber for driver");
LOG.debug("Creating grabber");
grabber = new OpenIMAJGrabber();
}

Expand All @@ -43,12 +43,18 @@ public List<WebcamDevice> getDevices() {

DeviceList list = grabber.getVideoDevices().get();
for (Device device : list.asArrayList()) {

if (device == null) {
LOG.error("Native discovery problem! Null device detected by grabber!");
continue;
}

devices.add(new DefaultDevice(device));
}

if (LOG.isDebugEnabled()) {
for (WebcamDevice device : devices) {
LOG.debug("Found device " + device);
LOG.debug("Found device {}", device);
}
}
}
Expand Down

0 comments on commit 840ba46

Please sign in to comment.