-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
214 lines (194 loc) · 7.42 KB
/
index.html
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Barcode Scanner</title>
<script src="https://unpkg.com/@zxing/library@0.21.3/umd/index.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f5f5f5;
}
#camera-container {
width: 100%;
max-width: 640px;
margin: 20px;
position: relative;
}
video {
width: 100%;
border: 2px solid #ccc;
border-radius: 5px;
}
#output {
margin-top: 20px;
}
.item {
display: flex;
justify-content: space-between;
width: 300px;
margin: 5px 0;
background: #fff;
padding: 10px;
border-radius: 5px;
border: 1px solid #ddd;
}
.total {
font-weight: bold;
margin-top: 10px;
}
#item-counter {
margin-top: 10px;
font-weight: bold;
}
button {
margin-top: 20px;
padding: 10px 20px;
background-color: #007BFF;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
#buzzer-button {
background-color: #FF6347;
margin-top: 10px;
}
#buzzer-button:hover {
background-color: #FF4500;
}
</style>
</head>
<body>
<div id="camera-container">
<video id="video" autoplay playsinline></video>
</div>
<div id="output">
<div id="item-counter">Items Scanned: 0</div>
<div class="total" id="total">Total: £0.00</div>
<div id="scanned-items"></div>
</div>
<button id="reset-button">Reset</button>
<button id="buzzer-button">Gameshow Buzzer</button>
<script>
const mockDatabasePath = './mock-database.json';
const bigSweepItemsPath = './big-sweep.json';
let mockDatabase = {};
let bigSweepItems = [];
// Load mock database
fetch(mockDatabasePath)
.then(response => response.json())
.then(data => {
mockDatabase = data;
})
.catch(error => console.error('Error loading mock database:', error));
// Load Big Sweep items
fetch(bigSweepItemsPath)
.then(response => response.json())
.then(data => {
bigSweepItems = data;
})
.catch(error => console.error('Error loading Big Sweep items:', error));
const scannedItems = [];
const scannedItemsContainer = document.getElementById('scanned-items');
const totalContainer = document.getElementById('total');
const itemCounter = document.getElementById('item-counter');
const resetButton = document.getElementById('reset-button');
const buzzerButton = document.getElementById('buzzer-button');
const videoElement = document.getElementById('video');
let total = 0;
let scanningPaused = false;
const beepSuccess = new Audio('./success-beep.mp3');
const beepFail = new Audio('./error-beep.mp3');
const gameshowBuzzer = new Audio('./buzzer-sound.mp3');
const updateUI = () => {
scannedItemsContainer.innerHTML = '';
total = 0;
scannedItems.forEach(item => {
const itemDiv = document.createElement('div');
itemDiv.classList.add('item');
itemDiv.innerHTML = `<span>${item.name}</span><span>£${item.price.toFixed(2)}</span>`;
scannedItemsContainer.appendChild(itemDiv);
total += item.price;
});
totalContainer.textContent = `Total: £${total.toFixed(2)}`;
itemCounter.textContent = `Items Scanned: ${scannedItems.length}`;
};
const reset = () => {
scannedItems.length = 0;
updateUI();
};
resetButton.addEventListener('click', reset);
buzzerButton.addEventListener('click', () => {
gameshowBuzzer.play();
});
const startScanner = async () => {
try {
const stream = await navigator.mediaDevices.getUserMedia({
video: {
facingMode: "environment"
}
});
videoElement.srcObject = stream;
const videoTracks = stream.getVideoTracks();
const settings = videoTracks[0].getSettings();
console.log('Camera settings:', settings);
// Workaround for iOS Safari
videoElement.onloadedmetadata = () => {
if (videoElement.videoWidth && videoElement.videoHeight) {
const aspectRatio = videoElement.videoWidth / videoElement.videoHeight;
videoElement.style.width = `${640}px`;
videoElement.style.height = `${640 / aspectRatio}px`;
}
};
const codeReader = new ZXing.BrowserMultiFormatReader();
await codeReader.decodeFromVideoDevice(null, videoElement, async (result, err) => {
if (result && !scanningPaused) {
const code = result.text;
if (scannedItems.some(item => item.code === code)) {
beepFail.play();
} else if (mockDatabase[code]) {
scannedItems.push({ ...mockDatabase[code], code });
beepSuccess.play();
scanningPaused = true;
videoElement.style.filter = 'brightness(50%)';
setTimeout(() => {
videoElement.style.filter = 'brightness(100%)';
scanningPaused = false;
}, 500);
} else if (bigSweepItems.includes(code)) {
const randomValue = Math.floor(Math.random() * (300 - 50 + 1)) + 50;
scannedItems.push({ name: "Big Sweep Item", price: randomValue, code });
beepSuccess.play();
scanningPaused = true;
videoElement.style.filter = 'brightness(50%)';
setTimeout(() => {
videoElement.style.filter = 'brightness(100%)';
scanningPaused = false;
}, 500);
} else {
beepFail.play();
}
updateUI();
}
});
} catch (error) {
console.error('Camera error:', error);
alert("Unable to access the camera. Please ensure permissions are granted.");
}
};
startScanner();
</script>
</body>
</html>