Skip to content

Commit

Permalink
Enhance IP camera driver, update few dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Jan 15, 2013
1 parent 929bed1 commit 6a42c16
Show file tree
Hide file tree
Showing 14 changed files with 315 additions and 62 deletions.
33 changes: 6 additions & 27 deletions webcam-capture-drivers/webcam-capture-driver-ipcam/.classpath
@@ -1,32 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="src" path="src/examples/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/examples/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
9 changes: 7 additions & 2 deletions webcam-capture-drivers/webcam-capture-driver-ipcam/pom.xml
Expand Up @@ -77,12 +77,17 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.2</version>
<version>4.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.2.2</version>
<version>4.2.3</version>
</dependency>
<dependency>

This comment has been minimized.

Copy link
@sarxos

sarxos Jan 15, 2013

Author Owner

remove this dependency

<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.9</version>
</dependency>
</dependencies>

Expand Down
@@ -1,8 +1,12 @@
package com.github.sarxos.webcam.ds.ipcam;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JFrame;

import com.github.sarxos.webcam.Webcam;
Expand All @@ -17,23 +21,28 @@ public static void main(String[] args) throws MalformedURLException {
// available to be viewed online. Here in this example we are creating
// IP camera device working in PULL mode to request static JPEG images.

String address = "http://www.dasding.de/ext/webcam/webcam770.php?cam=1";
IpCamDevice livecam = new IpCamDevice("dasding", new URL(address), IpCamMode.PULL);
JFrame f = new JFrame("Dasding Studio Live IP Cameras");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(0, 3, 1, 1));

List<WebcamPanel> panels = new ArrayList<WebcamPanel>();

IpCamDriver driver = new IpCamDriver();
driver.register(livecam);
for (Webcam webcam : Webcam.getWebcams()) {

Webcam.setDriver(driver);
WebcamPanel panel = new WebcamPanel(webcam, new Dimension(256, 144), false);
panel.setFillArea(true);
panel.setFPS(0.2); // 0.2 FPS = 1 frame per 5 seconds
panel.setBorder(BorderFactory.createEmptyBorder());

WebcamPanel panel = new WebcamPanel(Webcam.getDefault());
panel.setFPS(0.2); // 1 frame per 5 seconds
f.add(panel);
panels.add(panel);
}

JFrame f = new JFrame("Dasding Studio Live IP Camera");
f.add(panel);
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

for (WebcamPanel panel : panels) {
panel.start();
}
}

}
@@ -0,0 +1,8 @@
<storage>
<ipcam name="Dasding 1" url="http://www.dasding.de/ext/webcam/webcam770.php?cam=1" />
<ipcam name="Dasding 2" url="http://www.dasding.de/ext/webcam/webcam770.php?cam=2" />
<ipcam name="Dasding 4" url="http://www.dasding.de/ext/webcam/webcam770.php?cam=4" />
<ipcam name="Dasding 6" url="http://www.dasding.de/ext/webcam/webcam770.php?cam=6" />
<ipcam name="Dasding 7" url="http://www.dasding.de/ext/webcam/webcam770.php?cam=7" />
<ipcam name="Dasding 10" url="http://www.dasding.de/ext/webcam/webcam770.php?cam=10" />
</storage>
Expand Up @@ -245,14 +245,21 @@ public Dimension[] getSizes() {
open();
}

BufferedImage img = getImage();
int w = img.getWidth();
int h = img.getHeight();

sizes = new Dimension[] { new Dimension(w, h) };
int attempts = 0;
do {
BufferedImage img = getImage();
if (img != null) {
sizes = new Dimension[] { new Dimension(img.getWidth(), img.getHeight()) };
break;
}
} while (attempts++ < 5);

close();

if (sizes == null) {
throw new WebcamException("Cannot get initial image from IP camera device " + getName());
}

return sizes;
}

Expand Down
Expand Up @@ -2,9 +2,8 @@

import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.github.sarxos.webcam.WebcamDevice;
import com.github.sarxos.webcam.WebcamException;
Expand All @@ -20,7 +19,7 @@ public class IpCamDeviceRegistry {
/**
* Contains IP cameras.
*/
private static final Set<IpCamDevice> DEVICES = new HashSet<IpCamDevice>();
private static final List<IpCamDevice> DEVICES = new ArrayList<IpCamDevice>();

/**
* Register IP camera.
Expand All @@ -41,6 +40,10 @@ public static void register(String name, URL url, IpCamMode mode) {
register(new IpCamDevice(name, url, mode));
}

public static void register(String name, URL url, IpCamMode mode, IpCamAuth auth) {
register(new IpCamDevice(name, url, mode, auth));
}

public static boolean isRegistered(IpCamDevice ipcam) {
for (IpCamDevice d : DEVICES) {
if (d.getName().equals(ipcam.getName())) {
Expand Down Expand Up @@ -83,6 +86,6 @@ public static void unregister(IpCamDevice ipcam) {
* @return Collection of registered IP cameras
*/
public static List<IpCamDevice> getIpCameras() {
return new ArrayList<IpCamDevice>(DEVICES);
return Collections.unmodifiableList(DEVICES);
}
}
Expand Up @@ -14,6 +14,16 @@
*/
public class IpCamDriver implements WebcamDriver {

public IpCamDriver() {
this(null);
}

public IpCamDriver(IpCamStorage storage) {
if (storage != null) {
storage.open();
}
}

@Override
public List<WebcamDevice> getDevices() {
return Collections.unmodifiableList((List<? extends WebcamDevice>) IpCamDeviceRegistry.getIpCameras());
Expand Down
@@ -1,20 +1,27 @@
package com.github.sarxos.webcam.ds.ipcam;

import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlEnumValue;


/**
* How to obtain new images from IP cameras.
*
* @author Bartosz Firyn (SarXos)
*/
@XmlEnum
public enum IpCamMode {

/**
* Device will pull image from IP camera.
*/
@XmlEnumValue("pull")
PULL,

/**
* IP camera HTTP server will push new image to the device.
*/
@XmlEnumValue("pull")
PUSH,

}
@@ -0,0 +1,73 @@
package com.github.sarxos.webcam.ds.ipcam;

import java.io.File;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import com.github.sarxos.webcam.WebcamException;
import com.github.sarxos.webcam.ds.ipcam.impl.IpCamDescriptor;


@XmlRootElement(name = "storage")
@XmlAccessorType(XmlAccessType.FIELD)
public class IpCamStorage {

private static final Class<?>[] CLASSES = new Class<?>[] {
IpCamStorage.class,
IpCamDescriptor.class,
};

private static final JAXBContext CTX;
static {
JAXBContext c = null;
try {
c = JAXBContext.newInstance(CLASSES);
} catch (JAXBException e) {
throw new RuntimeException(e);
} finally {
CTX = c;
}
}

@XmlElement(name = "ipcam")
private List<IpCamDescriptor> descriptors = null;

private transient File file = null;

protected IpCamStorage() {
}

public IpCamStorage(String file) {
this(new File(file));
}

public IpCamStorage(File file) {
this.file = file;
}

protected List<IpCamDescriptor> getDescriptors() {
return descriptors;
}

public void open() {

IpCamStorage storage = null;
try {
Unmarshaller unmarshaller = CTX.createUnmarshaller();
storage = (IpCamStorage) unmarshaller.unmarshal(file);
} catch (JAXBException e) {
throw new WebcamException(e);
}

for (IpCamDescriptor d : storage.getDescriptors()) {
IpCamDeviceRegistry.register(d.getName(), d.getURL(), d.getMode(), d.getAuth());
}
}
}
@@ -0,0 +1,78 @@
package com.github.sarxos.webcam.ds.ipcam.impl;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

import com.github.sarxos.webcam.WebcamException;
import com.github.sarxos.webcam.ds.ipcam.IpCamAuth;
import com.github.sarxos.webcam.ds.ipcam.IpCamMode;


@XmlAccessorType(XmlAccessType.FIELD)
public class IpCamDescriptor {

@XmlAccessorType(XmlAccessType.FIELD)
protected static class AuthParams {

@XmlAttribute
private String user = null;

@XmlAttribute
private String password = null;

public String getUser() {
return user;
}

public String getPassword() {
return password;
}
}

@XmlAttribute
private String name = null;

@XmlAttribute(name = "url")
private String urlString = null;

private transient URL url = null;

@XmlAttribute
private IpCamMode mode = IpCamMode.PULL;

@XmlElement(name = "auth")
private AuthParams authParams = null;

private transient IpCamAuth auth = null;

public String getName() {
return name;
}

public URL getURL() {
if (urlString != null && url == null) {
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
throw new WebcamException(e);
}
}
return url;
}

public IpCamMode getMode() {
return mode;
}

public IpCamAuth getAuth() {
if (authParams != null && auth == null) {
auth = new IpCamAuth(authParams.getUser(), authParams.getPassword());
}
return auth;
}
}
Expand Up @@ -11,7 +11,6 @@
<logger name="org.apache.http.headers" level="DEBUG" />
<logger name="com.github.sarxos.webcam.ds.ipcam" level="debug" />


<root>
<level value="info" />
<appender-ref ref="ConsoleAppender" />
Expand Down

0 comments on commit 6a42c16

Please sign in to comment.