-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcamera.tsx
372 lines (362 loc) · 11.1 KB
/
camera.tsx
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import { PointerEvent, useCallback, useEffect, useRef, useState } from 'react'
import exampleImg from '../assets/example.jpg'
import { threshold, useStorage } from './utils'
const ratio = 16 / 9
const maxZoomLevel = 10
export function Camera({
onCapture,
}: {
onCapture(canvas: HTMLCanvasElement): void
}) {
const [dim, setDim] = useState({ width: 240, height: 320 })
const [ready, setReady] = useState(false)
const videoRef = useRef<HTMLVideoElement>(null)
const containerRef = useRef<HTMLDivElement>(null)
const [exampleOn, setExampleOn] = useState(false)
const [cameraSelectOn, setCameraSelectOn] = useState(false)
const [furthurHelpOn, setFurthurHelpOn] = useState(false)
const [devices, setDevices] = useState<MediaDeviceInfo[]>()
const [deviceId, setDeviceId] = useState<string>()
const [nativeResolutionOn, setNativeResolutionOn] = useStorage(
'nativeResolutionOn',
'1'
)
const updateDimension = useCallback(() => {
if (videoRef.current && containerRef.current) {
const width = containerRef.current.clientWidth
const height = width / ratio
videoRef.current.width = width
videoRef.current.height = height
setDim({ width, height })
}
}, [setDim])
useEffect(() => {
window.addEventListener('resize', updateDimension)
return () => {
window.removeEventListener('resize', updateDimension)
}
}, [updateDimension])
// Get the video stream from the camera
useEffect(() => {
try {
navigator.mediaDevices
.getUserMedia({
audio: false,
video: {
...(nativeResolutionOn === '1'
? { width: { ideal: 7680 }, height: { ideal: 4320 } }
: undefined),
...(deviceId
? { deviceId: { exact: deviceId } }
: { facingMode: 'environment' }),
},
})
.then(mediaStream => {
videoRef.current!.srcObject = mediaStream
})
} catch {
throw new Error('WebRTC not supported')
}
}, [deviceId, nativeResolutionOn])
// Get the list of available video input devices.
// Only used when the user clicks "Camera not working?"
useEffect(() => {
const getDevices = async () => {
try {
const deviceInfos = await navigator.mediaDevices.enumerateDevices()
setDevices(deviceInfos.filter(({ kind }) => kind === 'videoinput'))
} catch {
throw new Error('WebRTC not supported')
}
}
cameraSelectOn && getDevices()
}, [cameraSelectOn])
// Below for pinch to zoom
const pointEventsRef = useRef<PointerEvent<HTMLDivElement>[]>([])
const pinchStartInfoRef = useRef({ distance: 1, scale: 1 })
const [_scale, setScale] = useStorage('zoomScale', 1)
const scale = parseFloat(_scale)
const onPointerUp = (e: PointerEvent<HTMLDivElement>) => {
pointEventsRef.current = pointEventsRef.current.filter(
prev => prev.pointerId !== e.pointerId
)
}
const getDistance = () => {
const x1 = pointEventsRef.current[0].clientX
const y1 = pointEventsRef.current[0].clientY
const x2 = pointEventsRef.current[1].clientX
const y2 = pointEventsRef.current[1].clientY
return Math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
}
return (
<>
<div
style={{
margin: 16,
border: '1px solid #ff606060',
flexShrink: 0,
}}
>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
padding: '0 4px',
color: '#cfed57',
}}
>
<div>CODE MATRIX</div>
<div>SEQUENCE</div>
</div>
<div
ref={containerRef}
style={{
position: 'relative',
// Fix a weird bug that the video goes outside of the <video> frame on android emulator
// Also necessary for pinch to zoom
overflow: 'hidden',
// For pinch to zoom
touchAction: 'none',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
}}
onPointerDown={e => {
pointEventsRef.current = [...pointEventsRef.current, e]
// 2 finger pinch zoom starts
if (pointEventsRef.current.length === 2) {
pinchStartInfoRef.current = {
distance: getDistance(),
scale,
}
}
}}
onPointerMove={e => {
pointEventsRef.current = pointEventsRef.current.map(prev =>
prev.pointerId === e.pointerId ? e : prev
)
if (pointEventsRef.current.length === 2) {
const zoomRatioChange =
getDistance() / pinchStartInfoRef.current.distance
setScale(
Math.max(
Math.min(
pinchStartInfoRef.current.scale * zoomRatioChange,
maxZoomLevel
),
1
)
)
}
}}
onPointerUp={onPointerUp}
onPointerCancel={onPointerUp}
onPointerLeave={onPointerUp}
onPointerOut={onPointerUp}
>
<video
ref={videoRef}
playsInline
onCanPlay={() => {
videoRef?.current?.play()
setReady(true)
updateDimension()
}}
style={{
objectFit: 'cover',
transform: `scale(${scale})`,
}}
muted
/>
{ready && (
<div
style={{
boxSizing: 'border-box',
position: 'absolute',
display: 'grid',
top: 0,
gridTemplateColumns: '5fr 2fr',
padding: 4,
// Ideally "100%" is enough, but it doesn't work on iOS
width: dim.width,
height: dim.height,
}}
>
<div
style={{
gridColumn: 1,
border: '1px dashed #cfed57',
}}
/>
<div
style={{
gridColumn: 2,
border: '1px dashed #cfed57',
borderLeft: 0,
}}
/>
</div>
)}
</div>
<div
style={{
fontSize: '0.4em',
color: '#ff606080',
margin: 2,
whiteSpace: 'pre',
}}
>
ZOOM_RATIO{' '}
{scale.toFixed(6)}x
</div>
</div>
<div style={{ margin: 16, marginTop: 0, overflow: 'auto' }}>
Move the camera closer to the screen (or use pinch zoom) to avoid
unnecessary contents. Don't rotate or tilt.
<a
style={{ marginLeft: 4 }}
href="#"
onClick={() => {
setExampleOn(!exampleOn)
}}
>
{exampleOn ? 'Hide' : 'Show'} the example
</a>
{exampleOn && (
<div>
<img style={{ width: '70%' }} src={exampleImg} />
</div>
)}
<div style={{ marginTop: 8 }}>
{!cameraSelectOn ? (
<a
onClick={() => {
setCameraSelectOn(true)
}}
href="#"
>
Camera not working?
</a>
) : (
<>
<div>- Specify the camera to use:</div>
<select
onChange={e => {
setDeviceId(e.target.value)
}}
value={deviceId}
>
{devices &&
devices.map(({ label, deviceId }, i) => (
<option value={deviceId} key={deviceId}>
{i + 1}: {label}
</option>
))}
</select>
<label style={{ display: 'block', marginTop: 8 }}>
-
<input
type="checkbox"
checked={nativeResolutionOn === '1'}
onChange={() => {
setNativeResolutionOn(
nativeResolutionOn === '0' ? '1' : '0'
)
}}
/>
Use native resolution{' '}
<small style={{ fontSize: '0.6em' }}>
(turning it off may fix the black camera issue)
</small>
</label>
{!furthurHelpOn ? (
<a
onClick={() => {
setFurthurHelpOn(true)
}}
href="#"
>
Still not working?
</a>
) : (
<>
<p>
iOS user: Please use Safari browser (see{' '}
<a
href="https://stackoverflow.com/a/29164511"
rel="noopener"
target="_blank"
>
why
</a>
). Make sure you have granted access to the camera (Settings
- Safari - Camera - choose "Ask")
</p>
<p>
Android user: If none of the options above work, please
check{' '}
<a
href="https://github.com/govizlora/optical-breacher/issues/7"
target="_blank"
rel="noopener"
>
this issue
</a>
.
</p>
</>
)}
</>
)}
</div>
</div>
<button
style={{
margin: 'auto',
marginBottom: 16,
}}
onClick={() => {
const canvas = document.createElement('canvas')
const mediaStream = videoRef.current!.srcObject as MediaStream
const {
width: camWidth = 1,
height: camHeight = 1,
} = mediaStream.getTracks()[0].getSettings()
let sourceX = 0
let sourceY = 0
let sourceH = camHeight / scale
let sourceW = camWidth / scale
const context = canvas.getContext('2d')!
if (camWidth / camHeight > ratio) {
// The camera very wide
sourceW = sourceH * ratio
canvas.height = Math.min(sourceH, 720)
canvas.width = canvas.height * ratio
} else {
// The camera is very tall
sourceH = sourceW / ratio
canvas.width = Math.min(sourceW, 1280)
canvas.height = canvas.width / ratio
}
sourceX = (camWidth - sourceW) / 2
sourceY = (camHeight - sourceH) / 2
context.drawImage(
videoRef.current!,
sourceX,
sourceY,
sourceW,
sourceH,
0,
0,
canvas.width,
canvas.height
)
threshold(context)
onCapture(canvas)
}}
>
SCAN
</button>
</>
)
}