@digipair/skill-webcam 0.93.0-0 → 0.93.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js DELETED
@@ -1,1143 +0,0 @@
1
- import require$$0 from 'os';
2
- import require$$0$1 from 'child_process';
3
- import require$$1 from 'fs';
4
- import require$$3 from 'path';
5
-
6
- /**
7
- * JS utils
8
- *
9
- */
10
-
11
- function _type_of$1(obj) {
12
- "@swc/helpers - typeof";
13
- return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
14
- }
15
- var Utils$4 = {
16
- //Highly used as an inheritance
17
- setDefaults: function setDefaults(object, defaults) {
18
- var defaults = (typeof defaults === "undefined" ? "undefined" : _type_of$1(defaults)) === "object" ? defaults : {};
19
- var object = (typeof object === "undefined" ? "undefined" : _type_of$1(object)) === "object" ? object : {};
20
- if (object === defaults) {
21
- return object;
22
- }
23
- for(var defaultName in defaults){
24
- var defaultVal = defaults[defaultName];
25
- var objectVal = object[defaultName];
26
- if ((typeof defaultVal === "undefined" ? "undefined" : _type_of$1(defaultVal)) === "object") {
27
- object[defaultName] = Utils$4.setDefaults(objectVal, defaultVal);
28
- } else if (typeof objectVal === "undefined") {
29
- object[defaultName] = defaults[defaultName];
30
- }
31
- }
32
- return object;
33
- },
34
- //Node-webcam escape string
35
- escape: function escape(cmd) {
36
- return '"' + cmd.replace(/(["\s'$`\\()])/g, '\\$1') + '"';
37
- }
38
- };
39
- var Utils_1 = Utils$4;
40
-
41
- /**
42
- * based on mrdoob's named like Node's EventEmitter
43
- * Used primarily as an inheritance via apply
44
- *
45
- */
46
-
47
- var EventDispatcher$1 = function EventDispatcher() {};
48
- EventDispatcher$1.prototype = {
49
- constructor: EventDispatcher$1,
50
- apply: function apply(object) {
51
- object.on = EventDispatcher$1.prototype.on;
52
- object.hasListener = EventDispatcher$1.prototype.hasListener;
53
- object.removeListener = EventDispatcher$1.prototype.removeListener;
54
- object.dispatch = EventDispatcher$1.prototype.dispatch;
55
- return object;
56
- },
57
- on: function on(type, listener) {
58
- if (this._listeners === undefined) {
59
- this._listeners = {};
60
- }
61
- var listeners = this._listeners;
62
- if (listeners[type] === undefined) {
63
- listeners[type] = [];
64
- }
65
- if (listeners[type].indexOf(listener) === -1) {
66
- listeners[type].push(listener);
67
- }
68
- },
69
- hasListener: function hasListener(type, listener) {
70
- if (this._listeners === undefined) {
71
- return false;
72
- }
73
- var listeners = this._listeners;
74
- if (listeners[type] !== undefined && listeners[type].indexOf(listener) !== -1) {
75
- return true;
76
- }
77
- return false;
78
- },
79
- removeListener: function removeListener(type, listener) {
80
- if (this._listeners === undefined) {
81
- return;
82
- }
83
- var listeners = this._listeners;
84
- var listenerArray = listeners[type];
85
- if (listenerArray !== undefined) {
86
- var index = listenerArray.indexOf(listener);
87
- if (index !== -1) {
88
- listenerArray.splice(index, 1);
89
- }
90
- }
91
- },
92
- dispatch: function dispatch(event) {
93
- if (this._listeners === undefined) {
94
- return;
95
- }
96
- var listeners = this._listeners;
97
- var listenerArray = listeners[event.type];
98
- if (listenerArray !== undefined) {
99
- event.target = this;
100
- var array = [];
101
- var length = listenerArray.length;
102
- for(var i = 0; i < length; i++){
103
- array[i] = listenerArray[i];
104
- }
105
- for(var i = 0; i < length; i++){
106
- array[i].call(this, event);
107
- }
108
- }
109
- }
110
- };
111
- var EventDispatcher_1 = EventDispatcher$1;
112
-
113
- /**
114
- * Shared camera utils
115
- *
116
- */
117
- var OS$1 = require$$0;
118
- var FS$1 = require$$1;
119
- var CameraUtils$1 = {
120
- getCameras: function getCameras(callback) {
121
- switch(CameraUtils$1.Platform){
122
- case "linux":
123
- case "darwin":
124
- return CameraUtils$1.getLinuxCameras(callback);
125
- }
126
- },
127
- //Linux cameras read /dev dir
128
- getLinuxCameras: function getLinuxCameras(callback) {
129
- var reg = /^video/i;
130
- var dir = "/dev/";
131
- FS$1.readdir(dir, function(err, data) {
132
- if (err) {
133
- throw err;
134
- }
135
- var cams = [];
136
- var dl = data.length;
137
- for(var i = 0; i < dl; i++){
138
- var camPath = data[i];
139
- if (camPath.match(reg)) {
140
- cams.push(dir + camPath);
141
- }
142
- }
143
- callback && callback(cams);
144
- });
145
- }
146
- };
147
- CameraUtils$1.Platform = OS$1.platform();
148
- var CameraUtils_1 = CameraUtils$1;
149
-
150
- /**
151
- * Shot struct
152
- *
153
- * @class Shot
154
- * @constructor
155
- * @param {String|null} location
156
- * @param {Buffer|null} data
157
- *
158
- */
159
- function Shot$2(location, data) {
160
- var scope = this;
161
- scope.location = location;
162
- scope.data = data;
163
- }
164
- Shot$2.prototype = {
165
- constructor: Shot$2,
166
- /**
167
- * Shot location
168
- *
169
- * @property location
170
- *
171
- * @type {String|null}
172
- *
173
- */ location: null,
174
- /**
175
- * Shot data or buffer
176
- *
177
- * @property data
178
- *
179
- * @type {Buffer|null}
180
- *
181
- */ data: null
182
- };
183
- // Export
184
- var Shot_1 = Shot$2;
185
-
186
- /**
187
- *
188
- * Webcam base class
189
- *
190
- * @class Webcam
191
- * @constructor
192
- * @param {Object} options composition options
193
- * used to set
194
- *
195
- */
196
- var CHILD_PROCESS$2 = require$$0$1;
197
- var EXEC$2 = CHILD_PROCESS$2.exec;
198
- var FS = require$$1;
199
- var Utils$3 = Utils_1;
200
- var EventDispatcher = EventDispatcher_1;
201
- var CameraUtils = CameraUtils_1;
202
- var Shot$1 = Shot_1;
203
- /*
204
- * Main class
205
- *
206
- */ function Webcam$3(options) {
207
- var scope = this;
208
- scope.shots = [];
209
- scope.opts = Utils$3.setDefaults(options, Webcam$3.Defaults);
210
- }
211
- Webcam$3.prototype = {
212
- constructor: Webcam$3,
213
- /**
214
- * Main opts from construction
215
- *
216
- * @property opts
217
- * @type {Object}
218
- *
219
- */ opts: {},
220
- /**
221
- * Array of Shot objects
222
- *
223
- * @property shots
224
- * @type {Shots[]}
225
- *
226
- */ shots: [],
227
- /**
228
- * Basic camera instance clone
229
- *
230
- * @method clone
231
- *
232
- * @return Webcam
233
- *
234
- */ clone: function clone() {
235
- return new this.constructor(this.opts);
236
- },
237
- /**
238
- * Clear shot and camera memory data
239
- *
240
- * @method clear
241
- *
242
- */ clear: function clear() {
243
- var scope = this;
244
- scope.shots = [];
245
- },
246
- /**
247
- * List available cameras
248
- *
249
- * @method list
250
- *
251
- * @param {Function} callback returns a list of cameras
252
- *
253
- */ list: CameraUtils.getCameras,
254
- /**
255
- * List available camera controls
256
- *
257
- * @method listControls
258
- *
259
- * @param {Function(Array<CameraControl>)} callback Function that receives
260
- * camera controls
261
- *
262
- * @param {String} stdoutOverride fswebcam command output override (for
263
- * testing purposes)
264
- *
265
- */ listControls: function listControls(callback, stdoutOverride) {
266
- var scope = this;
267
- var sh;
268
- try {
269
- sh = scope.getListControlsSh();
270
- } catch (_) {
271
- callback && callback([]);
272
- }
273
- //Shell execute
274
- var shArgs = {
275
- maxBuffer: 1024 * 10000
276
- };
277
- EXEC$2(sh, shArgs, function(err, out, derr) {
278
- if (err) {
279
- return callback && callback(err);
280
- }
281
- if (scope.opts.verbose && derr) {
282
- console.log(derr);
283
- }
284
- scope.parseListControls(stdoutOverride || out + derr, callback);
285
- });
286
- },
287
- /**
288
- * Has camera
289
- *
290
- * @method hasCamera
291
- *
292
- * @param {Function} callback returns a Boolean
293
- *
294
- */ hasCamera: function hasCamera(callback) {
295
- var scope = this;
296
- scope.list(function(list) {
297
- callback && callback(!!list.length);
298
- });
299
- },
300
- /**
301
- * Capture shot
302
- *
303
- * @method capture
304
- *
305
- * @param {String} location
306
- * @param {Function} callback
307
- * @return void
308
- *
309
- */ capture: function capture(location, callback) {
310
- var scope = this;
311
- if (location === null && scope.opts.callbackReturn === Webcam$3.CallbackReturnTypes.location) {
312
- console.warn("If capturing image in memory\
313
- your callback return type cannot be the location");
314
- scope.opts.callbackReturn = "buffer";
315
- }
316
- var fileType = Webcam$3.OutputTypes[scope.opts.output];
317
- var location = location === null ? null : location.match(/\..*$/) ? location : location + "." + fileType;
318
- //Shell statement grab
319
- var sh = scope.generateSh(location);
320
- if (scope.opts.verbose) {
321
- console.log(sh);
322
- }
323
- //Shell execute
324
- var shArgs = {
325
- maxBuffer: 1024 * 10000
326
- };
327
- EXEC$2(sh, shArgs, function(err, out, derr) {
328
- if (err) {
329
- return callback && callback(err);
330
- }
331
- if (scope.opts.verbose && derr) {
332
- console.log(derr);
333
- }
334
- //Run validation overrides
335
- var validationErrors;
336
- if (validationErrors = scope.runCaptureValidations(derr)) {
337
- return callback && callback(validationErrors);
338
- }
339
- //Callbacks
340
- var shot = scope.createShot(location, derr);
341
- if (scope.opts.saveShots) {
342
- scope.shots.push(shot);
343
- }
344
- scope.dispatch({
345
- type: "capture",
346
- shot: shot
347
- });
348
- callback && scope.handleCallbackReturnType(callback, shot);
349
- });
350
- },
351
- /**
352
- * Generate cli command string
353
- *
354
- * @method generateSh
355
- *
356
- * @return {String}
357
- *
358
- */ generateSh: function generateSh(location) {
359
- return "";
360
- },
361
- /**
362
- * Create a shot overider
363
- *
364
- * @method createShot
365
- *
366
- * @return {String}
367
- *
368
- */ createShot: function createShot(location, data) {
369
- return new Shot$1(location, data);
370
- },
371
- /**
372
- * Get shot instances from cache index
373
- *
374
- * @method getShot
375
- *
376
- * @param {Number} shot Index of shots called
377
- * @param {Function} callback Returns a call from FS.readFile data
378
- *
379
- * @throws Error if shot at index not found
380
- *
381
- * @return {Boolean}
382
- *
383
- */ getShot: function getShot(index, callback) {
384
- var scope = this;
385
- var shot = scope.shots[index | 0];
386
- if (!shot) {
387
- throw new Error("Shot number " + index + " not found");
388
- }
389
- return shot;
390
- },
391
- /**
392
- * Get last shot taken image data
393
- *
394
- * @method getLastShot
395
- *
396
- * @throws Error Camera has no last shot
397
- *
398
- * @return {Shot}
399
- *
400
- */ getLastShot: function getLastShot() {
401
- var scope = this;
402
- if (!scope.shots.length) {
403
- throw new Error("Camera has no last shot");
404
- }
405
- return scope.getShot(scope.shots.length - 1);
406
- },
407
- /**
408
- * Get shot buffer from location
409
- * 0 indexed
410
- *
411
- * @method getShotBuffer
412
- *
413
- * @param {Number} shot Index of shots called
414
- * @param {Function} callback Returns a call from FS.readFile data
415
- *
416
- * @return {Boolean}
417
- *
418
- */ getShotBuffer: function getShotBuffer(shot, callback) {
419
- var scope = this;
420
- if (typeof shot === "number") {
421
- shot = scope.getShot(shot);
422
- }
423
- if (shot.location) {
424
- FS.readFile(shot.location, function(err, data) {
425
- callback(err, data);
426
- });
427
- } else if (!shot.data) {
428
- callback(new Error("Shot not valid"));
429
- } else {
430
- callback(null, shot.data);
431
- }
432
- },
433
- /**
434
- * Get last shot buffer taken image data
435
- *
436
- * @method getLastShotBuffer
437
- *
438
- * @throws Error Shot not found
439
- *
440
- * @return {Shot}
441
- *
442
- */ getLastShotBuffer: function getLastShotBuffer(callback) {
443
- var scope = this;
444
- var shot = scope.getLastShot();
445
- scope.getShotBuffer(shot, callback);
446
- },
447
- /**
448
- * Get shot base64 as image
449
- * if passed Number will return a base 64 in the callback
450
- *
451
- * @method getBase64
452
- *
453
- * @param {Number|FS.readFile} shot To be converted
454
- * @param {Function( Error|null, Mixed )} callback Returns base64 string
455
- *
456
- * @return {String} Dont use
457
- *
458
- */ getBase64: function getBase64(shot, callback) {
459
- var scope = this;
460
- scope.getShotBuffer(shot, function(err, data) {
461
- if (err) {
462
- callback(err);
463
- return;
464
- }
465
- var base64 = scope.getBase64FromBuffer(data);
466
- callback(null, base64);
467
- });
468
- },
469
- /**
470
- * Get base64 string from bufer
471
- *
472
- * @method getBase64
473
- *
474
- * @param {Number|FS.readFile} shot To be converted
475
- * @param {Function( Error|null, Mixed )} callback Returns base64 string
476
- *
477
- * @return {String} Dont use
478
- *
479
- */ getBase64FromBuffer: function getBase64FromBuffer(shotBuffer) {
480
- var scope = this;
481
- var image = "data:image/" + scope.opts.output + ";base64," + Buffer.from(shotBuffer).toString("base64");
482
- return image;
483
- },
484
- /**
485
- * Get last shot taken base 64 string
486
- *
487
- * @method getLastShot64
488
- *
489
- * @param {Function} callback
490
- *
491
- */ getLastShot64: function getLastShot64(callback) {
492
- var scope = this;
493
- if (!scope.shots.length) {
494
- callback && callback(new Error("Camera has no last shot"));
495
- }
496
- scope.getBase64(scope.shots.length - 1, callback);
497
- },
498
- /**
499
- * Get last shot taken image data
500
- *
501
- * @method handleCallbackReturnType
502
- *
503
- * @param {Function} callback
504
- * @param {String} location
505
- *
506
- * @throws Error callbackReturn Type not valid
507
- *
508
- */ handleCallbackReturnType: function handleCallbackReturnType(callback, shot) {
509
- var scope = this;
510
- switch(scope.opts.callbackReturn){
511
- case Webcam$3.CallbackReturnTypes.location:
512
- return callback(null, shot.location);
513
- case Webcam$3.CallbackReturnTypes.buffer:
514
- return scope.getShotBuffer(shot, callback);
515
- case Webcam$3.CallbackReturnTypes.base64:
516
- return scope.getBase64(shot, callback);
517
- default:
518
- return callback(new Error("Callback return type not valid " + scope.opts.callbackReturn));
519
- }
520
- },
521
- /**
522
- * Data validations for a command line output
523
- *
524
- * @override
525
- *
526
- * @param {String} Command exec output
527
- *
528
- * @return {Error|null}
529
- *
530
- */ runCaptureValidations: function runCaptureValidations(data) {
531
- return null;
532
- }
533
- };
534
- EventDispatcher.prototype.apply(Webcam$3.prototype);
535
- /**
536
- * Base defaults for option construction
537
- *
538
- * @property Webcam.Defaults
539
- *
540
- * @type Object
541
- * @static
542
- *
543
- */ Webcam$3.Defaults = {
544
- //Picture related
545
- width: 1280,
546
- height: 720,
547
- quality: 100,
548
- //Delay to take shot
549
- delay: 0,
550
- //Title of the saved picture
551
- title: false,
552
- //Subtitle of the saved picture
553
- subtitle: false,
554
- //Timestamp of the saved picture
555
- timestamp: false,
556
- //Save shots in memory
557
- saveShots: true,
558
- // [jpeg, png] support varies
559
- // Webcam.OutputTypes
560
- output: "jpeg",
561
- //Which camera to use
562
- //Use Webcam.list() for results
563
- //false for default device
564
- device: false,
565
- // [location, buffer, base64]
566
- // Webcam.CallbackReturnTypes
567
- callbackReturn: "location",
568
- //Logging
569
- verbose: false
570
- };
571
- /**
572
- * Global output types
573
- * Various for platform
574
- *
575
- * @property Webcam.OutputTypes
576
- *
577
- * @type Object
578
- * @static
579
- *
580
- */ Webcam$3.OutputTypes = {
581
- "jpeg": "jpg",
582
- "png": "png",
583
- "bmp": "bmp"
584
- };
585
- /**
586
- * Global output types
587
- * Various for platform
588
- *
589
- * @property Webcam.CallbackReturnTypes
590
- *
591
- * @type Object
592
- * @static
593
- *
594
- */ Webcam$3.CallbackReturnTypes = {
595
- "default": "location",
596
- "location": "location",
597
- "buffer": "buffer",
598
- "base64": "base64" // String ex : "data..."
599
- };
600
- //Export
601
- var Webcam_1 = Webcam$3;
602
-
603
- /**
604
- * API for fswebcam
605
- *
606
- * @requires [ fswebcam ]
607
- *
608
- * @param Object options
609
- *
610
- */
611
- function _type_of(obj) {
612
- "@swc/helpers - typeof";
613
- return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
614
- }
615
- var Webcam$2 = Webcam_1;
616
- var Utils$2 = Utils_1;
617
- var Shot = Shot_1;
618
- //Main class
619
- function FSWebcam$1(options) {
620
- var scope = this;
621
- scope.opts = Utils$2.setDefaults(options, FSWebcam$1.Defaults);
622
- Webcam$2.call(scope, scope.opts);
623
- if (scope.opts.output === "png" && scope.opts.quality > 9) {
624
- scope.opts.quality = 9;
625
- }
626
- }
627
- FSWebcam$1.prototype = Object.create(Webcam$2.prototype);
628
- FSWebcam$1.prototype.constructor = FSWebcam$1;
629
- FSWebcam$1.prototype.bin = "fswebcam";
630
- /**
631
- * @override
632
- *
633
- * Create shot possibly from memory stdout
634
- *
635
- */ FSWebcam$1.prototype.createShot = function(location, data) {
636
- if (location === null) {
637
- var data = Buffer.from(data);
638
- }
639
- return new Shot(location, data);
640
- };
641
- /**
642
- * @override
643
- *
644
- * Generate shell statement
645
- *
646
- * @param String location
647
- *
648
- */ FSWebcam$1.prototype.generateSh = function(location) {
649
- var scope = this;
650
- var resolution = " -r " + scope.opts.width + "x" + scope.opts.height;
651
- // Adding frame rate
652
- var frames = scope.opts.frames ? "-F " + scope.opts.frames : "";
653
- var output = "--" + scope.opts.output;
654
- var quality = scope.opts.quality;
655
- var delay = scope.opts.delay ? "-D " + scope.opts.delay : "";
656
- var title = scope.opts.title ? "--title " + scope.opts.title : "";
657
- var subtitle = scope.opts.subtitle ? "--subtitle " + scope.opts.subtitle : "";
658
- var timestamp = scope.opts.timestamp ? "--timestamp " + scope.opts.timestamp : "";
659
- var device = scope.opts.device ? "-d " + scope.opts.device : "";
660
- var grey = scope.opts.greyscale ? "--greyscale" : "";
661
- var rotation = scope.opts.rotation ? "--rotate " + scope.opts.rotation : "";
662
- var banner = !scope.opts.topBanner && !scope.opts.bottomBanner ? "--no-banner" : scope.opts.topBanner ? "--top-banner" : "--bottom-banner";
663
- var skip = scope.opts.skip ? "--skip " + scope.opts.skip : "";
664
- if (scope.opts.saturation) {
665
- scope.opts.setValues.Saturation = scope.opts.saturation;
666
- }
667
- var setValues = scope.getControlSetString(scope.opts.setValues);
668
- var verbose = scope.opts.verbose ? "" : " -q";
669
- // Use memory if null location
670
- var shellLocation = location === null ? "- -" : location;
671
- var sh = scope.bin + " " + verbose + " " + resolution + " " + frames + " " + output + " " + quality + " " + delay + " " + title + " " + subtitle + " " + timestamp + " " + device + " " + grey + " " + rotation + " " + banner + " " + setValues + " " + skip + " " + shellLocation;
672
- return sh;
673
- };
674
- /**
675
- * Get control values string
676
- *
677
- * @param {Object} setValues
678
- *
679
- * @returns {String}
680
- *
681
- */ FSWebcam$1.prototype.getControlSetString = function(setValues) {
682
- var str = "";
683
- if ((typeof setValues === "undefined" ? "undefined" : _type_of(setValues)) !== "object") {
684
- return str;
685
- }
686
- for(var setName in setValues){
687
- var val = setValues[setName];
688
- if (!val) {
689
- continue;
690
- }
691
- // Add a space to separate values if there are multiple control values being set
692
- if (str.length > 0) {
693
- str += " ";
694
- }
695
- str += "-s ".concat(setName, "=").concat(val);
696
- }
697
- return str;
698
- };
699
- /**
700
- * Get shell statement to get the available camera controls
701
- *
702
- * @returns {String}
703
- *
704
- */ FSWebcam$1.prototype.getListControlsSh = function() {
705
- var scope = this;
706
- var devSwitch = scope.opts.device ? " --device=" + scope.opts.device.trim() : "";
707
- return scope.bin + devSwitch + " --list-controls";
708
- };
709
- /**
710
- * Parse output of list camera controls shell command
711
- *
712
- * @param {String} stdout Output of the list camera control shell command
713
- *
714
- * @param {Function(Array<CameraControl>)} callback Function that receives
715
- * camera controls
716
- *
717
- */ FSWebcam$1.prototype.parseListControls = function(stdout, callback) {
718
- var cameraControls = [];
719
- var inOptions = false;
720
- var prefixLength = 0;
721
- var headerRegExp = new RegExp("(?<prefix>.*)------------------\\s+-------------\\s+-----.*");
722
- var rangeRegExp = new RegExp("(?<name>.*?)" + "\\s+-?\\d+(?:\\s+\\(\\d+%\\))?\\s+" + "(?<min>-?\\d+)" + " - " + "(?<max>-?\\d+)", "i");
723
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
724
- try {
725
- for(var _iterator = stdout.split(/\n|\r|\n\r|\r\n/)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
726
- var line = _step.value;
727
- line = line.slice(prefixLength).trim();
728
- inOptions = inOptions && line.startsWith("-") ? false : inOptions;
729
- if (inOptions) {
730
- var rangeGroups = line.match(rangeRegExp);
731
- if (rangeGroups) {
732
- var name = rangeGroups.groups.name;
733
- var minRange = parseInt(rangeGroups.groups.min);
734
- var maxRange = parseInt(rangeGroups.groups.max);
735
- cameraControls.push({
736
- name: name,
737
- type: "range",
738
- min: minRange,
739
- max: maxRange
740
- });
741
- } else if (line.lastIndexOf("|") !== -1) {
742
- var opts = [];
743
- var opt = "";
744
- var name = "";
745
- var idx = line.lastIndexOf("|");
746
- while(idx !== -1){
747
- opt = line.slice(idx + 1).trim();
748
- opts.push(opt);
749
- var firstIdx = line.indexOf(opt);
750
- var lastIdx = line.lastIndexOf(opt);
751
- if (!name && firstIdx !== -1 && firstIdx !== lastIdx) {
752
- name = line.slice(0, firstIdx).trim();
753
- line = line.slice(firstIdx + opt.length);
754
- idx = line.lastIndexOf("|");
755
- }
756
- line = line.slice(0, idx).trim();
757
- idx = line.lastIndexOf("|");
758
- }
759
- if (name && line.trim()) {
760
- opts.push(line.trim());
761
- } else if (!name) {
762
- // Find largest number of words with two consecutive matches
763
- var words = line.split(" ").filter(function(item) {
764
- return Boolean(item);
765
- }).reverse();
766
- var num_words = 1;
767
- opt = words.slice(0, num_words).reverse().join(" ");
768
- var re = new RegExp(opt + "\\s+" + opt);
769
- while(!re.test(line)){
770
- num_words += 1;
771
- opt = words.slice(0, num_words).reverse().join(" ");
772
- re = new RegExp(opt + "\\s+" + opt);
773
- }
774
- var firstIdx = line.indexOf(opt);
775
- name = line.slice(0, firstIdx).trim();
776
- opts.push(opt);
777
- }
778
- cameraControls.push({
779
- name: name,
780
- type: "list",
781
- opts: opts.reverse()
782
- });
783
- }
784
- }
785
- var obj = line.match(headerRegExp);
786
- if (obj) {
787
- inOptions = true;
788
- // The output of the fswebcam --list-controls command has
789
- // terminal escape characters at the beginning of the each line
790
- prefixLength = obj.groups.prefix.length;
791
- }
792
- }
793
- } catch (err) {
794
- _didIteratorError = true;
795
- _iteratorError = err;
796
- } finally{
797
- try {
798
- if (!_iteratorNormalCompletion && _iterator.return != null) {
799
- _iterator.return();
800
- }
801
- } finally{
802
- if (_didIteratorError) {
803
- throw _iteratorError;
804
- }
805
- }
806
- }
807
- callback && callback(cameraControls);
808
- };
809
- /**
810
- * Data validations based on fs output
811
- *
812
- * @inheritdoc
813
- *
814
- */ FSWebcam$1.prototype.runCaptureValidations = function(data) {
815
- if (FSWebcam$1.Validations.noWebcam.exec(data)) {
816
- return new Error("No webcam found");
817
- }
818
- return null;
819
- };
820
- //Defaults
821
- FSWebcam$1.Defaults = {
822
- topBanner: false,
823
- bottomBanner: false,
824
- title: false,
825
- subTitle: false,
826
- timestamp: false,
827
- greyscale: false,
828
- saturation: false,
829
- skip: false,
830
- setValues: {}
831
- };
832
- //Validations const
833
- FSWebcam$1.Validations = {
834
- noWebcam: /no.*such.*(file|device)/i
835
- };
836
- //Export
837
- var FSWebcam_1 = FSWebcam$1;
838
-
839
- /**
840
- * API for imagesnap Mac OSX
841
- *
842
- * @requires [ imagesnap ]
843
- *
844
- * @param Object options
845
- *
846
- */
847
- var CHILD_PROCESS$1 = require$$0$1;
848
- var EXEC$1 = CHILD_PROCESS$1.exec;
849
- var Webcam$1 = Webcam_1;
850
- var Utils$1 = Utils_1;
851
- //Main class
852
- function ImageSnapWebcam$1(options) {
853
- var scope = this;
854
- scope.opts = Utils$1.setDefaults(options, ImageSnapWebcam$1.Defaults);
855
- //Without a delay imagesnap will not work
856
- //Test on macbook 2015 13' retina
857
- if (scope.opts.delay < 1) {
858
- scope.opts.delay = 1;
859
- }
860
- //Construct
861
- Webcam$1.call(scope, scope.opts);
862
- }
863
- ImageSnapWebcam$1.prototype = Object.create(Webcam$1.prototype);
864
- ImageSnapWebcam$1.prototype.constructor = ImageSnapWebcam$1;
865
- ImageSnapWebcam$1.prototype.bin = "imagesnap";
866
- /**
867
- * @override
868
- *
869
- * Generate shell statement
870
- *
871
- * @param String location
872
- *
873
- */ ImageSnapWebcam$1.prototype.generateSh = function(location) {
874
- var scope = this;
875
- var verbose = scope.opts.verbose ? "-v" : "-q";
876
- var delay = scope.opts.delay ? "-w " + scope.opts.delay : "";
877
- var device = scope.opts.device ? "-d '" + scope.opts.device + "'" : "";
878
- var sh = scope.bin + " " + delay + " " + device + " " + verbose + " " + location;
879
- return sh;
880
- };
881
- /**
882
- * @Override
883
- *
884
- * Webcam list
885
- *
886
- * @param Function callback
887
- *
888
- */ ImageSnapWebcam$1.prototype.list = function(callback) {
889
- var scope = this;
890
- var sh = scope.bin + " -l";
891
- var cams = [];
892
- EXEC$1(sh, function(err, data, out) {
893
- var lines = data.split("\n");
894
- var ll = lines.length;
895
- for(var i = 0; i < ll; i++){
896
- var line = lines[i];
897
- if (line === "Video Devices:" || !line) {
898
- continue;
899
- }
900
- //imagesnap update adds extra stuff
901
- line = line.replace(/.*?\[(.*?)\].*/, "$1");
902
- cams.push(line);
903
- }
904
- callback && callback(cams);
905
- });
906
- };
907
- //Defaults
908
- ImageSnapWebcam$1.Defaults = {
909
- delay: 1
910
- };
911
- //Export
912
- var ImageSnapWebcam_1 = ImageSnapWebcam$1;
913
-
914
- /**
915
- * API for Windows
916
- *
917
- * @requires [ CommandCam ]
918
- *
919
- * @param Object options
920
- *
921
- */
922
- var CHILD_PROCESS = require$$0$1;
923
- var EXEC = CHILD_PROCESS.exec;
924
- var Webcam = Webcam_1;
925
- var Utils = Utils_1;
926
- var Path = require$$3;
927
- //Main class
928
- function WindowsWebcam$1(options) {
929
- var scope = this;
930
- scope.opts = Utils.setDefaults(options, WindowsWebcam$1.Defaults);
931
- //Construct
932
- Webcam.call(scope, scope.opts);
933
- //command cam uses miliseconds
934
- scope.opts.delay = scope.opts.delay * 1000;
935
- }
936
- WindowsWebcam$1.prototype = Object.create(Webcam.prototype);
937
- WindowsWebcam$1.prototype.constructor = WindowsWebcam$1;
938
- WindowsWebcam$1.prototype.bin = "\"" + Path.resolve(__dirname, "..", "bindings", "CommandCam", "CommandCam.exe") + "\"";
939
- /**
940
- * @override
941
- *
942
- * Generate shell statement
943
- *
944
- * @param String location
945
- *
946
- */ WindowsWebcam$1.prototype.generateSh = function(location) {
947
- var scope = this;
948
- var device = scope.opts.device ? "/devnum " + scope.opts.device : "";
949
- var delay = scope.opts.delay ? "/delay " + scope.opts.delay : "";
950
- var sh = scope.bin + " " + delay + " " + device + " " + "/filename " + location;
951
- return sh;
952
- };
953
- /**
954
- * List webcam devices using bin
955
- *
956
- * @param Function callback
957
- *
958
- */ WindowsWebcam$1.prototype.list = function(callback) {
959
- var scope = this;
960
- var sh = scope.bin + " /devlist";
961
- var cams = [];
962
- EXEC(sh, function(err, data, out) {
963
- if (err) {
964
- throw err;
965
- }
966
- var lines = out.split("\n");
967
- var ll = lines.length;
968
- var camNum = 1;
969
- for(var i = 0; i < ll; i++){
970
- var line = lines[i];
971
- line = line.replace("\r", "");
972
- if (!!line && line !== "Available capture devices:" && "No video devices found") {
973
- cams.push(camNum.toString());
974
- camNum++;
975
- }
976
- }
977
- callback && callback(cams);
978
- });
979
- };
980
- //Defaults
981
- WindowsWebcam$1.Defaults = {
982
- output: "bmp"
983
- };
984
- //Export
985
- var WindowsWebcam_1 = WindowsWebcam$1;
986
-
987
- /**
988
- * Factory based on OS output
989
- *
990
- */
991
-
992
- var OS = require$$0;
993
- //Webcam types
994
- var FSWebcam = FSWebcam_1;
995
- var ImageSnapWebcam = ImageSnapWebcam_1;
996
- var WindowsWebcam = WindowsWebcam_1;
997
- //Main singleton
998
- var Factory = new function() {
999
- var scope = this;
1000
- //Main Class get
1001
- scope.create = function(options, type) {
1002
- var p = type || Factory.Platform;
1003
- var Type = Factory.Types[p];
1004
- if (!Type) {
1005
- throw new Error("Sorry, no webcam type specified yet for platform " + p);
1006
- }
1007
- return new Type(options);
1008
- };
1009
- };
1010
- Factory.Platform = OS.platform();
1011
- //OS Webcam types
1012
- Factory.Types = {
1013
- linux: FSWebcam,
1014
- darwin: ImageSnapWebcam,
1015
- fswebcam: FSWebcam,
1016
- win32: WindowsWebcam,
1017
- win64: WindowsWebcam
1018
- };
1019
- //Export
1020
- var Factory_1 = Factory;
1021
-
1022
- /**
1023
- * Main classes and use
1024
- *
1025
- * @module NodeWebcam
1026
- *
1027
- */
1028
- /**
1029
- * @class API
1030
- *
1031
- */ var NodeWebcam = {
1032
- version: "0.4.1",
1033
- REVISION: 4,
1034
- Factory: Factory_1,
1035
- Webcam: Factory_1,
1036
- FSWebcam: FSWebcam_1,
1037
- ImageSnapWebcam: ImageSnapWebcam_1,
1038
- WindowsWebcam: WindowsWebcam_1
1039
- };
1040
- //API
1041
- /**
1042
- * Main create
1043
- *
1044
- * @method create
1045
- *
1046
- * @param {Object} options
1047
- *
1048
- */ NodeWebcam.create = function(options) {
1049
- return NodeWebcam.Factory.create(options);
1050
- };
1051
- /**
1052
- * Quick capture helper
1053
- *
1054
- * @method capture
1055
- *
1056
- * @param {String} location
1057
- * @param {Object} options
1058
- * @param {Function} callback
1059
- *
1060
- */ NodeWebcam.capture = function(location, options, callback) {
1061
- var webcam = NodeWebcam.create(options);
1062
- webcam.capture(location, callback);
1063
- return webcam;
1064
- };
1065
- /**
1066
- * Camera list helper
1067
- *
1068
- * @method list
1069
- *
1070
- * @param {Function(Array<String>)} callback
1071
- *
1072
- */ NodeWebcam.list = function(callback) {
1073
- var cam = NodeWebcam.create({});
1074
- cam.list(callback);
1075
- };
1076
- /**
1077
- * @typedef CameraControl
1078
- * @type {Object}
1079
- * @param {String} name Control name, as it should appear in the setValues object
1080
- * @param {String} type Either "range" or "list"
1081
- * @param {Number} min For "range" controls, minimum control value
1082
- * @param {Number} max For "range" controls, maximum control value
1083
- * @param {Array(<String>)} opts For "list" controls, available control options
1084
- *
1085
- */ /**
1086
- * Camera options helper
1087
- *
1088
- * @method listControls
1089
- *
1090
- * @param {String} device
1091
- * @param {Function(Array<CameraControl>)} callback
1092
- *
1093
- */ NodeWebcam.listControls = function(device, callback) {
1094
- var cam = NodeWebcam.create({
1095
- device: device
1096
- });
1097
- cam.listControls(callback);
1098
- };
1099
- //Export
1100
- var nodeWebcam = NodeWebcam;
1101
-
1102
- let WebcamService = class WebcamService {
1103
- async capture(params, _pinsSettingsList, _context) {
1104
- const { width = 1280, height = 720, quality = 100, output = 'jpeg', device, verbose = false } = params;
1105
- const opts = {
1106
- width,
1107
- height,
1108
- quality,
1109
- delay: 0,
1110
- saveShots: false,
1111
- output,
1112
- device: device != null ? device : false,
1113
- callbackReturn: 'base64',
1114
- verbose
1115
- };
1116
- const webcam = nodeWebcam.create(opts);
1117
- const result = await new Promise((resolve, reject)=>{
1118
- webcam.capture("capture", (err, data)=>{
1119
- if (err) {
1120
- reject(err);
1121
- } else {
1122
- resolve(data);
1123
- }
1124
- });
1125
- });
1126
- webcam.clear();
1127
- return result;
1128
- }
1129
- async list(_params, _pinsSettingsList, _context) {
1130
- let webcam = nodeWebcam.create({});
1131
- const list = (await new Promise((resolve)=>{
1132
- webcam.list((data)=>{
1133
- resolve(data);
1134
- });
1135
- })).map((camera)=>camera.replace(/^=> /g, ''));
1136
- webcam.clear();
1137
- return list;
1138
- }
1139
- };
1140
- const capture = (params, pinsSettingsList, context)=>new WebcamService().capture(params, pinsSettingsList, context);
1141
- const list = (params, pinsSettingsList, context)=>new WebcamService().list(params, pinsSettingsList, context);
1142
-
1143
- export { capture, list };