Skip to content

Commit 02cc5eb

Browse files
committed
(feat:JS): add functionalities to access rear camera
1 parent a2915a0 commit 02cc5eb

5 files changed

Lines changed: 64 additions & 9 deletions

File tree

public/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ <h1 class="picture-section-headline">You've got some ingredients but no idea wha
189189
<div class="video-btn-canvas">
190190
<video class="video"></video>
191191
<button class="take-picture">take a picture</button>
192+
<button id="switchCamera">Switch Camera</button>
192193
<canvas class="canvas"></canvas>
193194
<p class="chat-gpt-vision-text"></p>
194195
<button class="previous-page">previous page</button>

public/index.js

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ import {
5858
emailUserRecipeSection,
5959
} from "./js_utilities/query_selector.js";
6060

61+
let currentCameraIndex = 0;
62+
const switchCameraButton = document.getElementById('switchCamera');
63+
6164

6265

6366
wantToTakeAPicture.addEventListener("click", () => {
@@ -252,15 +255,63 @@ recipeButtons.forEach((button) => {
252255
});
253256

254257
// Picture section
255-
navigator.mediaDevices
256-
.getUserMedia(constraint)
257-
.then((stream) => {
258+
259+
260+
async function getVideoDevices() {
261+
const devices = await navigator.mediaDevices.enumerateDevices();
262+
return devices.filter(device => device.kind === 'videoinput');
263+
}
264+
265+
async function startCamera(deviceId) {
266+
const constraints = {
267+
audio: false,
268+
video: {
269+
deviceId: deviceId ? { exact: deviceId } : undefined,
270+
width: {min: 1024, ideal: 1280, max: 1920},
271+
height: {min: 576, ideal: 720, max: 1080}
272+
}
273+
};
274+
275+
try {
276+
const stream = await navigator.mediaDevices.getUserMedia(constraints);
258277
video.srcObject = stream;
259-
video.play();
260-
})
261-
.catch((error) => {
278+
await video.play();
279+
} catch (error) {
262280
console.error("Error accessing camera:", error);
263-
});
281+
}
282+
}
283+
284+
async function initializeCamera() {
285+
const videoDevices = await getVideoDevices();
286+
287+
if (videoDevices.length > 1) {
288+
switchCameraButton.style.display = 'block';
289+
switchCameraButton.addEventListener('click', () => {
290+
currentCameraIndex = (currentCameraIndex + 1) % videoDevices.length;
291+
startCamera(videoDevices[currentCameraIndex].deviceId);
292+
});
293+
} else {
294+
switchCameraButton.style.display = 'none';
295+
}
296+
297+
// Start with the rear camera if available
298+
const rearCameraDevice = videoDevices.find(device => device.label.toLowerCase().includes('back') || device.label.toLowerCase().includes('rear'));
299+
startCamera(rearCameraDevice ? rearCameraDevice.deviceId : videoDevices[0].deviceId);
300+
}
301+
302+
initializeCamera();
303+
304+
305+
306+
// navigator.mediaDevices
307+
// .getUserMedia(constraint)
308+
// .then((stream) => {
309+
// video.srcObject = stream;
310+
// video.play();
311+
// })
312+
// .catch((error) => {
313+
// console.error("Error accessing camera:", error);
314+
// });
264315

265316
function capturePhoto() {
266317
context.drawImage(video, 0, 0, 400, 100);

public/js_utilities/query_selector.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ const constraint = {
4242
audio: false,
4343
video: {
4444
width: {min: 1024, ideal: 1280, max: 1920},
45-
height: {min: 576, ideal: 720, max: 1080}
45+
height: {min: 576, ideal: 720, max: 1080},
46+
facingMode: { ideal: 'environment' } // tells the browser to prefer the rear camera if available.
47+
48+
4649
}
4750
}
4851

public/url_folder.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
https://oaidalleapiprodscus.blob.core.windows.net/private/org-nYbqgo3O0LNnYYAoKAmApBfx/user-58Je7efi0iy880e2UYCdYpBm/img-QhUaTMW5KjXvVEFVqpbnc4WK.png?st=2024-07-30T12%3A45%3A56Z&se=2024-07-30T14%3A45%3A56Z&sp=r&sv=2023-11-03&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-07-29T23%3A53%3A11Z&ske=2024-07-30T23%3A53%3A11Z&sks=b&skv=2023-11-03&sig=dk5PjiI69ok5GWveRY4lJjVFsgv1MhLFxe2OFGCWh5s%3D
1+
https://oaidalleapiprodscus.blob.core.windows.net/private/org-nYbqgo3O0LNnYYAoKAmApBfx/user-58Je7efi0iy880e2UYCdYpBm/img-fNuhmqRpsQhGAE0g5M9gQKUX.png?st=2024-07-31T11%3A35%3A07Z&se=2024-07-31T13%3A35%3A07Z&sp=r&sv=2023-11-03&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-07-30T22%3A58%3A24Z&ske=2024-07-31T22%3A58%3A24Z&sks=b&skv=2023-11-03&sig=0XASy0nJI13AwDlIlnWunniyYOuQMpEOWgTj82sluLc%3D

speech.mp3

290 KB
Binary file not shown.

0 commit comments

Comments
 (0)