From 2c908ca37d2492d06af853e4d625ef2762fbb279 Mon Sep 17 00:00:00 2001 From: Bartosz Firyn Date: Sun, 19 Oct 2014 15:56:23 +0200 Subject: [PATCH] Add utility method Webcam.getWebcamByName(..), closes #270 --- .../java/com/github/sarxos/webcam/Webcam.java | 27 +++++++++++++++++++ .../com/github/sarxos/webcam/WebcamTest.java | 24 +++++++++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) 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 f1f12fd0..c5b342cb 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 @@ -1194,6 +1194,9 @@ public WebcamLock getLock() { return lock; } + /** + * Shutdown webcam framework. + */ public static void shutdown() { // stop discovery service @@ -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; + } } diff --git a/webcam-capture/src/test/java/com/github/sarxos/webcam/WebcamTest.java b/webcam-capture/src/test/java/com/github/sarxos/webcam/WebcamTest.java index e7898cd9..e8d041a9 100644 --- a/webcam-capture/src/test/java/com/github/sarxos/webcam/WebcamTest.java +++ b/webcam-capture/src/test/java/com/github/sarxos/webcam/WebcamTest.java @@ -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; @@ -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); + } + }