forked from expo/expo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraUtils.ts
170 lines (144 loc) · 4.99 KB
/
CameraUtils.ts
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
/* eslint-env browser */
import invariant from 'invariant';
import { PictureOptions } from './../Camera.types';
import { CameraType, CaptureOptions, ImageSize, ImageType } from './CameraModule.types';
import { requestUserMediaAsync } from './UserMediaManager';
import { CameraTypeToFacingMode, ImageTypeFormat, MinimumConstraints } from './constants';
interface ConstrainLongRange {
max?: number;
min?: number;
exact?: number;
ideal?: number;
}
export function getImageSize(videoWidth: number, videoHeight: number, scale: number): ImageSize {
const width = videoWidth * scale;
const ratio = videoWidth / width;
const height = videoHeight / ratio;
return {
width,
height,
};
}
export function toDataURL(
canvas: HTMLCanvasElement,
imageType: ImageType,
quality: number
): string {
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?: CameraType,
width?: number | ConstrainLongRange,
height?: number | ConstrainLongRange
): boolean {
return preferredCameraType !== undefined && width !== undefined && height !== undefined;
}
function ensureCaptureOptions(config: any): CaptureOptions {
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: HTMLVideoElement, pictureOptions: PictureOptions): string {
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(): MediaTrackSupportedConstraints | null {
if (navigator.mediaDevices && navigator.mediaDevices.getSupportedConstraints) {
return navigator.mediaDevices.getSupportedConstraints();
}
return null;
}
export function getIdealConstraints(
preferredCameraType: CameraType,
width?: number | ConstrainLongRange,
height?: number | ConstrainLongRange
): MediaStreamConstraints {
let preferredConstraints: MediaStreamConstraints = {
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 as MediaTrackConstraints).facingMode = {
[key]: facingMode,
};
} else {
(preferredConstraints.video as MediaTrackConstraints).facingMode = {
ideal: CameraTypeToFacingMode[preferredCameraType],
};
}
}
if (isMediaTrackConstraints(preferredConstraints.video)) {
preferredConstraints.video.width = width;
preferredConstraints.video.height = height;
}
return preferredConstraints;
}
function isMediaTrackConstraints(input: any): input is MediaTrackConstraints {
return input && typeof input.video !== 'boolean';
}
export async function getStreamDevice(
preferredCameraType: CameraType,
preferredWidth?: number | ConstrainLongRange,
preferredHeight?: number | ConstrainLongRange
): Promise<MediaStream> {
const constraints: MediaStreamConstraints = getIdealConstraints(
preferredCameraType,
preferredWidth,
preferredHeight
);
const stream: MediaStream = await requestUserMediaAsync(constraints);
return stream;
}
export function isWebKit(): boolean {
return /WebKit/.test(navigator.userAgent) && !/Edg/.test(navigator.userAgent);
}