-
Notifications
You must be signed in to change notification settings - Fork 12
/
app-media-image-capture.js
549 lines (482 loc) · 16.2 KB
/
app-media-image-capture.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/**
@license
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at
http://polymer.github.io/LICENSE.txt The complete set of authors may be found at
http://polymer.github.io/AUTHORS.txt The complete set of contributors may be
found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by Google as
part of the polymer project is also subject to an additional IP rights grant
found at http://polymer.github.io/PATENTS.txt
*/
import {Base, Polymer} from '@polymer/polymer/polymer-legacy.js';
import {AppMedia as AppMedia$0} from './app-media-recorder.js';
export const AppMedia = AppMedia$0 || {};
/**
* @enum {string}
* @see https://www.w3.org/TR/image-capture/#MeteringMode
*/
AppMedia$0.MeteringMode = {
SINGLE_SHOT: 'single-shot',
CONTINUOUS: 'continuous',
MANUAL: 'manual',
NONE: 'none'
};
/**
* @enum {string}
* @see https://www.w3.org/TR/image-capture/#FillLightMode
*/
AppMedia$0.FillLightMode = {
AUTO: 'auto',
OFF: 'off',
FLASH: 'flash'
};
var imageCaptureSupported = true;
if (window.ImageCapture == null) {
Base._warn('Image Capture API is not supported in this browser!');
imageCaptureSupported = false;
}
var TRACK_CONSTRAINT_NAMES = [
'whiteBalanceMode',
'colorTemperature',
'exposureMode',
'exposureCompensation',
'iso',
'focusMode',
'pointsOfInterest',
'brightness',
'contrast',
'saturation',
'sharpness',
'zoom',
'torch'
];
var PHOTO_SETTING_NAMES = ['fillLightMode', 'imageHeight', 'imageWidth'];
/**
`app-media-image-capture` implements a helpful wrapper element for the emerging
[Image Capture API](https://www.w3.org/TR/image-capture/). This element enables
straight-forward photographic image control and capture that cooperates nicely
with other app-media elements.
NOTE: Image Capture API is still under development. As of today (April 7th,
2017), the API requires a polyfill or an [Origin
Trial](https://github.com/jpchase/OriginTrials/blob/gh-pages/developer-guide.md).
This element will politely neglect to register itself in browsers that do not
feature the appropriate ImageRecorder global constructor.
Chrome and Firefox have partial support for the API behind flags. See the
[ImageCapture API browser
support](https://github.com/w3c/mediacapture-image/blob/gh-pages/implementation-status.md)
page for details.
If you wish to support browsers that do not implement the Image Capture API,
please consider including a polyfill such as
[this one](https://github.com/GoogleChrome/imagecapture-polyfill) in your app.
Note that most polyfills will not enable full functionality, but they should
give your app a reasonable fallback in browsers that do not natively support the
API.
ELEMENT DESIGN NOTE: Many of the properties of this element have a default
configuration value of `null`. While this is weird, it is important to keep the
behavior of multi-property observers in the element implementation normalized
across Polymer 1.x and 2.x. Expect this aspect of the API to change as the
element graduates from the current hybrid implementation to something that can
rely on Polymer >=2.x observer semantics.
@group App Elements
@demo demo/index.html
*/
Polymer({
is: 'app-media-image-capture',
properties: {
/**
* The input media stream to capture images from. Note that the
* stream must have at least one video track in order to be a suitable
* source for image capture.
*
* @type {MediaStream}
*/
stream: {type: Object},
/**
* MediaStreams can have multiple video tracks. This property enables
* you to configure the index of the video track to use.
*/
trackIndex: {
type: Number,
value: 0,
},
/**
* The video track selected from the input MediaStream. This track will
* be the source for any images captured.
*
* @type {MediaStreamTrack}
*/
videoTrack: {
type: Object,
notify: true,
computed: '__computeVideoTrack(stream, trackIndex)',
},
/**
* An ImageCapture instance associated with the selected video track.
*
* @type {ImageCapture}
*/
imageCapture: {
type: Object,
notify: true,
computed: '__computeImageCapture(videoTrack)',
},
/**
* The last photo captured by the image capture instance.
*
* @type {Blob}
*/
lastPhoto: {
type: Object,
notify: true,
readOnly: true,
},
/**
* The last frame grabbed by the image capture instance.
*
* @type {ImageBitmap}
*/
lastFrame: {
type: Object,
notify: true,
readOnly: true,
},
/**
* The PhotoCapabilities for the device providing the image data
* associated with the chosen video track. This object contains
* information about the minimum, maximum, current and incremental
* values for various camera settings.
*
* @see https://w3c.github.io/mediacapture-image/##photocapabilities-section
*/
photoCapabilities: {
type: Object,
readOnly: true,
notify: true,
},
/**
* The MediaTrackCapabilities for the device providing the image data
* associated with the chosen video track. This object contains
* information about the minimum, maximum, current and incremental
* values for various camera settings.
*
* @see https://w3c.github.io/mediacapture-image/#mediatrackcapabilities-section
*/
trackCapabilities: {
type: Object,
notify: true,
computed: '__computeTrackCapabilities(imageCapture, videoTrack)',
},
/**
* The PhotoSettings that will be used to configure the ImageCapture
* instance used by this element. This configuration is generated
* based on the individually configured properties on this element.
* A full list of configurable properties can be found
* [here](https://w3c.github.io/mediacapture-image/##photosettings-section).
*
* @type {!PhotoSettings}
*/
photoSettings: {
type: Object,
readOnly: true,
notify: true,
},
/**
* @type {Polymer.AppMedia.FillLightMode}
* @see https://w3c.github.io/mediacapture-image/##photosettings-section
*/
fillLightMode: {
type: String,
value: null,
},
/** @see https://w3c.github.io/mediacapture-image/##photosettings-section */
imageHeight: {
type: Number,
value: null,
},
/** @see https://w3c.github.io/mediacapture-image/##photosettings-section */
imageWidth: {
type: Number,
value: null,
},
/**
* Note that red eye reduction may not be controllable. If it is reported as
* controllable, the value of this property will be respected. Otherwise it
* will be ignored.
*
* @see https://w3c.github.io/mediacapture-image/##photosettings-section
*/
redEyeReduction: {
type: Boolean,
value: null,
},
/**
* The constraints that will be applied to the MediaStreamTrack that
* is associated with the ImageCapture instance. This configuration is
* generated based on the individually configured properties on this
* element. A full list of configurable constraints can be found
* [here](https://w3c.github.io/mediacapture-image/#constrainable-properties)
*
* Note that if a given setting is not supported by the current track, it
* will be ignored. Also, any constraints that are suported will be clamped
* to the bounds that are reported by the track PhotoCapabilities instance.
*
* @type {!MediaTrackConstraints}
*/
trackConstraints: {
type: Object,
readOnly: true,
notify: true,
},
/**
@see https://w3c.github.io/mediacapture-image/#mediatracksettings-members
*/
whiteBalanceMode: {
type: String,
value: null,
},
/**
@see https://w3c.github.io/mediacapture-image/#mediatracksettings-members
*/
colorTemperature: {
type: Number,
value: null,
},
/**
* @type {Polymer.AppMedia.MeteringMode}
* @see https://w3c.github.io/mediacapture-image/#mediatracksettings-members
*/
exposureMode: {
type: String,
value: null,
},
/**
@see https://w3c.github.io/mediacapture-image/#mediatracksettings-members
*/
exposureCompensation: {
type: Number,
value: null,
},
/**
@see https://w3c.github.io/mediacapture-image/#mediatracksettings-members
*/
iso: {
type: Number,
value: null,
},
/**
* @type {Polymer.AppMedia.MeteringMode}
* @see https://w3c.github.io/mediacapture-image/#mediatracksettings-members
*/
focusMode: {
type: String,
value: null,
},
/**
@see https://w3c.github.io/mediacapture-image/#mediatracksettings-members
*/
pointsOfInterest: {
type: Array,
value: null,
},
/**
@see https://w3c.github.io/mediacapture-image/#mediatracksettings-members
*/
brightness: {
type: Number,
value: null,
},
/**
@see https://w3c.github.io/mediacapture-image/#mediatracksettings-members
*/
contrast: {
type: Number,
value: null,
},
/**
@see https://w3c.github.io/mediacapture-image/#mediatracksettings-members
*/
saturation: {
type: Number,
value: null,
},
/**
@see https://w3c.github.io/mediacapture-image/#mediatracksettings-members
*/
sharpness: {
type: Number,
value: null,
},
/**
@see https://w3c.github.io/mediacapture-image/#mediatracksettings-members
*/
zoom: {
type: Number,
value: null,
},
/**
@see https://w3c.github.io/mediacapture-image/#mediatracksettings-members
*/
torch: {
type: Boolean,
value: null,
}
},
observers: [
'__updatePhotoCapabilities(imageCapture, videoTrack)',
'__updatePhotoSettings(imageCapture, photoCapabilities, fillLightMode, imageHeight, imageWidth, redEyeReduction)',
'__updateTrackConstraints(imageCapture, trackCapabilities, whiteBalanceMode, exposureMode, focusMode, pointsOfInterest, exposureCompensation, colorTemperature, iso, brightness, contrast, saturation, sharpness, zoom, torch)'
],
/**
* Take a photo. Returns a promise that resolves a photographic Blob.
*
* @return Promise<Blob>
* @see https://www.w3.org/TR/image-capture/#dom-imagecapture-takephoto
*/
takePhoto: function() {
if (!imageCaptureSupported) {
return;
}
if (this.imageCapture == null) {
return Promise.reject(new Error('ImageCapture instance not ready.'));
}
return this.imageCapture.takePhoto(this.photoSettings)
.then(function(photo) {
this._setLastPhoto(photo);
return photo;
}.bind(this));
},
/**
* Grab a frame from the camera. Returns a promise that resolves an
* ImageBitmap suitable for drawing to a canvas or used in conjunction
* with object detection.
*
* @return Promise<ImageBitmap>
* @see https://www.w3.org/TR/image-capture/#dom-imagecapture-grabframe
*/
grabFrame: function() {
if (!imageCaptureSupported) {
return;
}
if (this.imageCapture == null) {
return Promise.reject(new Error('ImageCapture instance not ready.'));
}
return this.imageCapture.grabFrame().then(function(frame) {
this._setLastFrame(frame);
return frame;
}.bind(this));
},
__computeVideoTrack: function(stream, trackIndex) {
if (stream == null || trackIndex == null) {
return;
}
return stream.getVideoTracks()[trackIndex];
},
__computeImageCapture: function(videoTrack) {
if (!imageCaptureSupported) {
return;
}
return new ImageCapture(videoTrack);
},
__computeTrackCapabilities: function(imageCapture, videoTrack) {
if (imageCapture == null || videoTrack == null) {
return;
}
return videoTrack.getCapabilities();
},
__updatePhotoCapabilities: function() {
if (!imageCaptureSupported) {
return;
}
this.debounce('updatePhotoCapabilities', function() {
this.imageCapture.getPhotoCapabilities()
.then(function(photoCapabilities) {
this._setPhotoCapabilities(photoCapabilities);
}.bind(this))
.catch(function(e) {
this.fire('image-capture-capabilities-error', e);
}.bind(this));
});
},
/**
* ELEMENT DESIGN NOTE: While this observer does not rely on the
* arguments passed to it, it also avoids the pitfall of relying on
* properties that may be in a partial / indeterminant state at the
* time that its debounce'd implementation is invoked. As long as
* this remains true, it should not be subject to the undesirable
* behavior exhibited by some elements such as iron-selector.
*/
__updateTrackConstraints: function() {
this.debounce('updateTrackConstraints', function() {
var trackConstraints = this.__generateConfigurationObject(
this.trackCapabilities, TRACK_CONSTRAINT_NAMES);
// TODO(cdata): It's not clear how constraints from one controller
// (such as this element) should attempt to compose with constraints
// from some other controller that has a reference to the same
// track. It's possible that we may benefit from some additional
// abstraction dedicated to managing constraints (or perhaps
// conflicts between controllers).
// @see https://www.w3.org/TR/mediacapture-streams/#dfn-applyconstraints
this._setTrackConstraints(trackConstraints);
});
},
__updatePhotoSettings: function() {
this.debounce('updatePhotoSettings', function() {
// Don't update anything until we know the available capabilities:
if (this.photoCapabilities == null) {
return;
}
var photoSettings = this.__generateConfigurationObject(
this.photoCapabilities, PHOTO_SETTING_NAMES);
// Red eye reduction is a special case. The capability is reported
// as one of three states, and only one (controllable) allows the
// user to configure it.
// @see https://w3c.github.io/mediacapture-image/#redeyereduction-section
if (this.redEyeReduction != null &&
this.photoCapabilities.redEyeReduction === 'controllable') {
photoSettings.redEyeReduction = this.redEyeReduction;
}
this._setPhotoSettings(photoSettings);
});
},
/**
* The purpose of this method is to take a set of capabilities reported
* by the platform, and sift the properties of this element through
* the filter of those reported capabilities. Properties that are not
* supported are ignored. Properties that are supported but don't have
* values within a MediaSettingsRange are clamped to that range.
* Properties with values that do not occur within a reported vector of
* supported values are ignored.
*/
__generateConfigurationObject: function(reportedCapabilities, allowedNames) {
var configurationObject = {};
if (reportedCapabilities != null) {
for (var i = 0; i < allowedNames.length; ++i) {
var name = allowedNames[i];
var value = this[name];
var capability = reportedCapabilities[name];
var capabilityIsArray = Array.isArray(capability);
// Don't set photo options if a value is provided, and skip
// if the name is not present in the PhotoCapabilities object
// (setting such properties will probably result in an error).
if (value == null || capability == null || capability === false ||
(capabilityIsArray && capability.indexOf(value) === -1)) {
continue;
}
// If the capability is an object, then it is probably a
// MediaSettingsRange. This means we need to clamp the value to the
// min/max values of the range (otherwise it will likely throw when
// we try to apply the configuration).
// @see https://w3c.github.io/mediacapture-image/#mediasettingsrange
if (!capabilityIsArray && capability instanceof Object) {
if (capability.min >= value) {
value = capability.min;
} else if (capability.max <= value) {
value = capability.max;
}
}
configurationObject[name] = value;
}
}
return configurationObject;
}
});