-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.html
201 lines (133 loc) · 4.3 KB
/
stream.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<style>
body {
font-size: 20px;
font-family: "Helvetica Neue";
}
button{
padding: .5em;
font-weight: bold;
font-size: 1em;
color: black;
background: #555;
border:none;
border-radius: 6px;
margin: 1em;
}
#gallery img {
width:40%;
height:auto;
padding:5%;
}
</style>
<body style="background:black;">
<div style="position:absolute;top:0;left:0;right:30%; bottom: 86px;">
<img id="preview" style="width:100%; height:100%; object-fit: contain; object-position:center center;z-index:1;">
<img id="afterview" style="width:100%; height:100%; object-fit: contain; object-position:center center;z-index:2;display:none;background:white;">
<canvas id="test"></canvas>
</div>
<div style="position:absolute;left:0;right:30%;height:86px;bottom:0;background:#111;display:flex;align-content:center;">
<button onclick="actTakePicture()">Take picture</button>
<button onmousedown="SONY_actZoomInStart()" onmouseup="SONY_actZoomInStop()">Zoom in</button>
<button onmousedown="SONY_actZoomOutStart()" onmouseup="SONY_actZoomOutStop()">Zoom out</button>
</div>
<div id=gallery style="position:absolute;width:30%;right:0;top:0;bottom:0;background:#1a1a1a;display: flex; flex-wrap: wrap;align-items:flex-start;">
</div>
</body>
<script>
// elements
let img = document.getElementById("preview");
// SONY api
async function SONY(method, params){
const api = "http://10.0.0.1:10000/camera"
const rawResponse = await fetch(api, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
mode: 'no-cors',
body: JSON.stringify({"method": method, "params": params, "id": 1, "version": "1.0"})
});
return await rawResponse.json();
}
async function SONY_startLiveview(){
return (await SONY("startLiveview", [""]))["result"][0];
}
async function SONY_actZoomIn(){
await SONY("actZoom", ["in","1shot"]);
}
async function SONY_actZoomInStart(){
await SONY("actZoom", ["in","start"]);
}
async function SONY_actZoomInStop(){
await SONY("actZoom", ["in","stop"]);
}
async function SONY_actZoomOut(){
await SONY("actZoom", ["out","1shot"]);
}
async function SONY_actZoomOutStart(){
await SONY("actZoom", ["out","start"]);
}
async function SONY_actZoomOutStop(){
await SONY("actZoom", ["out","stop"]);
}
async function SONY_actTakePicture(){
return (await SONY("actTakePicture", [""]))["result"][0][0];
}
// async function SONY_startMovieRec(){
// return (await SONY("startMovieRec", [""]))["result"][0];
// }
//
// async function SONY_stopMovieRec(){
// return (await SONY("stopMovieRec", [""]))["result"][0];
// }
// Methods
async function actTakePicture(){
var img2 = document.getElementById("afterview");
let url = await SONY_actTakePicture();
img2.src = url;
img2.style.display = 'block';
setTimeout(function(){img2.style.display = 'none'; startLiveview();}, 3000);
var elem = document.createElement("img");
elem.src = url;
document.getElementById("gallery").appendChild(elem);
}
async function startLiveview(){
const liveview_endpoint = await SONY_startLiveview();
await fetch(liveview_endpoint,{ mode: 'no-cors' }).then(async function(response){
var running = true;
const reader = response.body.getReader();
var bytes = [];
var len = 0;
while(running) {
// parse common header
const commonHeader = await read(8); // ignore
if (commonHeader[0] != 255) {break;}
const num = commonHeader[3] * 1 + commonHeader[2] * 256;
// parse payload header
const payloadHeader = await read(128); // ignore
const jpeg_data_size = payloadHeader[6] * 1 + payloadHeader[5] * 256 + payloadHeader[4] * 65536; // reverse
const padding_size = payloadHeader[7];
const jpeg = await read(jpeg_data_size);
await read(padding_size);
//var blob = new Blob([Uint8Array.from(jpeg)], { type: "image/jpg" });
//var url = URL.createObjectURL(blob);
//img.src = URL.createObjectURL();
// URL.revokeObjectURL(blobcontainer.url);
img.src = 'data:image/jpeg;base64,' + btoa(String.fromCharCode.apply(null, Uint8Array.from(jpeg)))
}
async function read(n) {
while (len < n) {
const result = await reader.read();
result.value.forEach( function(v){
bytes.push(v);
});
len+=result.value.length;
if (result.done) {
running = false;
}
}
len -= n;
return bytes.splice(0, n);
}
});
}
startLiveview();
</script>