Image3 / templates /index.html
nagasurendra's picture
Create templates/index.html
65da94c verified
raw
history blame
3.54 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Camera Capture</title>
<style>
#videoElement {
width: 100%;
height: auto;
}
#camera-controls {
display: flex;
justify-content: space-between;
margin-top: 10px;
}
#capture-button {
margin-top: 10px;
}
</style>
</head>
<body>
<h1>Camera Capture</h1>
<!-- Camera Icon -->
<button id="open-camera-btn" onclick="startCamera()">πŸ“·</button>
<!-- Video Capture Element -->
<video id="videoElement" autoplay></video>
<!-- Camera Control Buttons -->
<div id="camera-controls">
<button id="switch-camera-btn" onclick="switchCamera()">Switch Camera</button>
<button id="capture-button" onclick="captureImage()">Capture</button>
</div>
<!-- Image Display and Action Buttons -->
<div id="captured-image-section" style="display:none;">
<img id="captured-image" src="" alt="Captured Image" width="100%">
<button onclick="retakeImage()">Retake</button>
<button onclick="submitImage()">Submit</button>
</div>
<script>
let videoStream;
let currentStream;
let videoElement = document.getElementById('videoElement');
let capturedImageSection = document.getElementById('captured-image-section');
let capturedImage = document.getElementById('captured-image');
function startCamera() {
navigator.mediaDevices.enumerateDevices()
.then(devices => {
devices.forEach(device => {
if (device.kind === 'videoinput') {
currentStream = device.deviceId;
startStream(currentStream);
}
});
})
.catch(err => console.error("Error accessing camera:", err));
}
function startStream(deviceId) {
navigator.mediaDevices.getUserMedia({
video: { deviceId: { exact: deviceId } }
})
.then(stream => {
videoElement.srcObject = stream;
videoStream = stream;
})
.catch(err => console.error("Error accessing stream:", err));
}
function switchCamera() {
if (videoStream) {
videoStream.getTracks().forEach(track => track.stop());
}
startCamera();
}
function captureImage() {
let canvas = document.createElement('canvas');
canvas.width = videoElement.videoWidth;
canvas.height = videoElement.videoHeight;
canvas.getContext('2d').drawImage(videoElement, 0, 0, canvas.width, canvas.height);
let imageData = canvas.toDataURL('image/jpeg');
capturedImage.src = imageData;
capturedImageSection.style.display = 'block';
// Sending the captured image to Flask server
fetch('/capture', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: 'image=' + encodeURIComponent(imageData)
}).then(response => response.json())
.then(data => console.log(data.message));
}
function retakeImage() {
capturedImageSection.style.display = 'none';
startCamera();
}
function submitImage() {
fetch('/post_to_instagram', {
method: 'POST'
})
.then(response => response.json())
.then(data => {
alert(data.message);
});
}
</script>
</body>
</html>