forked from expo/expo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraUtils.js
113 lines (113 loc) · 4.4 KB
/
CameraUtils.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
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
/* eslint-env browser */
import invariant from 'invariant';
import { CameraType, ImageType } from './CameraModule.types';
import { requestUserMediaAsync } from './UserMediaManager';
import { CameraTypeToFacingMode, ImageTypeFormat, MinimumConstraints } from './constants';
export function getImageSize(videoWidth, videoHeight, scale) {
const width = videoWidth * scale;
const ratio = videoWidth / width;
const height = videoHeight / ratio;
return {
width,
height,
};
}
export function toDataURL(canvas, imageType, quality) {
invariant(Object.values(ImageType).includes(imageType), `expo-camera: ${imageType} is not a valid ImageType. Expected a string from: ${Object.values(ImageType).join(', ')}`);
const format = ImageTypeFormat[imageType];
if (imageType === ImageType.jpg) {
invariant(quality <= 1 && quality >= 0, `expo-camera: ${quality} is not a valid image quality. Expected a number from 0...1`);
return canvas.toDataURL(format, quality);
}
else {
return canvas.toDataURL(format);
}
}
export function hasValidConstraints(preferredCameraType, width, height) {
return preferredCameraType !== undefined && width !== undefined && height !== undefined;
}
function ensureCaptureOptions(config) {
const captureOptions = {
scale: 1,
imageType: ImageType.png,
isImageMirror: false,
};
for (const key in config) {
if (key in config && config[key] !== undefined && key in captureOptions) {
captureOptions[key] = config[key];
}
}
return captureOptions;
}
const DEFAULT_QUALITY = 0.92;
export function captureImage(video, pictureOptions) {
const config = ensureCaptureOptions(pictureOptions);
const { scale, imageType, quality = DEFAULT_QUALITY, isImageMirror } = config;
const { videoWidth, videoHeight } = video;
const { width, height } = getImageSize(videoWidth, videoHeight, scale);
// Build the canvas size and draw the camera image to the context from video
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const context = canvas.getContext('2d');
//TODO: Bacon: useless
if (!context)
throw new Error('Context is not defined');
// Flip horizontally (as css transform: rotateY(180deg))
if (isImageMirror) {
context.setTransform(-1, 0, 0, 1, canvas.width, 0);
}
context.drawImage(video, 0, 0, width, height);
const base64 = toDataURL(canvas, imageType, quality);
return base64;
}
function getSupportedConstraints() {
if (navigator.mediaDevices && navigator.mediaDevices.getSupportedConstraints) {
return navigator.mediaDevices.getSupportedConstraints();
}
return null;
}
export function getIdealConstraints(preferredCameraType, width, height) {
let preferredConstraints = {
audio: false,
video: {},
};
if (hasValidConstraints(preferredCameraType, width, height)) {
return MinimumConstraints;
}
const supports = getSupportedConstraints();
// TODO: Bacon: Test this
if (!supports || !supports.facingMode || !supports.width || !supports.height)
return MinimumConstraints;
if (preferredCameraType && Object.values(CameraType).includes(preferredCameraType)) {
const facingMode = CameraTypeToFacingMode[preferredCameraType];
if (isWebKit()) {
const key = facingMode === 'user' ? 'exact' : 'ideal';
preferredConstraints.video.facingMode = {
[key]: facingMode,
};
}
else {
preferredConstraints.video.facingMode = {
ideal: CameraTypeToFacingMode[preferredCameraType],
};
}
}
if (isMediaTrackConstraints(preferredConstraints.video)) {
preferredConstraints.video.width = width;
preferredConstraints.video.height = height;
}
return preferredConstraints;
}
function isMediaTrackConstraints(input) {
return input && typeof input.video !== 'boolean';
}
export async function getStreamDevice(preferredCameraType, preferredWidth, preferredHeight) {
const constraints = getIdealConstraints(preferredCameraType, preferredWidth, preferredHeight);
const stream = await requestUserMediaAsync(constraints);
return stream;
}
export function isWebKit() {
return /WebKit/.test(navigator.userAgent) && !/Edg/.test(navigator.userAgent);
}
//# sourceMappingURL=CameraUtils.js.map