forked from MABelanger/jslib-html5-camera-photo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
185 lines (165 loc) · 6.66 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>jslib-html5-camera-photo.min</title>
</head>
<body>
<script src="/jslib-html5-camera-photo.min.js"></script>
<div id="divId">
<label for="facingModeSelectId">facingMode</label>
<select id="facingModeSelectId" name="facingMode">
<option value="ENVIRONMENT" selected="selected">environment</option>
<option value="USER">user</option>
</select>
<button id="startDefaultAllButtonId">startDefaultAll</button>
<button id="startDefaultResolutionButtonId">startDefaultResolution</button>
<button id="startMaxResolutionId">startMaxResolution</button>
<button id="takePhotoButtonId">takePhoto</button>
<button id="stopCameraButtonId">stopCamera</button>
<button id="showInputVideoDeviceInfosButtonId">showInputVideoDeviceInfos</button>
<br/>
<div id="cameraSettingsId"></div>
<div id="inputVideoDeviceInfosId"></div>
<video id="videoId" autoplay="true" playsInline></video>
<img alt="imgId" id="imgId">
</div>
<div id="root">
</div>
<script>
var FACING_MODES = JslibHtml5CameraPhoto.FACING_MODES;
var IMAGE_TYPES = JslibHtml5CameraPhoto.IMAGE_TYPES;
// get video and image elements
var videoElement = document.getElementById('videoId');
var imgElement = document.getElementById('imgId');
// get select and buttons elements
var facingModeSelectElement =
document.getElementById('facingModeSelectId');
var startCameraDefaultAllButtonElement =
document.getElementById('startDefaultAllButtonId');
var startDefaultResolutionButtonElement =
document.getElementById('startDefaultResolutionButtonId');
var startMaxResolutionButtonElement =
document.getElementById('startMaxResolutionId');
var takePhotoButtonElement =
document.getElementById('takePhotoButtonId');
var stopCameraButtonElement =
document.getElementById('stopCameraButtonId');
var cameraSettingElement =
document.getElementById('cameraSettingsId');
var showInputVideoDeviceInfosButtonElement =
document.getElementById('showInputVideoDeviceInfosButtonId');
var inputVideoDeviceInfosElement =
document.getElementById('inputVideoDeviceInfosId');
// instantiate JslibHtml5CameraPhoto with the videoElement
var cameraPhoto = new JslibHtml5CameraPhoto.default(videoElement);
function startCameraDefaultAll () {
cameraPhoto.startCamera()
.then(() => {
var log = `Camera started with default All`;
console.log(log);
})
.catch((error) => {
console.error('Camera not started!', error);
});
}
// start the camera with prefered environment facingMode ie. ()
// if the environment facingMode is not avalible, it will fallback
// to the default camera avalible.
function startCameraDefaultResolution () {
var facingMode = facingModeSelectElement.value;
cameraPhoto.startCamera(FACING_MODES[facingMode])
.then(() => {
var log =
`Camera started with default resolution and ` +
`prefered facingMode : ${facingMode}`;
console.log(log);
})
.catch((error) => {
console.error('Camera not started!', error);
});
}
// function called by the buttons.
function takePhoto () {
var sizeFactor = 1;
var imageType = IMAGE_TYPES.JPG;
var imageCompression = 1;
var config = {
sizeFactor,
imageType,
imageCompression
};
var dataUri = cameraPhoto.getDataUri(config);
imgElement.src = dataUri;
}
function showCameraSettings () {
var settings = cameraPhoto.getCameraSettings();
// by default is no camera...
var innerHTML = 'No camera';
if (settings) {
var {aspectRatio, frameRate, height, width} = settings;
innerHTML = `
aspectRatio:${aspectRatio}
frameRate: ${frameRate}
height: ${height}
width: ${width}
`;
}
cameraSettingElement.innerHTML = innerHTML;
}
function showInputVideoDeviceInfos () {
var inputVideoDeviceInfos = cameraPhoto.getInputVideoDeviceInfos();
// by default is no inputVideoDeviceInfo...
var innerHTML = 'No inputVideoDeviceInfo';
if (inputVideoDeviceInfos) {
innerHTML = '';
inputVideoDeviceInfos.forEach((inputVideoDeviceInfo) => {
var {kind, label, deviceId} = inputVideoDeviceInfo;
var inputVideoDeviceInfoHTML = `
kind: ${kind}
label: ${label}
deviceId: ${deviceId}
<br/>
`;
innerHTML += inputVideoDeviceInfoHTML;
});
}
inputVideoDeviceInfosElement.innerHTML = innerHTML;
}
function stopCamera () {
cameraPhoto.stopCamera()
.then(() => {
console.log('Camera stoped!');
})
.catch((error) => {
console.log('No camera to stop!:', error);
});
}
function startCameraMaxResolution () {
var facingMode = facingModeSelectElement.value;
cameraPhoto.startCameraMaxResolution(FACING_MODES[facingMode])
.then(() => {
var log =
`Camera started with maximum resoluton and ` +
`prefered facingMode : ${facingMode}`;
console.log(log);
})
.catch((error) => {
console.error('Camera not started!', error);
});
}
document.addEventListener('DOMContentLoaded', function () {
// update camera setting
setInterval(() => {
showCameraSettings();
}, 500);
// bind the buttons to the right functions.
startCameraDefaultAllButtonElement.onclick = startCameraDefaultAll;
startDefaultResolutionButtonElement.onclick = startCameraDefaultResolution;
startMaxResolutionButtonElement.onclick = startCameraMaxResolution;
takePhotoButtonElement.onclick = takePhoto;
stopCameraButtonElement.onclick = stopCamera;
showInputVideoDeviceInfosButtonElement.onclick = showInputVideoDeviceInfos;
});
</script>
</body>
</html>