diff --git a/webcam-capture-drivers/driver-ipcam/pom.xml b/webcam-capture-drivers/driver-ipcam/pom.xml index c825d1fc..99c5f979 100644 --- a/webcam-capture-drivers/driver-ipcam/pom.xml +++ b/webcam-capture-drivers/driver-ipcam/pom.xml @@ -18,7 +18,6 @@ junit junit - 4.11 test diff --git a/webcam-capture-drivers/driver-ipcam/src/examples/java/JpegDasdingStudioExample.java b/webcam-capture-drivers/driver-ipcam/src/examples/java/JpegDasdingStudioExample.java index f560aa90..70249f6e 100644 --- a/webcam-capture-drivers/driver-ipcam/src/examples/java/JpegDasdingStudioExample.java +++ b/webcam-capture-drivers/driver-ipcam/src/examples/java/JpegDasdingStudioExample.java @@ -11,7 +11,6 @@ import com.github.sarxos.webcam.WebcamPanel; import com.github.sarxos.webcam.ds.ipcam.IpCamDriver; import com.github.sarxos.webcam.ds.ipcam.IpCamStorage; -import com.github.sarxos.webcam.log.WebcamLogConfigurator; /** @@ -41,8 +40,6 @@ public class JpegDasdingStudioExample { public static void main(String[] args) throws MalformedURLException { - WebcamLogConfigurator.configure("src/examples/resources/cameras.xml"); - JFrame f = new JFrame("Dasding Studio Live IP Cameras Demo"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new GridLayout(0, 3, 1, 1)); diff --git a/webcam-capture-examples/pom.xml b/webcam-capture-examples/pom.xml index 4491494b..deabe0cd 100644 --- a/webcam-capture-examples/pom.xml +++ b/webcam-capture-examples/pom.xml @@ -30,6 +30,7 @@ webcam-capture-swt-awt webcam-capture-video-recording webcam-capture-onejar + webcam-capture-websockets diff --git a/webcam-capture-examples/webcam-capture-websockets/README.md b/webcam-capture-examples/webcam-capture-websockets/README.md new file mode 100644 index 00000000..1ab921be --- /dev/null +++ b/webcam-capture-examples/webcam-capture-websockets/README.md @@ -0,0 +1,19 @@ +# Webcam WebSockets Example + +This example demonstrates how images feed can be transported over the WebSocket. + +## What Is This + + +## Screenshoots + + +## License + +Copyright (C) 2015 Bartosz Firyn + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/webcam-capture-examples/webcam-capture-websockets/pom.xml b/webcam-capture-examples/webcam-capture-websockets/pom.xml new file mode 100644 index 00000000..cbcd8640 --- /dev/null +++ b/webcam-capture-examples/webcam-capture-websockets/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + + + com.github.sarxos + webcam-capture-examples + 0.3.11-SNAPSHOT + + + webcam-capture-example-websockets + jar + + Webcam Capture - WebSockets Example + Example demonstrating how to transmit images over WebSockets from webcam server to the web application frontend + + + + com.github.sarxos + webcam-capture-driver-ipcam + ${project.version} + + + org.eclipse.jetty.aggregate + jetty-all + 9.2.7.v20150116 + + + org.slf4j + slf4j-simple + 1.7.2 + + + com.fasterxml.jackson.core + jackson-databind + 2.5.1 + + + + diff --git a/webcam-capture-examples/webcam-capture-websockets/src/etc/resources/screen.png b/webcam-capture-examples/webcam-capture-websockets/src/etc/resources/screen.png new file mode 100644 index 00000000..8f9d5633 Binary files /dev/null and b/webcam-capture-examples/webcam-capture-websockets/src/etc/resources/screen.png differ diff --git a/webcam-capture-examples/webcam-capture-websockets/src/etc/resources/screen2.png b/webcam-capture-examples/webcam-capture-websockets/src/etc/resources/screen2.png new file mode 100644 index 00000000..37daaa9d Binary files /dev/null and b/webcam-capture-examples/webcam-capture-websockets/src/etc/resources/screen2.png differ diff --git a/webcam-capture-examples/webcam-capture-websockets/src/main/html/index.html b/webcam-capture-examples/webcam-capture-websockets/src/main/html/index.html new file mode 100644 index 00000000..18d8581c --- /dev/null +++ b/webcam-capture-examples/webcam-capture-websockets/src/main/html/index.html @@ -0,0 +1,14 @@ + + + + + + + + +
+

webcam websocket transport example

+
+
+ + diff --git a/webcam-capture-examples/webcam-capture-websockets/src/main/html/style.css b/webcam-capture-examples/webcam-capture-websockets/src/main/html/style.css new file mode 100644 index 00000000..d97e2ba6 --- /dev/null +++ b/webcam-capture-examples/webcam-capture-websockets/src/main/html/style.css @@ -0,0 +1,16 @@ + +.wrapper { + margin-left: auto; + margin-right: auto; + width: 1084px; + text-align: center; +} + +img { + margin-left: 8px; + margin-bottom: 8px; + width: 348px; + height: 198px; + box-shadow: 0 0 16px #444; + border: 1px solid #fff; +} diff --git a/webcam-capture-examples/webcam-capture-websockets/src/main/html/webcam-capture-logo-small.jpg b/webcam-capture-examples/webcam-capture-websockets/src/main/html/webcam-capture-logo-small.jpg new file mode 100644 index 00000000..b723ef32 Binary files /dev/null and b/webcam-capture-examples/webcam-capture-websockets/src/main/html/webcam-capture-logo-small.jpg differ diff --git a/webcam-capture-examples/webcam-capture-websockets/src/main/html/ws.js b/webcam-capture-examples/webcam-capture-websockets/src/main/html/ws.js new file mode 100644 index 00000000..69551e5a --- /dev/null +++ b/webcam-capture-examples/webcam-capture-websockets/src/main/html/ws.js @@ -0,0 +1,50 @@ +$(document).ready(function() { + + var ws = new WebSocket("ws://127.0.0.1:8123/"); + + ws.onopen = function(e) { + if (typeof console !== 'undefined') { + console.info('WS open'); + } + }; + + ws.onmessage = function (e) { + + var data = JSON.parse(e.data), + type = data.type, + i = 0, + $webcams = $('#webcams'), + $img = null; + + if (typeof console !== 'undefined') { + console.info('WS message', type); + } + + if (type === 'list') { + for (i = 0; i < data.webcams.length; i += 1) { + $img = $("") + .attr("src", "webcam-capture-logo-small.jpg") + .attr("alt", data.webcams[i]) + .attr("name", data.webcams[i]); + $webcams.append($img) + } + } else if (type === 'image') { + $("img[name='" + data.webcam + "']") + .attr("src", "data:image/jpeg;base64," + data.image) + .trigger("change"); + } + }; + + ws.onclose = function() { + if (typeof console !== 'undefined') { + console.info('WS close'); + } + }; + + ws.onerror = function(err) { + if (typeof console !== 'undefined') { + console.info('WS error'); + } + }; +}); + diff --git a/webcam-capture-examples/webcam-capture-websockets/src/main/java/WebcamCache.java b/webcam-capture-examples/webcam-capture-websockets/src/main/java/WebcamCache.java new file mode 100644 index 00000000..0c0e6883 --- /dev/null +++ b/webcam-capture-examples/webcam-capture-websockets/src/main/java/WebcamCache.java @@ -0,0 +1,85 @@ +import java.awt.image.BufferedImage; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.github.sarxos.webcam.Webcam; +import com.github.sarxos.webcam.WebcamEvent; +import com.github.sarxos.webcam.WebcamListener; +import com.github.sarxos.webcam.WebcamUpdater; + + +public class WebcamCache implements WebcamUpdater.DelayCalculator, WebcamListener { + + /** + * How often images are updated on Dasding server. + */ + private static final long DELAY = 10000; + + /** + * Webcams list. + */ + private Map webcams = new HashMap(); + + /** + * WebSocket handlers. + */ + private List handlers = new ArrayList(); + + /** + * Static instance to make access easier. + */ + private static final WebcamCache CACHE = new WebcamCache(); + + public WebcamCache() { + for (Webcam webcam : Webcam.getWebcams()) { + webcam.addWebcamListener(this); + webcam.open(true, this); + webcams.put(webcam.getName(), webcam); + } + } + + @Override + public long calculateDelay(long snapshotDuration, double deviceFps) { + return Math.max(DELAY - snapshotDuration, 0); + } + + public static BufferedImage getImage(String name) { + return CACHE.webcams.get(name).getImage(); + } + + public static List getWebcamNames() { + return new ArrayList(CACHE.webcams.keySet()); + } + + @Override + public void webcamOpen(WebcamEvent we) { + // do nothing + } + + @Override + public void webcamClosed(WebcamEvent we) { + // do nothing + } + + @Override + public void webcamDisposed(WebcamEvent we) { + // do nothing + } + + @Override + public void webcamImageObtained(WebcamEvent we) { + for (WebcamWebSocketHandler handler : handlers) { + handler.newImage(we.getSource(), we.getImage()); + } + } + + public static void subscribe(WebcamWebSocketHandler handler) { + CACHE.handlers.add(handler); + } + + public static void unsubscribe(WebcamWebSocketHandler handler) { + CACHE.handlers.remove(handler); + } +} diff --git a/webcam-capture-examples/webcam-capture-websockets/src/main/java/WebcamWebSocketHandler.java b/webcam-capture-examples/webcam-capture-websockets/src/main/java/WebcamWebSocketHandler.java new file mode 100644 index 00000000..cf88c9d5 --- /dev/null +++ b/webcam-capture-examples/webcam-capture-websockets/src/main/java/WebcamWebSocketHandler.java @@ -0,0 +1,120 @@ +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.Base64; +import java.util.HashMap; +import java.util.Map; + +import javax.imageio.ImageIO; + +import org.eclipse.jetty.websocket.api.Session; +import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose; +import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; +import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError; +import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; +import org.eclipse.jetty.websocket.api.annotations.WebSocket; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.sarxos.webcam.Webcam; + + +@WebSocket +public class WebcamWebSocketHandler { + + private static final Logger LOG = LoggerFactory.getLogger(WebcamWebSocketHandler.class); + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private Session session; + + private void teardown() { + try { + session.close(); + session = null; + } finally { + WebcamCache.unsubscribe(this); + } + } + + private void setup(Session session) { + + this.session = session; + + Map message = new HashMap(); + message.put("type", "list"); + message.put("webcams", WebcamCache.getWebcamNames()); + + send(message); + + WebcamCache.subscribe(this); + } + + @OnWebSocketClose + public void onClose(int status, String reason) { + LOG.info("WebSocket closed, status = {}, reason = {}", status, reason); + teardown(); + } + + @OnWebSocketError + public void onError(Throwable t) { + LOG.error("WebSocket error", t); + teardown(); + } + + @OnWebSocketConnect + public void onConnect(Session session) { + LOG.info("WebSocket connect, from = {}", session.getRemoteAddress().getAddress()); + setup(session); + } + + @OnWebSocketMessage + public void onMessage(String message) { + LOG.info("WebSocket message, text = {}", message); + } + + public void newImage(Webcam webcam, BufferedImage image) { + + LOG.info("New image from {}", webcam); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try { + ImageIO.write(image, "JPG", baos); + } catch (IOException e) { + LOG.error(e.getMessage(), e); + } + + String base64 = null; + try { + base64 = new String(Base64.getEncoder().encode(baos.toByteArray()), "UTF8"); + } catch (UnsupportedEncodingException e) { + LOG.error(e.getMessage(), e); + } + + Map message = new HashMap(); + message.put("type", "image"); + message.put("webcam", webcam.getName()); + message.put("image", base64); + + send(message); + } + + private void send(String message) { + try { + session.getRemote().sendString(message); + } catch (IOException e) { + LOG.error("Exception when sending string", e); + } + } + + private void send(Object object) { + try { + send(MAPPER.writeValueAsString(object)); + } catch (JsonProcessingException e) { + LOG.error(e.getMessage(), e); + } + } +} \ No newline at end of file diff --git a/webcam-capture-examples/webcam-capture-websockets/src/main/java/WebcamWebSocketsExample.java b/webcam-capture-examples/webcam-capture-websockets/src/main/java/WebcamWebSocketsExample.java new file mode 100644 index 00000000..30ecd6e7 --- /dev/null +++ b/webcam-capture-examples/webcam-capture-websockets/src/main/java/WebcamWebSocketsExample.java @@ -0,0 +1,42 @@ +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.websocket.server.WebSocketHandler; +import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; + +import com.github.sarxos.webcam.Webcam; +import com.github.sarxos.webcam.ds.ipcam.IpCamDriver; +import com.github.sarxos.webcam.ds.ipcam.IpCamStorage; + + +/** + * This example demonstrates how webcam capture IP camera driver can be used + * with conjunction with websockets to feed data to the web application + * frontend. + * + * @author Bartosz Firyn (sarxos) + */ +public class WebcamWebSocketsExample { + + static { + Webcam.setDriver(new IpCamDriver(new IpCamStorage("src/main/resources/cameras.xml"))); + } + + public static void main(String[] args) throws Exception { + + for (String name : WebcamCache.getWebcamNames()) { + System.out.println("Will read webcam " + name); + } + + Server server = new Server(8123); + WebSocketHandler wsHandler = new WebSocketHandler() { + + @Override + public void configure(WebSocketServletFactory factory) { + factory.register(WebcamWebSocketHandler.class); + } + }; + + server.setHandler(wsHandler); + server.start(); + server.join(); + } +} diff --git a/webcam-capture-examples/webcam-capture-websockets/src/main/resources/cameras.xml b/webcam-capture-examples/webcam-capture-websockets/src/main/resources/cameras.xml new file mode 100644 index 00000000..8b3ee2c0 --- /dev/null +++ b/webcam-capture-examples/webcam-capture-websockets/src/main/resources/cameras.xml @@ -0,0 +1,9 @@ + + + + + + + + +