Spaces:
Runtime error
Runtime error
Create camera.html
Browse files- templates/camera.html +204 -0
templates/camera.html
ADDED
@@ -0,0 +1,204 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Camera Capture</title>
|
7 |
+
|
8 |
+
<!-- Inline CSS -->
|
9 |
+
<style>
|
10 |
+
body {
|
11 |
+
font-family: Arial, sans-serif;
|
12 |
+
background-color: #f4f4f4;
|
13 |
+
margin: 0;
|
14 |
+
padding: 0;
|
15 |
+
}
|
16 |
+
|
17 |
+
.container {
|
18 |
+
max-width: 800px;
|
19 |
+
margin: 50px auto;
|
20 |
+
text-align: center;
|
21 |
+
}
|
22 |
+
|
23 |
+
h1 {
|
24 |
+
color: #333;
|
25 |
+
}
|
26 |
+
|
27 |
+
.btn {
|
28 |
+
background-color: #007bff;
|
29 |
+
color: white;
|
30 |
+
padding: 10px 20px;
|
31 |
+
border: none;
|
32 |
+
border-radius: 5px;
|
33 |
+
cursor: pointer;
|
34 |
+
margin: 10px;
|
35 |
+
}
|
36 |
+
|
37 |
+
.btn:hover {
|
38 |
+
background-color: #0056b3;
|
39 |
+
}
|
40 |
+
|
41 |
+
#videoElement {
|
42 |
+
width: 100%;
|
43 |
+
max-width: 600px;
|
44 |
+
margin: 20px 0;
|
45 |
+
}
|
46 |
+
|
47 |
+
#cameraSelect {
|
48 |
+
padding: 5px;
|
49 |
+
margin: 10px;
|
50 |
+
}
|
51 |
+
|
52 |
+
#capturedImage {
|
53 |
+
margin-top: 20px;
|
54 |
+
}
|
55 |
+
|
56 |
+
#hfResult {
|
57 |
+
text-align: left;
|
58 |
+
background: #fff;
|
59 |
+
padding: 10px;
|
60 |
+
border-radius: 5px;
|
61 |
+
margin-top: 20px;
|
62 |
+
max-height: 300px;
|
63 |
+
overflow-y: auto;
|
64 |
+
}
|
65 |
+
</style>
|
66 |
+
</head>
|
67 |
+
<body>
|
68 |
+
<div class="container">
|
69 |
+
<h1>Camera Capture</h1>
|
70 |
+
<div id="cameraContainer">
|
71 |
+
<select id="cameraSelect">
|
72 |
+
<option value="user">Front Camera</option>
|
73 |
+
<option value="environment">Back Camera</option>
|
74 |
+
</select>
|
75 |
+
<video id="videoElement" autoplay playsinline></video>
|
76 |
+
<button id="captureButton" class="btn">Capture Image</button>
|
77 |
+
</div>
|
78 |
+
<canvas id="canvas" style="display: none;"></canvas>
|
79 |
+
<div id="result">
|
80 |
+
<img id="capturedImage" style="display: none; max-width: 100%;">
|
81 |
+
<div id="actionButtons" style="display: none; margin-top: 10px;">
|
82 |
+
<button id="retakeButton" class="btn">Retake</button>
|
83 |
+
<button id="uploadButton" class="btn">Upload to Zapier</button>
|
84 |
+
</div>
|
85 |
+
</div>
|
86 |
+
</div>
|
87 |
+
|
88 |
+
<!-- Inline JavaScript -->
|
89 |
+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
|
90 |
+
<script>
|
91 |
+
$(document).ready(function() {
|
92 |
+
const video = document.getElementById('videoElement');
|
93 |
+
const canvas = document.getElementById('canvas');
|
94 |
+
const capturedImage = document.getElementById('capturedImage');
|
95 |
+
const cameraSelect = document.getElementById('cameraSelect');
|
96 |
+
const actionButtons = document.getElementById('actionButtons');
|
97 |
+
let stream = null;
|
98 |
+
let currentImageUrl = null;
|
99 |
+
|
100 |
+
// Open camera
|
101 |
+
$('#openCamera').click(function(e) {
|
102 |
+
e.preventDefault(); // Prevent link from refreshing page
|
103 |
+
$('#cameraContainer').show();
|
104 |
+
actionButtons.style.display = 'none';
|
105 |
+
capturedImage.style.display = 'none';
|
106 |
+
startCamera(cameraSelect.value);
|
107 |
+
});
|
108 |
+
|
109 |
+
// Switch camera
|
110 |
+
cameraSelect.addEventListener('change', function() {
|
111 |
+
if (stream) {
|
112 |
+
stopCamera();
|
113 |
+
startCamera(this.value);
|
114 |
+
}
|
115 |
+
});
|
116 |
+
|
117 |
+
// Capture image
|
118 |
+
$('#captureButton').click(function() {
|
119 |
+
canvas.width = video.videoWidth;
|
120 |
+
canvas.height = video.videoHeight;
|
121 |
+
canvas.getContext('2d').drawImage(video, 0, 0);
|
122 |
+
const dataUrl = canvas.toDataURL('image/jpeg');
|
123 |
+
|
124 |
+
// Send to server
|
125 |
+
$.ajax({
|
126 |
+
type: 'POST',
|
127 |
+
url: '/capture',
|
128 |
+
data: { image: dataUrl },
|
129 |
+
success: function(response) {
|
130 |
+
if (response.status === 'success') {
|
131 |
+
capturedImage.src = response.image_url;
|
132 |
+
capturedImage.style.display = 'block';
|
133 |
+
actionButtons.style.display = 'block';
|
134 |
+
currentImageUrl = response.image_url;
|
135 |
+
stopCamera();
|
136 |
+
$('#cameraContainer').hide();
|
137 |
+
} else {
|
138 |
+
alert('Error: ' + response.message);
|
139 |
+
}
|
140 |
+
},
|
141 |
+
error: function() {
|
142 |
+
alert('Failed to capture image.');
|
143 |
+
}
|
144 |
+
});
|
145 |
+
});
|
146 |
+
|
147 |
+
// Retake button
|
148 |
+
$('#retakeButton').click(function() {
|
149 |
+
$('#cameraContainer').show();
|
150 |
+
actionButtons.style.display = 'none';
|
151 |
+
capturedImage.style.display = 'none';
|
152 |
+
startCamera(cameraSelect.value);
|
153 |
+
});
|
154 |
+
|
155 |
+
// Upload to Zapier
|
156 |
+
$('#uploadButton').click(function() {
|
157 |
+
if (!currentImageUrl) {
|
158 |
+
alert('No image to upload.');
|
159 |
+
return;
|
160 |
+
}
|
161 |
+
$.ajax({
|
162 |
+
type: 'POST',
|
163 |
+
url: '/upload_zapier',
|
164 |
+
data: { image_url: currentImageUrl },
|
165 |
+
success: function(response) {
|
166 |
+
if (response.status === 'success') {
|
167 |
+
alert('Image sent to Zapier successfully!');
|
168 |
+
actionButtons.style.display = 'none';
|
169 |
+
capturedImage.style.display = 'none';
|
170 |
+
} else {
|
171 |
+
alert('Error sending to Zapier: ' + response.message);
|
172 |
+
}
|
173 |
+
},
|
174 |
+
error: function() {
|
175 |
+
alert('Failed to send image to Zapier.');
|
176 |
+
}
|
177 |
+
});
|
178 |
+
});
|
179 |
+
|
180 |
+
function startCamera(facingMode) {
|
181 |
+
const constraints = {
|
182 |
+
video: { facingMode: facingMode }
|
183 |
+
};
|
184 |
+
navigator.mediaDevices.getUserMedia(constraints)
|
185 |
+
.then(function(mediaStream) {
|
186 |
+
stream = mediaStream;
|
187 |
+
video.srcObject = stream;
|
188 |
+
})
|
189 |
+
.catch(function(err) {
|
190 |
+
alert('Error accessing camera: ' + err.message);
|
191 |
+
});
|
192 |
+
}
|
193 |
+
|
194 |
+
function stopCamera() {
|
195 |
+
if (stream) {
|
196 |
+
stream.getTracks().forEach(track => track.stop());
|
197 |
+
stream = null;
|
198 |
+
video.srcObject = null;
|
199 |
+
}
|
200 |
+
}
|
201 |
+
});
|
202 |
+
</script>
|
203 |
+
</body>
|
204 |
+
</html>
|