-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
408 lines (365 loc) · 15.9 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>QR Code Generator and Decoder</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.section {
margin-bottom: 40px;
}
.qr-container {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.qr-item {
text-align: center;
width: 100%;
max-width: 200px;
}
.qr-item svg {
max-width: 100%;
height: auto;
margin-bottom: 10px;
}
.button {
display: inline-block;
padding: 10px 20px;
margin: 5px 0;
background-color: #4CAF50;
color: white;
text-align: center;
cursor: pointer;
border: none;
border-radius: 5px;
}
.button:hover {
background-color: #45a049;
}
input[type="file"] {
display: inline-block;
padding: 10px 20px;
margin: 5px 0;
background-color: #4CAF50;
color: white;
text-align: center;
cursor: pointer;
border: none;
border-radius: 5px;
}
input[type="file"]::file-selector-button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
input[type="file"]::file-selector-button:hover {
background-color: #45a049;
}
textarea {
width: 100%;
height: 100px;
margin-bottom: 10px;
}
input[type="number"] {
width: 100px;
}
#webcam {
display: none;
margin-bottom: 10px;
width: 320px; /* Smaller webcam preview */
}
#image-preview img {
max-width: 200px; /* Smaller image preview */
}
#hidden-image-preview {
display: none;
}
#capture-button {
display: none;
}
.hidden {
display: none;
}
.file-list {
list-style-type: decimal;
padding-left: 20px;
}
.file-list li {
margin-bottom: 5px;
}
#camera-options, #quality-options {
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="section">
<h2>Part One: Generate QR Codes</h2>
<textarea id="input-text" placeholder="Enter or paste your long text here..."></textarea>
<button class="button" onclick="pasteText()">Paste</button><br>
<label for="chunk-length">Chunk Length:</label>
<input type="number" id="chunk-length" value="2000" min="500" max="4000"><br>
<button class="button" onclick="generateQRs()">Generate QRs</button>
<button class="button hidden" id="download-all" onclick="downloadAllQRs()">Download All</button>
<div id="qr-output" class="qr-container"></div>
<div id="hidden-svg-container" class="hidden"></div>
</div>
<div class="section">
<h2>Part Two: Decode QR Codes</h2>
<input type="file" accept="image/*" id="file-input" onchange="loadFile(event)"><br>
<button class="button" onclick="openWebcam()">Open Webcam</button>
<div id="camera-options" class="hidden">
<label for="camera-select">Select Camera:</label>
<select id="camera-select"></select><br>
<label for="quality-select">Select Quality:</label>
<select id="quality-select">
<option value="low">Low</option>
<option value="medium">Medium</option>
<option value="high">High</option>
</select><br>
</div>
<video id="webcam" autoplay></video>
<button class="button" id="capture-button" onclick="captureImage()">Capture</button><br>
<canvas id="canvas" style="display:none;"></canvas>
<div id="image-preview"></div>
<div id="hidden-image-preview"></div>
<button class="button" onclick="decodeQR()">Decode</button>
<textarea id="current-scan" placeholder="Current Scan"></textarea>
<ul id="file-list" class="file-list"></ul>
<button class="button" onclick="nextText()">Next</button>
<button class="button" onclick="clearCurrent()">Clear</button>
<textarea id="total-text" placeholder="Total Text"></textarea>
<button class="button" onclick="clearAll()">Clear All</button>
<button class="button" onclick="copyTotal()">Copy</button>
</div>
<script src="https://unpkg.com/@zxing/library@latest"></script>
<script type="module">
import QrScanner from "https://unpkg.com/qr-scanner/qr-scanner.min.js";
let currentFileName = '';
let currentStream;
window.pasteText = async function() {
const text = await navigator.clipboard.readText();
document.getElementById('input-text').value = text;
}
window.generateQRs = async function() {
document.getElementById('download-all').classList.add('hidden');
const text = document.getElementById('input-text').value;
const chunkLength = parseInt(document.getElementById('chunk-length').value);
const qrOutput = document.getElementById('qr-output');
const hiddenSvgContainer = document.getElementById('hidden-svg-container');
qrOutput.innerHTML = '';
hiddenSvgContainer.innerHTML = '';
const chunks = [];
for (let i = 0; i < text.length; i += chunkLength) {
chunks.push(text.slice(i, i + chunkLength));
}
const writer = new ZXing.BrowserQRCodeSvgWriter();
for (let index = 0; index < chunks.length; index++) {
const chunk = chunks[index];
// SVG for display
const svgElementSmall = writer.write(chunk, 200, 200);
svgElementSmall.setAttribute('id', 'svg-small-' + index);
const qrItem = document.createElement('div');
qrItem.classList.add('qr-item');
const fileName = `qr_code_${index + 1}.png`;
const fileLabel = document.createElement('div');
fileLabel.innerText = fileName;
qrItem.appendChild(fileLabel);
qrItem.appendChild(svgElementSmall);
// SVG for hidden use (save and share)
const svgElementLarge = writer.write(chunk, 1500, 1500);
svgElementLarge.setAttribute('id', 'svg-large-' + index);
svgElementLarge.classList.add('hidden');
hiddenSvgContainer.appendChild(svgElementLarge);
const saveButton = document.createElement('button');
saveButton.innerText = 'Save';
saveButton.classList.add('button');
saveButton.onclick = () => saveQRCode(svgElementLarge, index);
const shareButton = document.createElement('button');
shareButton.innerText = 'Share';
shareButton.classList.add('button');
shareButton.style.marginLeft = '10px';
shareButton.onclick = () => shareQRCode(svgElementLarge);
qrItem.appendChild(saveButton);
qrItem.appendChild(shareButton);
qrOutput.appendChild(qrItem);
await new Promise(resolve => setTimeout(resolve, 0)); // Yield to event loop
}
document.getElementById('download-all').classList.remove('hidden');
}
window.saveQRCode = function(svgElement, index) {
const svgData = new XMLSerializer().serializeToString(svgElement);
const canvas = document.createElement('canvas');
canvas.width = 1500;
canvas.height = 1500;
const ctx = canvas.getContext('2d');
const img = new Image();
img.onload = () => {
ctx.fillStyle = "white"; // White background
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0);
const pngData = canvas.toDataURL('image/png');
const a = document.createElement('a');
a.href = pngData;
a.download = `qr_code_${index + 1}.png`;
a.click();
};
img.src = 'data:image/svg+xml;base64,' + btoa(svgData);
}
window.shareQRCode = function(svgElement) {
const svgData = new XMLSerializer().serializeToString(svgElement);
const canvas = document.createElement('canvas');
canvas.width = 1500;
canvas.height = 1500;
const ctx = canvas.getContext('2d');
const img = new Image();
img.onload = async () => {
ctx.fillStyle = "white"; // White background
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0);
canvas.toBlob(async (blob) => {
const file = new File([blob], 'qr_code.png', { type: 'image/png' });
if (navigator.canShare && navigator.canShare({ files: [file] })) {
await navigator.share({
files: [file],
title: 'QR Code',
text: 'Here is your QR Code',
});
} else {
alert('Sharing not supported on this browser.');
}
});
};
img.src = 'data:image/svg+xml;base64,' + btoa(svgData);
}
window.loadFile = function(event) {
const file = event.target.files[0];
currentFileName = file.name;
const image = document.createElement('img');
image.src = URL.createObjectURL(file);
image.onload = () => URL.revokeObjectURL(image.src); // Free memory
const preview = document.getElementById('image-preview');
preview.innerHTML = '';
preview.appendChild(image);
const hiddenPreview = document.createElement('img');
hiddenPreview.src = image.src;
hiddenPreview.onload = () => URL.revokeObjectURL(hiddenPreview.src); // Free memory
const hiddenPreviewContainer = document.getElementById('hidden-image-preview');
hiddenPreviewContainer.innerHTML = '';
hiddenPreviewContainer.appendChild(hiddenPreview);
}
window.openWebcam = async function() {
const video = document.getElementById('webcam');
const cameraOptions = document.getElementById('camera-options');
cameraOptions.classList.remove('hidden');
const captureButton = document.getElementById('capture-button');
const cameraSelect = document.getElementById('camera-select');
const qualitySelect = document.getElementById('quality-select');
const devices = await navigator.mediaDevices.enumerateDevices();
const videoDevices = devices.filter(device => device.kind === 'videoinput');
cameraSelect.innerHTML = '';
videoDevices.forEach((device, index) => {
const option = document.createElement('option');
option.value = device.deviceId;
option.text = device.label || `Camera ${index + 1}`;
cameraSelect.appendChild(option);
});
cameraSelect.onchange = startWebcam;
qualitySelect.onchange = startWebcam;
async function startWebcam() {
if (currentStream) {
currentStream.getTracks().forEach(track => track.stop());
}
const constraints = {
video: {
deviceId: { exact: cameraSelect.value },
width: qualitySelect.value === 'high' ? 1280 : qualitySelect.value === 'medium' ? 640 : 320
}
};
currentStream = await navigator.mediaDevices.getUserMedia(constraints);
video.srcObject = currentStream;
}
startWebcam();
video.style.display = 'block';
captureButton.style.display = 'inline-block';
}
window.captureImage = function() {
currentFileName = 'Webcam capture';
const video = document.getElementById('webcam');
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
context.drawImage(video, 0, 0);
const imagePreview = document.getElementById('image-preview');
const img = document.createElement('img');
img.src = canvas.toDataURL('image/png');
imagePreview.innerHTML = '';
imagePreview.appendChild(img);
const hiddenPreview = document.createElement('img');
hiddenPreview.src = canvas.toDataURL('image/png');
const hiddenPreviewContainer = document.getElementById('hidden-image-preview');
hiddenPreviewContainer.innerHTML = '';
hiddenPreviewContainer.appendChild(hiddenPreview);
video.style.display = 'none';
document.getElementById('capture-button').style.display = 'none';
document.getElementById('camera-options').classList.add('hidden');
video.srcObject.getTracks().forEach(track => track.stop());
}
window.decodeQR = async function() {
const imgElement = document.querySelector('#hidden-image-preview img');
if (!imgElement) {
alert('Please capture or upload an image first.');
return;
}
try {
const result = await QrScanner.scanImage(imgElement, { returnDetailedScanResult: true });
document.getElementById('current-scan').value = result.data;
addFileNameToList(currentFileName);
} catch (error) {
alert('No QR code found');
}
}
window.addFileNameToList = function(fileName) {
const fileList = document.getElementById('file-list');
const listItem = document.createElement('li');
listItem.innerText = fileName;
fileList.appendChild(listItem);
}
window.nextText = function() {
const currentScan = document.getElementById('current-scan').value;
const totalText = document.getElementById('total-text');
totalText.value += currentScan;
document.getElementById('current-scan').value = '';
}
window.clearCurrent = function() {
document.getElementById('current-scan').value = '';
}
window.clearAll = function() {
document.getElementById('current-scan').value = '';
document.getElementById('total-text').value = '';
document.getElementById('file-list').innerHTML = '';
}
window.copyTotal = async function() {
const totalText = document.getElementById('total-text').value;
await navigator.clipboard.writeText(totalText);
alert('Copied to clipboard!');
}
window.downloadAllQRs = function() {
const hiddenSvgContainer = document.getElementById('hidden-svg-container');
const svgElements = hiddenSvgContainer.querySelectorAll('svg');
svgElements.forEach((svgElement, index) => {
saveQRCode(svgElement, index);
});
}
</script>
</body>
</html>