Skip to content

Commit 8a9f4db

Browse files
committed
Apply supplied fix for change of camera
To fix issue #82
1 parent ff2990a commit 8a9f4db

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

node-red-node-ui-webcam/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-node-ui-webcam",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "A Node-RED ui node to capture images from a webcam.",
55
"author": "Nick O'Leary",
66
"license": "Apache-2.0",

node-red-node-ui-webcam/ui_webcam.js

+19-10
Original file line numberDiff line numberDiff line change
@@ -383,16 +383,25 @@ module.exports = function(RED) {
383383
$scope.$watch('msg', function(msg) {
384384
if (!msg) { return; }
385385
if (msg.camera !== undefined) {
386-
if (!isNaN(parseInt(msg.camera))) {
387-
var c = parseInt(msg.camera);
388-
if (c >= 0 || c < 16) {
389-
oldActiveCamera = activeCamera;
390-
$scope.disableCamera();
391-
activeCamera = null;
392-
window.localStorage.setItem("node-red-node-ui-webcam-activeCam",c);
393-
if (active !== false) {
394-
$scope.enableCamera();
395-
}
386+
let c = parseInt(msg.camera);
387+
if (!isNaN(c)) {
388+
// Check if cameras array is populated
389+
if ($scope.data.cameras && $scope.data.cameras.length > c) {
390+
$scope.changeCamera(c);
391+
} else {
392+
// Enumerate devices first, then change camera
393+
navigator.mediaDevices.enumerateDevices().then(function(devices) {
394+
$scope.data.cameras = devices.filter(function(device) {
395+
return device.kind === "videoinput";
396+
});
397+
if (c >= 0 && c < $scope.data.cameras.length) {
398+
$scope.changeCamera(c);
399+
} else {
400+
console.warn("Camera index out of range:", c);
401+
}
402+
}).catch(function(err) {
403+
console.error("Error enumerating devices:", err);
404+
});
396405
}
397406
}
398407
}

0 commit comments

Comments
 (0)