Skip to content

Commit

Permalink
Add utility method Webcam.getWebcamByName(..), closes #270
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Oct 19, 2014
1 parent a3e8de9 commit 2c908ca
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
27 changes: 27 additions & 0 deletions webcam-capture/src/main/java/com/github/sarxos/webcam/Webcam.java
Expand Up @@ -1194,6 +1194,9 @@ public WebcamLock getLock() {
return lock;
}

/**
* Shutdown webcam framework.
*/
public static void shutdown() {

// stop discovery service
Expand All @@ -1205,4 +1208,28 @@ public static void shutdown() {
// stop processor
WebcamProcessor.getInstance().shutdown();
}

/**
* Return webcam with given name or null if no device with given name has
* been found. Please note that specific webcam name may depend on the order
* it was connected to the USB port (e.g. /dev/video0 vs /dev/video1).
*
* @param name the webcam name
* @return Webcam with given name or null if not found
* @throws IllegalArgumentException when name is null
*/
public static Webcam getWebcamByName(String name) {

if (name == null) {
throw new IllegalArgumentException("Webcam name cannot be null");
}

for (Webcam webcam : getWebcams()) {
if (webcam.getName().equals(name)) {
return webcam;
}
}

return null;
}
}
Expand Up @@ -4,9 +4,8 @@
import java.awt.Image;
import java.util.List;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -166,4 +165,25 @@ public void test_registerDriver() {

Assert.assertSame(DummyDriver.class, driver.getClass());
}

@Test
public void test_GetWebcamByName() throws InstantiationException {
Webcam.setDriver(new DummyDriver());
for (Webcam webcam : Webcam.getWebcams()) {
Assert.assertEquals(webcam.getName(), Webcam.getWebcamByName(webcam.getName()).getName());
}
}

@Test
public void test_GetWebcamByNameWithNotExistingWebcamName() throws InstantiationException {
Webcam.setDriver(new DummyDriver());
Assert.assertNull(Webcam.getWebcamByName("DatCameraDoesNotExist"));
}

@Test(expected = IllegalArgumentException.class)
public void test_GetWebcamByNameWithNullArgument() throws InstantiationException {
Webcam.setDriver(new DummyDriver());
Webcam.getWebcamByName(null);
}

}

0 comments on commit 2c908ca

Please sign in to comment.