-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera-take.js
88 lines (71 loc) · 2.76 KB
/
camera-take.js
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
// Change Camera settings
// partial public const MediaTrackConstraints {
// whiteBalanceMode = true,
// exposureMode = true,
// focusMode = true,
// pointsOfInterest = true,
// exposureCompensation = true,
// exposureTime = true,
// colorTemperature = true,
// iso = true,
// brightness = true,
// contrast = true,
// pan = true,
// saturation = true,
// sharpness = true,
// focusDistance = true,
// tilt = true,
// zoom = true,
// torch = true,
// };
const input = document.querySelector('input[type="range"]');
var imageCapture;
navigator.mediaDevices.getUserMedia({ video: true })
.then(mediaStream => {
document.querySelector('video').srcObject = mediaStream;
const track = mediaStream.getVideoTracks()[0];
/// OVERRIDE Constraint
const capabilities = track.getCapabilities()
// Check whether aspectRatio and frameRate are supported.
if (!capabilities.aspectRatio || !capabilities.frameRate) {
return;
}
track.applyConstraints({advanced : [{aspectRatio: 1}] });
track.applyConstraints({advanced : [{frameRate: 5}] });
/// OVERRIDE Constraint
imageCapture = new ImageCapture(track);
return imageCapture.getPhotoCapabilities();
}).then(photoCapabilities => {
const settings = imageCapture.track.getSettings();
input.min = photoCapabilities.imageWidth.min;
input.max = photoCapabilities.imageWidth.max;
input.step = photoCapabilities.imageWidth.step;
return imageCapture.getPhotoSettings();
}).then(photoSettings => {
input.value = photoSettings.imageWidth;
}).catch(error => ChromeSamples.log('Argh!', error.name || error));
function onTakePhotoButtonClick() {
imageCapture.takePhoto({ imageWidth: input.value })
.then(blob => createImageBitmap(blob))
.then(imageBitmap => {
drawCanvas(imageBitmap);
ChromeSamples.log(`Photo size is ${imageBitmap.width}x${imageBitmap.height}`);
})
.catch(error => ChromeSamples.log(error));
}
document.querySelector('video').addEventListener('play', function () {
document.querySelector('#takePhotoButton').disabled = false;
});
/* Utils */
function drawCanvas(img) {
const canvas = document.querySelector('canvas');
canvas.width = getComputedStyle(canvas).width.split('px')[0];
canvas.height = getComputedStyle(canvas).height.split('px')[0];
let ratio = Math.min(canvas.width / img.width, canvas.height / img.height);
let x = (canvas.width - img.width * ratio) / 2;
let y = (canvas.height - img.height * ratio) / 2;
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height,
x, y, img.width * ratio, img.height * ratio);
}
document.querySelector('#takePhotoButton').addEventListener('click', onTakePhotoButtonClick);