Commit 2a2a0f9caf3b94a1052d833c412e1f06b4f4af06

Authored by Vladyslav
Committed by GitHub
1 parent f4ce41be

Added new settings to web-camera input widget (#2616)

... ... @@ -330,13 +330,13 @@
330 330 "name": "Web Camera Input",
331 331 "descriptor": {
332 332 "type": "latest",
333   - "sizeX": 9.5,
334   - "sizeY": 6.5,
  333 + "sizeX": 7.5,
  334 + "sizeY": 3,
335 335 "resources": [],
336 336 "templateHtml": "<tb-web-camera-widget ctx=\"ctx\">\n</tb-web-camera-widget>",
337   - "templateCss": "#container {\n overflow: auto;\n}\n\n.tbDatasource-container {\n margin: 5px;\n padding: 8px;\n}\n\n.tbDatasource-title {\n font-size: 1.200rem;\n font-weight: 500;\n padding-bottom: 10px;\n}\n\n.tbDatasource-table {\n width: 100%;\n box-shadow: 0 0 10px #ccc;\n border-collapse: collapse;\n white-space: nowrap;\n font-size: 1.000rem;\n color: #757575;\n}\n\n.tbDatasource-table td {\n position: relative;\n border-top: 1px solid rgba(0, 0, 0, 0.12);\n border-bottom: 1px solid rgba(0, 0, 0, 0.12);\n padding: 0px 18px;\n box-sizing: border-box;\n}",
  337 + "templateCss": "",
338 338 "controllerScript": "self.onInit = function() {\n var scope = self.ctx.$scope;\n scope.ctx = self.ctx;\n}\n\nself.onDataUpdated = function() {\n}\n\nself.typeParameters = function() {\n return {\n maxDatasources: 1,\n maxDataKeys: 1\n }\n}\n\nself.onDestroy = function() {\n}\n",
339   - "settingsSchema": "{}",
  339 + "settingsSchema": "{\n \"schema\": {\n \"type\": \"object\",\n \"title\": \"Web Camera\",\n \"properties\": {\n \"widgetTitle\": {\n \"title\": \"Widget title\",\n \"type\": \"string\",\n \"default\": \"\"\n },\n \"imageFormat\": {\n \"title\": \"Image Format\",\n \"type\": \"string\",\n \"default\": \"image/png\"\n },\n \"imageQuality\":{\n \"title\":\"Image quality that use lossy compression such as jpeg and webp\",\n \"type\":\"number\",\n \"default\": 0.92,\n \"min\": 0,\n \"max\": 1\n },\n \"maxWidth\": {\n \"title\": \"The maximal image width\",\n \"type\": \"number\",\n \"default\": 640\n }, \n \"maxHeight\": {\n \"title\": \"The maximal image heigth\",\n \"type\": \"number\",\n \"default\": 480\n }\n },\n \"required\": []\n },\n \"form\": [\n \"widgetTitle\",\n {\n \"key\": \"imageFormat\",\n \"type\": \"rc-select\",\n \"multiple\": false,\n \"items\": [\n {\n \"value\": \"image/jpeg\",\n \"label\": \"JPEG\"\n },\n {\n \"value\": \"image/png\",\n \"label\": \"PNG\"\n },\n {\n \"value\": \"image/webp\",\n \"label\": \"WEBP\"\n }\n ]\n },\n \"imageQuality\",\n \"maxWidth\",\n \"maxHeight\"\n ]\n}",
340 340 "dataKeySettingsSchema": "{}\n",
341 341 "defaultConfig": "{\"datasources\":[{\"type\":\"function\",\"name\":\"function\",\"dataKeys\":[{\"name\":\"f(x)\",\"type\":\"function\",\"label\":\"Random\",\"color\":\"#2196f3\",\"settings\":{},\"_hash\":0.15479322438769105,\"funcBody\":\"var value = prevValue + Math.random() * 100 - 50;\\nvar multiplier = Math.pow(10, 2 || 0);\\nvar value = Math.round(value * multiplier) / multiplier;\\nif (value < -1000) {\\n\\tvalue = -1000;\\n} else if (value > 1000) {\\n\\tvalue = 1000;\\n}\\nreturn value;\"}]}],\"timewindow\":{\"realtime\":{\"timewindowMs\":60000}},\"showTitle\":true,\"backgroundColor\":\"#fff\",\"color\":\"rgba(0, 0, 0, 0.87)\",\"padding\":\"8px\",\"settings\":{},\"title\":\"Web Camera Input\",\"showTitleIcon\":false,\"titleIcon\":\"more_horiz\",\"iconColor\":\"rgba(0, 0, 0, 0.87)\",\"iconSize\":\"24px\",\"titleTooltip\":\"\",\"dropShadow\":true,\"enableFullscreen\":false,\"widgetStyle\":{},\"titleStyle\":{\"fontSize\":\"16px\",\"fontWeight\":400},\"useDashboardTimewindow\":true,\"displayTimewindow\":true,\"showLegend\":false,\"actions\":{}}"
342 342 }
... ...
... ... @@ -52,6 +52,11 @@ function WebCameraWidgetController($element, $scope, $window, types, utils, attr
52 52 let canvas = null;
53 53 let photoCamera = null;
54 54 let dataKeyType = "";
  55 + let width = 640;
  56 + let height = 480;
  57 +
  58 + const DEFAULT_IMAGE_TYPE = 'image/jpeg';
  59 + const DEFAULT_IMAGE_QUALITY = 0.92;
55 60
56 61 vm.getStream = getStream;
57 62 vm.createPhoto = createPhoto;
... ... @@ -79,6 +84,8 @@ function WebCameraWidgetController($element, $scope, $window, types, utils, attr
79 84 vm.isEntityDetected = true;
80 85 }
81 86 }
  87 + width = vm.ctx.settings.maxWidth ? vm.ctx.settings.maxWidth : 640;
  88 + height = vm.ctx.settings.maxHeight ? vm.ctx.settings.maxWidth : 480;
82 89 if (datasource.dataKeys.length) {
83 90 $scope.currentKey = datasource.dataKeys[0].name;
84 91 dataKeyType = datasource.dataKeys[0].type;
... ... @@ -93,6 +100,24 @@ function WebCameraWidgetController($element, $scope, $window, types, utils, attr
93 100 }
94 101 });
95 102
  103 + function getVideoAspectRatio() {
  104 + if (videoElement.videoWidth && videoElement.videoWidth > 0 &&
  105 + videoElement.videoHeight && videoElement.videoHeight > 0) {
  106 + return videoElement.videoWidth / videoElement.videoHeight;
  107 + }
  108 + return width / height;
  109 + }
  110 +
  111 + vm.videoWidth = function() {
  112 + const videoRatio = getVideoAspectRatio();
  113 + return Math.min(width, height * videoRatio);
  114 + }
  115 +
  116 + vm.videoHeight = function() {
  117 + const videoRatio = getVideoAspectRatio();
  118 + return Math.min(height, width / videoRatio);
  119 + }
  120 +
96 121 function hasGetUserMedia() {
97 122 return !!($window.navigator.mediaDevices && $window.navigator.mediaDevices.getUserMedia);
98 123 }
... ... @@ -157,10 +182,12 @@ function WebCameraWidgetController($element, $scope, $window, types, utils, attr
157 182 }
158 183
159 184 function createPhoto() {
160   - canvas.width = videoElement.videoWidth;
161   - canvas.height = videoElement.videoHeight;
162   - canvas.getContext('2d').drawImage(videoElement, 0, 0);
163   - vm.previewPhoto = canvas.toDataURL('image/png');
  185 + canvas.width = vm.videoWidth();
  186 + canvas.height = vm.videoHeight();
  187 + canvas.getContext('2d').drawImage(videoElement, 0, 0, vm.videoWidth(), vm.videoHeight());
  188 + const mimeType = vm.ctx.settings.imageFormat ? vm.ctx.settings.imageFormat : DEFAULT_IMAGE_TYPE;
  189 + const quality = vm.ctx.settings.imageQuality ? vm.ctx.settings.imageQuality : DEFAULT_IMAGE_QUALITY;
  190 + vm.previewPhoto = canvas.toDataURL(mimeType, quality);
164 191 vm.isPreviewPhoto = true;
165 192 }
166 193
... ...