Uses 1113K of libraries. Click here for Pure Java version (2751L/15K).
1 | !7 |
2 | |
3 | cm WebCamDemo > DynPrintLog {
|
4 | !include #1029545 // API for Eleu |
5 | |
6 | O html(IWebRequest request) {
|
7 | //ret "URI: " + request.uri(); |
8 | ret hsansserif() + hfullcenter(p("WebCam Preview")
|
9 | + hfulltag video("", autoplay := true, id := "webcamPreview",
|
10 | style := "width: 600px; height: 400px; border: 5px solid red;") |
11 | + tag table(tr( |
12 | td(p("Snapshot 1 " + hbutton("Grab", onclick := "snapshot(document.getElementById('snapshotCanvas1'))"))
|
13 | + hfulltag canvas("", id := "snapshotCanvas1", width := 300, height := 200, style := "border: 3px solid green;"))
|
14 | + td(p("Snapshot 2 " + hbutton("Grab", onclick := "snapshot(document.getElementById('snapshotCanvas2'))"))
|
15 | + hfulltag canvas("", id := "snapshotCanvas2", width := 300, height := 200, style := "border: 3px solid green;"))
|
16 | + td(p("Diff snapshots " + hbutton("Perform", onclick := "analysis()"))
|
17 | + hfulltag canvas("", id := "analysisCanvas", width := 300, height := 200, style := "border: 3px solid green;"))))
|
18 | ) + hscript([[ |
19 | var video = document.querySelector("#webcamPreview");
|
20 | |
21 | if (navigator.mediaDevices.getUserMedia) {
|
22 | navigator.mediaDevices.getUserMedia({ video: true })
|
23 | .then(function (stream) {
|
24 | video.srcObject = stream; |
25 | getVideoSize(); |
26 | }) |
27 | .catch(function (err0r) {
|
28 | console.log("Something went wrong: " + err0r);
|
29 | }); |
30 | }; |
31 | |
32 | async function getVideoSize() {
|
33 | await new Promise(resolve => video.onloadedmetadata = resolve); |
34 | console.log(`video size: ${video.videoWidth}x${video.videoHeight}`);
|
35 | } |
36 | |
37 | var snapshotW, snapshotH; |
38 | |
39 | function snapshot(canvas) {
|
40 | var w = video.videoWidth, h = video.videoHeight; |
41 | var w2 = canvas.width, h2 = canvas.height; |
42 | if (w/h > w2/h2) |
43 | h2 = Math.round(h*w2/w); |
44 | else |
45 | w2 = Math.round(w*h2/h); |
46 | snapshotW = w2; snapshotH = h2; |
47 | canvas.getContext("2d").drawImage(video, 0, 0, w2, h2);
|
48 | canvas.grabbed = true; |
49 | |
50 | avgBrightness(canvas); |
51 | } |
52 | |
53 | function avgBrightness(canvas) {
|
54 | var imageData = canvas.getContext("2d").getImageData(0, 0, snapshotW, snapshotH);
|
55 | var pixels = imageData.data; |
56 | |
57 | var sum = 0; |
58 | for (var i = 0, il = pixels.length; i < il; i += 4) {
|
59 | sum += pixels[i]+pixels[i+1]+pixels[i+2]; |
60 | } |
61 | console.log("Brightness: " + sum/(snapshotW*snapshotH*255*3)*100);
|
62 | } |
63 | |
64 | function analysis() {
|
65 | if (!document.getElementById('snapshotCanvas1').grabbed)
|
66 | snapshot(document.getElementById('snapshotCanvas1'));
|
67 | |
68 | snapshot(document.getElementById('snapshotCanvas2'));
|
69 | |
70 | var canvas1 = document.getElementById("snapshotCanvas1");
|
71 | var canvasCtx1 = canvas1.getContext("2d");
|
72 | var canvas2 = document.getElementById("snapshotCanvas2");
|
73 | var canvasCtx2 = canvas2.getContext("2d");
|
74 | var canvasOut = document.getElementById("analysisCanvas");
|
75 | var canvasCtxOut = canvasOut.getContext("2d");
|
76 | |
77 | var size = snapshotW*snapshotH; |
78 | var pixels = new Uint8ClampedArray(size*4); |
79 | |
80 | var pixels1 = canvasCtx1.getImageData(0, 0, snapshotW, snapshotH).data; |
81 | var pixels2 = canvasCtx2.getImageData(0, 0, snapshotW, snapshotH).data; |
82 | |
83 | for (var i = 0; i < size*4; i++) {
|
84 | if ((i & 3) == 3) |
85 | pixels[i] = 255; // alpha |
86 | else |
87 | pixels[i] = 128+(pixels2[i]-pixels1[i])*0.5; |
88 | } |
89 | |
90 | var palette = canvasCtxOut.getImageData(0, 0, snapshotW, snapshotH); |
91 | palette.data.set(pixels); |
92 | canvasCtxOut.putImageData(palette, 0, 0); |
93 | } |
94 | ]]); |
95 | } |
96 | } |
download show line numbers debug dex old transpilations
Travelled to 5 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1029807 |
| Snippet name: | HTML WebCam Demo [LIVE at masterbot.online] |
| Eternal ID of this version: | #1029807/45 |
| Text MD5: | 34d9d4d8f2f303837667a6f8506370c4 |
| Transpilation MD5: | e9519c0a76c0fbcc117a7c3baaa43ab4 |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX source code (Dynamic Module) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2021-07-13 02:19:35 |
| Source code size: | 3984 bytes / 96 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 449 / 29234 |
| Version history: | 44 change(s) |
| Referenced in: | [show references] |