Skip to content

Commit da713fe

Browse files
committed
Small enhancements in WebSocket example, refs #390
1 parent 0f87892 commit da713fe

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
21
.wrapper {
32
margin-left: auto;
43
margin-right: auto;
54
width: 1084px;
65
text-align: center;
76
}
87

8+
.shadow {
9+
border: 1px solid #f00;
10+
transition: all .5s ease-in;
11+
}
12+
913
img {
1014
margin-left: 8px;
1115
margin-bottom: 8px;
1216
width: 348px;
1317
height: 198px;
1418
box-shadow: 0 0 16px #444;
1519
border: 1px solid #fff;
20+
transition: all 1s ease-in;
1621
}

webcam-capture-examples/webcam-capture-websockets/src/main/html/ws.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,15 @@ $(document).ready(function() {
2929
$webcams.append($img)
3030
}
3131
} else if (type === 'image') {
32-
$("img[name='" + data.webcam + "']")
32+
var $img = $("img[name='" + data.webcam + "']")
3333
.attr("src", "data:image/jpeg;base64," + data.image)
34+
.addClass('shadow')
3435
.trigger("change");
36+
setTimeout(function() {
37+
$img
38+
.removeClass('shadow')
39+
.trigger("change");
40+
}, 1000);
3541
}
3642
};
3743

webcam-capture-examples/webcam-capture-websockets/src/main/java/WebcamCache.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import java.util.List;
55
import java.util.Map;
66

7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
710
import com.github.sarxos.webcam.Webcam;
811
import com.github.sarxos.webcam.WebcamEvent;
912
import com.github.sarxos.webcam.WebcamListener;
@@ -12,6 +15,8 @@
1215

1316
public class WebcamCache implements WebcamUpdater.DelayCalculator, WebcamListener {
1417

18+
private static final Logger LOG = LoggerFactory.getLogger(WebcamCache.class);
19+
1520
/**
1621
* How often images are updated on Dasding server.
1722
*/
@@ -46,7 +51,13 @@ public long calculateDelay(long snapshotDuration, double deviceFps) {
4651
}
4752

4853
public static BufferedImage getImage(String name) {
49-
return CACHE.webcams.get(name).getImage();
54+
Webcam webcam = CACHE.webcams.get(name);
55+
try {
56+
return webcam.getImage();
57+
} catch (Exception e) {
58+
LOG.error("Exception when getting image from webcam", e);
59+
}
60+
return null;
5061
}
5162

5263
public static List<String> getWebcamNames() {

webcam-capture-examples/webcam-capture-websockets/src/main/java/WebcamWebSocketHandler.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ public void newImage(Webcam webcam, BufferedImage image) {
103103
}
104104

105105
private void send(String message) {
106-
try {
107-
session.getRemote().sendString(message);
108-
} catch (IOException e) {
109-
LOG.error("Exception when sending string", e);
106+
if (session.isOpen()) {
107+
try {
108+
session.getRemote().sendStringByFuture(message);
109+
} catch (Exception e) {
110+
LOG.error("Exception when sending string", e);
111+
}
110112
}
111113
}
112114

webcam-capture-examples/webcam-capture-websockets/src/main/java/WebcamWebSocketsExample.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import org.eclipse.jetty.server.Server;
22
import org.eclipse.jetty.websocket.server.WebSocketHandler;
33
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
46

57
import com.github.sarxos.webcam.Webcam;
68
import com.github.sarxos.webcam.ds.ipcam.IpCamDriver;
79
import com.github.sarxos.webcam.ds.ipcam.IpCamStorage;
810

911

1012
/**
11-
* This example demonstrates how webcam capture IP camera driver can be used
12-
* with conjunction with websockets to feed data to the web application
13-
* frontend.
13+
* This example demonstrates how webcam capture IP camera driver can be used with conjunction with
14+
* websockets to feed data to the web application frontend.
1415
*
1516
* @author Bartosz Firyn (sarxos)
1617
*/
@@ -20,10 +21,12 @@ public class WebcamWebSocketsExample {
2021
Webcam.setDriver(new IpCamDriver(new IpCamStorage("src/main/resources/cameras.xml")));
2122
}
2223

24+
private static final Logger LOG = LoggerFactory.getLogger(WebcamWebSocketsExample.class);
25+
2326
public static void main(String[] args) throws Exception {
2427

2528
for (String name : WebcamCache.getWebcamNames()) {
26-
System.out.println("Will read webcam " + name);
29+
LOG.info("Will read webcam {}", name);
2730
}
2831

2932
Server server = new Server(8123);

0 commit comments

Comments
 (0)