-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathvideoHelp.js
174 lines (174 loc) · 6.62 KB
/
videoHelp.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
exports.browser = navigator;
function getMedia() {
return exports.browser.getUserMedia
|| exports.browser.webkitGetUserMedia
|| exports.browser.mozGetUserMedia
|| exports.browser.msGetUserMedia;
}
exports.getMedia = getMedia;
/*
export interface MediaDevice{
deviceId: string
kind: "videoinput" | "audioinput" | string
label: string
groupId: string
}*/
function dataUriToBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0) {
byteString = atob(dataURI.split(',')[1]);
}
else {
byteString = window['unescape'](dataURI.split(',')[1]);
}
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to a typed array
var ia = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], { type: mimeString });
}
exports.dataUriToBlob = dataUriToBlob;
/** single image to transmittable resource */
function dataUriToFormData(dataURI, options) {
options = options || {};
options.form = options.form || new FormData();
options.form.append('file', dataUriToBlob(dataURI), options.fileName || 'file.jpg');
return options.form;
}
exports.dataUriToFormData = dataUriToFormData;
function drawImageArrayToCanvas(imgArray) {
var canvas = document.createElement('canvas');
// const di = this.getVideoDimensions()
var ctx = canvas.getContext('2d');
var width = imgArray[0].split(';').length;
var height = imgArray.length;
canvas.width = width;
canvas.height = height;
ctx.clearRect(0, 0, width, height);
var externData = {
imgData: ctx.getImageData(0, 0, width, height),
pos: 0
};
var tmp = null;
for (var x = 0; x < imgArray.length; ++x) {
var col = imgArray[x].split(';');
for (var i = 0; i < width; i++) {
tmp = parseInt(col[i], 10);
externData.imgData.data[externData.pos + 0] = (tmp >> 16) & 0xff;
externData.imgData.data[externData.pos + 1] = (tmp >> 8) & 0xff;
externData.imgData.data[externData.pos + 2] = tmp & 0xff;
externData.imgData.data[externData.pos + 3] = 0xff;
externData.pos += 4;
}
/*if (externData.pos >= 4 * width * height) {
ctx.putImageData(externData.imgData, 0, 0);
externData.pos = 0;
}*/
}
ctx.putImageData(externData.imgData, 0, 0);
return canvas;
}
exports.drawImageArrayToCanvas = drawImageArrayToCanvas;
var Fallback = /** @class */ (function () {
function Fallback(videoObject) {
var _this = this;
this.onImage = new core_1.EventEmitter();
this.videoObject = videoObject;
var dataImgArray = [];
//method intended to live within window memory
this.debug = function (tag, message) {
if (tag == 'notify' && message == 'Capturing finished.') {
_this.onImage.emit(drawImageArrayToCanvas(dataImgArray));
}
};
//method intended to live within window memory
this.onCapture = function () {
dataImgArray.length = 0;
videoObject.save();
};
//method intended to live within window memory
this.onSave = function (data) {
dataImgArray.push(data);
};
//Flash swf file expects window.webcam to exist as communication bridge
window['webcam'] = this;
}
Fallback.prototype.captureToCanvas = function () {
var _this = this;
return new Promise(function (res, rej) {
var subscription = _this.onImage.subscribe(function (img) {
res(img);
subscription.unsubscribe();
});
_this.videoObject.capture();
});
};
Fallback.prototype.captureBase64 = function (mime) {
return this.captureToCanvas()
.then(function (canvas) { return canvas.toDataURL(mime || 'image/jpeg'); });
};
/**
* Add <param>'s into fallback object
* @param cam - Flash web camera instance
* @returns {void}
*/
Fallback.prototype.addFallbackParams = function (options) {
var paramFlashVars = document.createElement('param');
paramFlashVars.name = 'FlashVars';
paramFlashVars.value = 'mode=' + options.fallbackMode + '&quality=' + options.fallbackQuality;
this.videoObject.appendChild(paramFlashVars);
var paramAllowScriptAccess = document.createElement('param');
paramAllowScriptAccess.name = 'allowScriptAccess';
paramAllowScriptAccess.value = 'always';
this.videoObject.appendChild(paramAllowScriptAccess);
//is this even needed?
this.videoObject.classid = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
var paramMovie = document.createElement('param');
paramMovie.name = 'movie';
paramMovie.value = options.fallbackSrc;
this.videoObject.appendChild(paramMovie);
this.videoObject.data = options.fallbackSrc;
};
return Fallback;
}());
exports.Fallback = Fallback;
function videoInputsByDevices(devices) {
return devicesByKind(devices, 'videoinput');
}
exports.videoInputsByDevices = videoInputsByDevices;
function audioInputsByDevices(devices) {
return devicesByKind(devices, 'audioinput');
}
exports.audioInputsByDevices = audioInputsByDevices;
function audioOutputsByDevices(devices) {
return devicesByKind(devices, 'audiooutput');
}
exports.audioOutputsByDevices = audioOutputsByDevices;
function devicesByKind(devices, kind) {
return devices.filter(function (device) { return device.kind === kind; });
}
exports.devicesByKind = devicesByKind;
function promiseDeviceById(id) {
return promiseDevices().then(function (devices) { return devices.find(function (device) { return device.deviceId == id; }); });
}
exports.promiseDeviceById = promiseDeviceById;
function promiseDevices() {
//const x:Promise<MediaDeviceInfo[]> = browser.mediaDevices.enumerateDevices().then( devices=>devices )
return exports.browser.mediaDevices.enumerateDevices();
}
exports.promiseDevices = promiseDevices;
function isFacingModeSupported() {
if (!exports.browser.mediaDevices)
return false;
var contraints = exports.browser.mediaDevices.getSupportedConstraints();
return contraints.facingMode;
}
exports.isFacingModeSupported = isFacingModeSupported;
//# sourceMappingURL=videoHelp.js.map