-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVdio.html
More file actions
122 lines (110 loc) · 3.27 KB
/
Vdio.html
File metadata and controls
122 lines (110 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE html>
<html>
<head>
<title>📷 Bharat Camera</title>
<style>
body {
text-align: center;
font-family: sans-serif;
background: #f0f0f0;
}
video, canvas {
width: 90%;
max-width: 400px;
border-radius: 8px;
margin: 10px 0;
}
select, button {
padding: 10px;
margin: 10px;
font-size: 16px;
border-radius: 5px;
}
</style>
</head>
<body>
<h2>📸 Bharat Camera</h2>
<select id="cameraList"></select><br>
<video id="video" autoplay playsinline muted></video><br>
<button onclick="startSelectedCamera()">🔄 Switch Camera</button>
<button onclick="takePhoto()">📸 Capture</button>
<canvas id="canvas" style="display:none;"></canvas>
<script>
const video = document.getElementById('video');
const canvas = document.getElementById('canvas');
const cameraList = document.getElementById('cameraList');
let currentStream = null;
let videoDevices = [];
async function requestPermissionFirst() {
try {
// Dummy request to ensure permission dialog shows
await navigator.mediaDevices.getUserMedia({ video: true });
} catch (e) {
alert("❌ Camera permission denied.");
}
}
async function getCameras() {
try {
const devices = await navigator.mediaDevices.enumerateDevices();
videoDevices = devices.filter(device => device.kind === 'videoinput');
cameraList.innerHTML = '';
videoDevices.forEach((device, index) => {
const option = document.createElement('option');
option.value = device.deviceId;
option.text = device.label || `Camera ${index + 1}`;
cameraList.appendChild(option);
});
} catch (err) {
alert("Camera list error: " + err.message);
}
}
async function startCamera(deviceId) {
if (currentStream) {
currentStream.getTracks().forEach(track => track.stop());
}
try {
const stream = await navigator.mediaDevices.getUserMedia({
video: { deviceId: { exact: deviceId } }
});
video.srcObject = stream;
currentStream = stream;
await video.play();
} catch (err) {
alert("❌ Camera not working: " + err.message);
fallbackCamera();
}
}
async function fallbackCamera() {
try {
const stream = await navigator.mediaDevices.getUserMedia({ video: true });
video.srcObject = stream;
currentStream = stream;
await video.play();
} catch (e) {
alert("🚫 No camera available: " + e.message);
}
}
function startSelectedCamera() {
const selectedId = cameraList.value;
if (selectedId) {
startCamera(selectedId);
}
}
function takePhoto() {
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
const ctx = canvas.getContext('2d');
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
canvas.style.display = 'block';
}
// Initialize
(async () => {
await requestPermissionFirst(); // ensure permission is asked first
await getCameras();
if (cameraList.options.length > 0) {
await startCamera(cameraList.value);
}
})();
</script>
</body>
</html>