add camera chooser - remove toggle button

use video prefs in startup function
This commit is contained in:
lukas fricke 2025-01-23 14:32:23 +01:00
parent cafac3f6ef
commit 85ea7d9147

View file

@ -56,14 +56,14 @@
<video autoplay="true" id="video"></video> <video autoplay="true" id="video"></video>
</div> </div>
<div class="col-4 "> <div class="col-4 ">
<div class = "row">
<button class="btn btn-primary float-end" onclick="takePicture();" id="start-button"> <label class = "form-label" for="camera_chooser">
Take photo Select a Camera
</button> </label>
<button class="btn btn-primary float-end" onclick="toggle_cam();"> <select class="form-select" aria-label="Default select example" name="camera_chooser" id = "camera_chooser">
Toggle camera
</button> </select>
</div>
<div id="control_elements" class="row align-items-end"> <div id="control_elements" class="row align-items-end">
<div class="col" name="length" > <div class="col" name="length" >
<label class = "form-label" for="real_length"> <label class = "form-label" for="real_length">
@ -150,6 +150,17 @@
</input> </input>
</div> </div>
</div> </div>
<div class="row">
<div class="col">
<label class = "form-check-label" for="default">
This should be the default Camera setup
</label>
</div>
<div class="col">
<input class="form-check-input" type="checkbox" id = "Default" name="default">
</input>
</div>
</div>
<button class="btn btn-primary float-end" type="submit">submit</button> <button class="btn btn-primary float-end" type="submit">submit</button>
</form> </form>
</div> </div>
@ -188,41 +199,69 @@
form.addEventListener("submit", add); form.addEventListener("submit", add);
function add(event){ function add(event){
event.preventDefault();
let microscope_setup = { let microscope_setup = {
"name" : document.getElementById("name").value, "name" : document.getElementById("name").value,
"FoV_Height" : document.getElementById("FoV_height").value, "FoV_Height" : document.getElementById("FoV_height").value,
"FoV_Width" : document.getElementById("FoV_width").value, "FoV_Width" : document.getElementById("FoV_width").value,
"Slip_Height" : document.getElementById("Slip_height").value, "Slip_Height" : document.getElementById("Slip_height").value,
"Slip_Width" : document.getElementById("Slip_width").value "Slip_Width" : document.getElementById("Slip_width").value,
"video_prefs" : video_prefs,
} }
let transaction = db.transaction(["microscope_setup"], "readwrite"); let transaction = db.transaction(["microscope_setup"], "readwrite");
let objectStore = transaction.objectStore("microscope_setup"); let objectStore = transaction.objectStore("microscope_setup");
let add_request = objectStore.add(microscope_setup); // (3) let add_request = objectStore.add(microscope_setup); // (3)
add_request.onsuccess = (event) => {
transaction.oncomplete = (event) => {
console.log("All done!"); console.log("All done!");
let id_of_setup = event.target.result;
if (document.getElementById("Default").checked){
let my_default = {"id" : 0, "microscope_setup" : id_of_setup};
let next_transaction = db.transaction(["defaults"], "readwrite");
let next_objectStore = next_transaction.objectStore("defaults");
let add_next_request = next_objectStore.put(my_default);
transaction.oncomplete = (event) => {
console.log("All done!");
};
transaction.onerror = (event) => {
console.log("something went wrong");
// Don't forget to handle errors!
};
}
}; };
transaction.onerror = (event) => { add_request.onerror = (event) => {
console.log("something went wrong"); console.log("something went wrong");
// Don't forget to handle errors! // Don't forget to handle errors!
}; };
event.preventDefault();
} }
</script>
<script>
var video = document.querySelector("#video"); var video = document.querySelector("#video");
var out = document.querySelector("#output"); var out = document.querySelector("#output");
var camera_chooser = document.querySelector("#camera_chooser")
// init video switching get IDs of video inputs // init video switching get IDs of video inputs
let video_prefs={ video: {width: { ideal: 99999} , height: { ideal: 99999 }}, audio: false };
let streaming = false;
let index = 1;
navigator.mediaDevices navigator.mediaDevices
.enumerateDevices() .enumerateDevices()
.then((devices) => { .then((devices) => {
devices.forEach((device) => { devices.forEach((device) => {
if (device.kind=="videoinput"){ if (device.kind=="videoinput"){
deviceIDs.push(device.deviceId); let cameraID = document.createElement("option");
let text = document.createTextNode("camera " + index);
if (index == 1){
video_prefs.video.deviceId = device.deviceId;
cameraID.defaultSelected = true;
}
++index;
cameraID.value = device.deviceId;
cameraID.appendChild(text);
camera_chooser.appendChild(cameraID);
} }
console.log(`${device.kind}: ${device.label} id = ${device.deviceId}`);
}); });
}) })
.catch((err) => { .catch((err) => {
@ -230,28 +269,11 @@ navigator.mediaDevices
}); });
// toggle between cameras
var deviceIDs = []; camera_chooser.addEventListener("change", change_cam);
var device_index = 0; function change_cam(){
video_prefs.video.deviceId = this.value;
function toggle_cam(){ startup();
device_index += 1
if (device_index>=deviceIDs.length){
device_index=0
};
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: {
deviceId: deviceIDs[device_index],
}, })
.then(function (stream) {
video.srcObject = stream;
})
.catch(function (error) {
console.log("Something went wrong!");
});
}
}; };
var real_dimensions = document.querySelector("#real_dimensions"); var real_dimensions = document.querySelector("#real_dimensions");
@ -338,7 +360,8 @@ function draw(e) {
} }
} }
let streaming = false; startup();
function startup() { function startup() {
output = document.getElementById("output"); output = document.getElementById("output");
@ -348,7 +371,7 @@ function draw(e) {
startButton = document.getElementById("start-button"); startButton = document.getElementById("start-button");
navigator.mediaDevices navigator.mediaDevices
.getUserMedia({ video: {width: { ideal: 99999} , height: { ideal: 99999 }}, audio: false }) .getUserMedia(video_prefs)
.then((stream) => { .then((stream) => {
video.srcObject = stream; video.srcObject = stream;
video.play(); video.play();
@ -427,10 +450,7 @@ function draw(e) {
} }
} }
// Set up our event listener to run the startup process
// once loading is complete.
window.addEventListener("load", startup, false);
//})();
</script> </script>
</html> </html>