How to Live Camera Display on Website ?
SEO Help and Tips
How to Live Camera Display on Website ?
Here is example code of live camera Display:
<head>
<title>Live Camera Feed</title>
</head>
<body>
<video id="cameraFeed" autoplay></video>
<div>
<label for="resolutionSelect">Select Resolution:</label>
<select id="resolutionSelect">
<option value="default">Default</option>
<option value="hd">HD (1280x720)</option>
<option value="fullhd">Full HD (1920x1080)</option>
</select>
</div>
<script>
const cameraFeed = document.getElementById('cameraFeed');
const resolutionSelect = document.getElementById('resolutionSelect');
let currentStream;
async function accessCamera(constraints) {
try {
// Close the previous stream if it exists
if (currentStream) {
currentStream.getTracks().forEach(track => track.stop());
}
const stream = await navigator.mediaDevices.getUserMedia(constraints);
cameraFeed.srcObject = stream;
currentStream = stream;
} catch (error) {
console.error('Error accessing camera:', error);
}
}
resolutionSelect.addEventListener('change', () => {
const selectedValue = resolutionSelect.value;
let constraints = { video: true };
if (selectedValue === 'hd') {
constraints.video = { width: 1280, height: 720 };
} else if (selectedValue === 'fullhd') {
constraints.video = { width: 1920, height: 1080 };
}
accessCamera(constraints);
});
// Check browser compatibility and access the camera
window.addEventListener('DOMContentLoaded', async () => {
if ('mediaDevices' in navigator && 'getUserMedia' in navigator.mediaDevices) {
await accessCamera({ video: true });
} else {
console.error('Camera access is not supported in this browser.');
}
});
</script>
</body>
Comments
Post a Comment
Thanks for your Comments.