-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQR_code_and_reader.html
More file actions
684 lines (608 loc) · 30.2 KB
/
QR_code_and_reader.html
File metadata and controls
684 lines (608 loc) · 30.2 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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Suite QR : Générateur & Scanner Ultimate</title>
<!-- 1. Charger Tailwind D'ABORD -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- 2. Configurer Tailwind ENSUITE -->
<script>
tailwind.config = {
darkMode: 'class',
theme: {
extend: {
colors: {
gray: {
750: '#2d3748',
850: '#1a202c',
950: '#0d1117',
}
}
}
}
}
</script>
<!-- 3. Gestion du Thème (Immédiat) -->
<script>
window.toggleTheme = function() {
const html = document.documentElement;
if (html.classList.contains('dark')) {
html.classList.remove('dark');
localStorage.setItem('theme', 'light');
} else {
html.classList.add('dark');
localStorage.setItem('theme', 'dark');
}
};
(function() {
const storedTheme = localStorage.getItem('theme');
if (storedTheme === 'dark') {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
})();
</script>
<!-- Librairies QR -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcode-generator/1.4.4/qrcode.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
<script src="https://unpkg.com/html5-qrcode@2.3.8/html5-qrcode.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jsqr@1.4.0/dist/jsQR.js"></script>
<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
/* --- STYLE GLOBAL & CUSTOM --- */
body {
font-family: 'Inter', system-ui, -apple-system, sans-serif;
transition: background-color 0.3s, color 0.3s;
}
/* Scrollbar personnalisée */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }
.dark ::-webkit-scrollbar-thumb { background: #475569; }
::-webkit-scrollbar-thumb:hover { background: #94a3b8; }
/* Layout Principal */
.app-container {
display: flex;
flex-direction: column;
min-height: 100vh;
}
/* --- STYLES DESKTOP --- */
@media (min-width: 768px) {
body { height: 100vh; overflow: hidden; }
.app-container { flex-direction: row; height: 100%; }
.generator-panel {
flex: 0 0 450px;
border-right-width: 1px;
height: 100%;
/* Important pour le scroll */
overflow-y: auto;
z-index: 20;
box-shadow: 4px 0 24px rgba(0,0,0,0.05);
}
.scanner-panel { flex: 1; height: 100%; overflow: hidden; }
}
/* --- GÉNÉRATEUR --- */
.generator-panel {
display: flex;
flex-direction: column;
}
/* --- SCANNER --- */
.scanner-panel {
display: flex;
flex-direction: column;
position: relative;
min-height: 500px;
flex: 1;
overflow: hidden;
}
/* Onglets Scanner */
.scanner-tabs {
display: flex;
padding: 0.5rem;
box-shadow: 0 1px 3px rgba(0,0,0,0.05);
z-index: 20;
position: sticky;
top: 0;
}
.tab-btn {
flex: 1;
padding: 0.75rem;
text-align: center;
border-radius: 0.5rem;
font-weight: 600;
cursor: pointer;
transition: all 0.2s;
}
/* Zone Caméra / Fichier */
.scanner-viewport {
flex: 1;
position: relative;
display: flex;
align-items: center;
justify-content: center;
background: #000;
overflow: hidden;
min-height: 400px;
}
#camera-view {
width: 100%; height: 100%;
position: absolute; top: 0; left: 0;
overflow: hidden;
}
#reader video {
width: 100% !important; height: 100% !important;
object-fit: cover !important;
border-radius: 0 !important;
}
/* Zone Drop Fichier */
.file-drop-zone {
width: 100%; height: 100%;
display: flex; flex-direction: column;
align-items: center;
justify-content: center;
border: 3px dashed;
margin: 1rem; border-radius: 1rem;
transition: all 0.3s;
}
/* --- RÉSULTAT SCAN (Flottant) --- */
.scan-result-overlay {
position: absolute; bottom: 0; left: 0; right: 0;
border-top-left-radius: 1.5rem; border-top-right-radius: 1.5rem;
padding: 1.5rem;
box-shadow: 0 -4px 20px rgba(0,0,0,0.2);
transform: translateY(100%);
visibility: hidden;
transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1), visibility 0s 0.3s;
z-index: 50;
}
.scan-result-overlay.visible {
transform: translateY(0);
visibility: visible;
transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1), visibility 0s 0s;
}
/* --- QR DISPLAY --- */
#qrcode-display canvas, #qrcode-display img {
max-width: 100%; height: auto;
border-radius: 0.5rem;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
image-rendering: pixelated;
/* Correction 1: Limiter la hauteur drastiquement pour laisser voir les boutons */
max-height: 40vh;
object-fit: contain;
}
/* --- FULLSCREEN OVERLAY --- */
#fs-overlay {
position: fixed; top: 0; left: 0; width: 100vw; height: 100vh;
background: black;
z-index: 9999;
display: none;
justify-content: center; align-items: center;
cursor: zoom-out;
}
#fs-overlay img {
width: 100vw; height: 100vh;
object-fit: contain; image-rendering: pixelated;
}
/* --- TOGGLE THEME & RESET --- */
.icon-btn {
width: 40px; height: 40px;
border-radius: 50%;
display: flex; align-items: center; justify-content: center;
cursor: pointer;
transition: all 0.3s ease;
position: relative;
z-index: 1000;
border: 1px solid rgba(0,0,0,0.1);
}
.icon-btn i {
pointer-events: none;
}
</style>
</head>
<body class="bg-gray-100 text-gray-800 dark:bg-gray-900 dark:text-gray-100">
<div class="app-container">
<!-- ========================================== -->
<!-- PANNEAU GAUCHE : GÉNÉRATEUR -->
<!-- ========================================== -->
<div class="generator-panel bg-white dark:bg-gray-800 border-gray-200 dark:border-gray-700 p-6">
<header class="mb-6 flex justify-between items-start flex-shrink-0">
<div>
<h1 class="text-2xl font-bold text-gray-800 dark:text-white flex items-center gap-2">
<i class="fa-solid fa-qrcode text-blue-600 dark:text-blue-400"></i> Suite QR <span class="text-xs bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200 px-2 py-1 rounded-full">Ultimate</span>
</h1>
<p class="text-sm text-gray-500 dark:text-gray-400 mt-1">Générateur haute densité & Scanner</p>
</div>
<div class="flex gap-2">
<!-- Bouton Reset -->
<button onclick="resetApp()" class="icon-btn bg-red-50 dark:bg-red-900/30 hover:bg-red-100 dark:hover:bg-red-900/50 text-red-500 dark:text-red-400" title="Réinitialiser l'application">
<i class="fa-solid fa-rotate-right"></i>
</button>
<!-- Bouton Thème -->
<button onclick="toggleTheme()" class="icon-btn bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 text-gray-600 dark:text-yellow-400" title="Changer le mode">
<i class="fa-solid fa-sun hidden dark:inline"></i>
<i class="fa-solid fa-moon dark:hidden"></i>
</button>
</div>
</header>
<!-- Zone Input -->
<div class="mb-4 relative z-10 flex-shrink-0">
<label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Contenu à encoder</label>
<textarea id="textInput" rows="3" class="w-full p-3 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none transition resize-none text-gray-900 dark:text-white placeholder-gray-400 dark:placeholder-gray-500" placeholder="URL, Texte, Wi-Fi..."></textarea>
<div class="flex flex-col gap-2 mt-2">
<span id="charCount" class="text-xs text-gray-500 dark:text-gray-400 text-right">0 caractères</span>
<button id="clearBtn" class="w-full flex items-center justify-center gap-2 bg-red-50 dark:bg-red-900/30 hover:bg-red-100 dark:hover:bg-red-900/50 text-red-600 dark:text-red-400 border border-red-200 dark:border-red-800 py-2 rounded-lg font-medium transition">
<i class="fa-solid fa-trash-can"></i> Effacer texte
</button>
</div>
</div>
<!-- Zone QR Affichage (Flexible avec scroll auto si trop grand) -->
<div class="flex-grow flex flex-col items-center justify-center min-h-[200px] bg-gray-50 dark:bg-gray-750 rounded-xl border border-dashed border-gray-300 dark:border-gray-600 mb-6 relative group mt-6 z-0" id="qrcode-container">
<div id="qrcode-display" class="p-2 bg-white rounded-lg shadow-sm">
<div class="text-gray-400 dark:text-gray-500 text-center p-4">
<i class="fa-regular fa-image text-3xl mb-2"></i><br>Le QR apparaîtra ici
</div>
</div>
<button id="expandBtn" class="hidden absolute top-2 right-2 bg-gray-800 text-white p-2 rounded-lg shadow-lg hover:bg-black transition">
<i class="fa-solid fa-expand"></i>
</button>
</div>
<!-- Boutons d'action (Fixés pour ne pas disparaitre, flex-shrink-0) -->
<div class="grid grid-cols-3 gap-3 mb-6 flex-shrink-0">
<button onclick="downloadQR('png')" class="flex items-center justify-center gap-2 py-2 px-4 bg-white dark:bg-gray-700 border border-gray-200 dark:border-gray-600 rounded-lg text-sm font-medium hover:bg-gray-50 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition shadow-sm">
<i class="fa-regular fa-file-image"></i> PNG
</button>
<button onclick="downloadQR('jpeg')" class="flex items-center justify-center gap-2 py-2 px-4 bg-white dark:bg-gray-700 border border-gray-200 dark:border-gray-600 rounded-lg text-sm font-medium hover:bg-gray-50 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition shadow-sm">
<i class="fa-regular fa-file-image"></i> JPG
</button>
<button onclick="downloadQR('pdf')" class="flex items-center justify-center gap-2 py-2 px-4 bg-white dark:bg-gray-700 border border-gray-200 dark:border-gray-600 rounded-lg text-sm font-medium hover:bg-gray-50 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition shadow-sm">
<i class="fa-regular fa-file-pdf"></i> PDF
</button>
</div>
</div>
<!-- ========================================== -->
<!-- PANNEAU DROITE : SCANNER -->
<!-- ========================================== -->
<div class="scanner-panel bg-gray-50 dark:bg-gray-900">
<div class="scanner-tabs bg-white dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700">
<div class="tab-btn active text-blue-600 dark:text-blue-400 bg-blue-50 dark:bg-blue-900/30" onclick="setScannerMode('file')" id="tab-file">
<i class="fa-solid fa-file-import mr-2"></i>Fichier
</div>
<div class="tab-btn text-gray-500 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700" onclick="setScannerMode('camera')" id="tab-camera">
<i class="fa-solid fa-camera mr-2"></i>Caméra
</div>
</div>
<div class="scanner-viewport border-b border-gray-200 dark:border-gray-800">
<div id="camera-view" class="hidden">
<div id="reader">
<video id="scanVideo" playsinline muted autoplay></video>
<canvas id="scanCanvas" hidden></canvas>
</div>
<div id="camera-permission-msg" class="absolute inset-0 flex flex-col items-center justify-center bg-gray-900 text-white p-6 text-center hidden z-50">
<i class="fa-solid fa-video-slash text-4xl mb-4 text-gray-500"></i>
<h3 class="font-bold text-lg mb-2">Caméra inactive</h3>
<p class="text-sm text-gray-400 mb-4" id="cam-error-text">Veuillez autoriser l'accès à la caméra pour scanner.</p>
<button onclick="startCamera()" class="bg-blue-600 hover:bg-blue-700 text-white px-6 py-2 rounded-full font-medium transition">Réessayer</button>
</div>
</div>
<div id="file-view" class="w-full h-full p-4 bg-gray-50 dark:bg-gray-900">
<div class="file-drop-zone border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-800 text-gray-500 dark:text-gray-400" id="dropZone">
<i class="fa-solid fa-cloud-arrow-up text-5xl mb-4 text-blue-200 dark:text-blue-900"></i>
<p class="font-medium text-lg text-gray-700 dark:text-gray-200">Glissez une image ici</p>
<p class="text-sm mt-1 mb-4">ou collez avec Ctrl+V</p>
<button onclick="document.getElementById('fileInput').click()" class="bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-200 px-6 py-2 rounded-full font-medium hover:bg-gray-50 dark:hover:bg-gray-600 transition shadow-sm">
Choisir un fichier
</button>
<input type="file" id="fileInput" accept="image/*" hidden>
</div>
</div>
</div>
<div id="resultOverlay" class="scan-result-overlay bg-white dark:bg-gray-800 border-t border-gray-200 dark:border-gray-700">
<div class="flex justify-between items-start mb-3">
<div class="flex items-center gap-2">
<div class="w-8 h-8 rounded-full bg-green-100 dark:bg-green-900 text-green-600 dark:text-green-300 flex items-center justify-center">
<i class="fa-solid fa-check"></i>
</div>
<span class="font-bold text-gray-800 dark:text-white">QR Détecté</span>
</div>
<button onclick="closeResult()" class="text-gray-400 hover:text-gray-600 dark:hover:text-gray-200 p-1">
<i class="fa-solid fa-times text-xl"></i>
</button>
</div>
<div class="bg-gray-50 dark:bg-gray-900 border border-gray-200 dark:border-gray-700 rounded-lg p-3 mb-4 break-all max-h-32 overflow-y-auto font-mono text-sm text-gray-700 dark:text-gray-300" id="scanContent">
...
</div>
<div class="grid grid-cols-2 gap-3" id="actionButtons"></div>
</div>
</div>
</div>
<div id="fs-overlay" onclick="this.style.display='none'">
<img id="fs-img" src="" alt="Full Screen QR">
</div>
<div id="toast" class="fixed top-5 left-1/2 transform -translate-x-1/2 bg-gray-800 dark:bg-white text-white dark:text-gray-900 px-4 py-2 rounded-full shadow-lg text-sm font-medium opacity-0 transition-opacity pointer-events-none z-[10000]">
Message ici
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
/* ================= LOGIQUE GLOBALE ================= */
// Variable globale pour stocker le résultat scanné (fix pour le bouton copier)
window.lastScannedText = "";
// Réinitialisation Totale
window.resetApp = function() {
// 1. Reset Générateur
const textInput = document.getElementById('textInput');
if(textInput) {
textInput.value = '';
document.getElementById('charCount').textContent = '0 caractères';
generate('');
}
// 2. Reset Scanner
resetScannerState();
setScannerMode('file'); // Revenir à l'onglet par défaut
// 3. Feedback
showToast("Application réinitialisée !");
};
/* ================= LOGIQUE GÉNÉRATEUR ================= */
const textInput = document.getElementById('textInput');
const qrDisplay = document.getElementById('qrcode-display');
const charCount = document.getElementById('charCount');
const expandBtn = document.getElementById('expandBtn');
if(textInput) {
textInput.addEventListener('input', () => {
const text = textInput.value;
charCount.textContent = `${text.length} caractères`;
generate(text);
});
}
const clearBtn = document.getElementById('clearBtn');
if(clearBtn) {
clearBtn.addEventListener('click', () => {
textInput.value = '';
charCount.textContent = '0 caractères';
generate('');
});
}
function generate(text) {
if (!text.trim()) {
qrDisplay.innerHTML = `<div class="text-gray-400 dark:text-gray-500 text-center p-4"><i class="fa-regular fa-image text-3xl mb-2"></i><br>Le QR apparaîtra ici</div>`;
expandBtn.classList.add('hidden');
return;
}
try {
// Pour supporter les gros textes, on augmente le typeNumber si besoin (0 = auto)
const qr = qrcode(0, 'L');
qr.addData(text);
qr.make();
// On garde 6 pour la taille des cellules, mais l'image peut devenir grande
const imgHtml = qr.createImgTag(6, 4);
qrDisplay.innerHTML = imgHtml;
const img = qrDisplay.querySelector('img');
if(img) {
img.style.maxWidth = "100%";
img.style.height = "auto";
img.style.borderRadius = "8px";
}
expandBtn.classList.remove('hidden');
expandBtn.onclick = () => {
document.getElementById('fs-img').src = img.src;
document.getElementById('fs-overlay').style.display = 'flex';
};
} catch (e) {
console.error(e);
qrDisplay.innerHTML = `<p class="text-red-500 text-sm text-center">Texte trop long !</p>`;
}
}
window.downloadQR = (fmt) => {
const img = qrDisplay.querySelector('img');
if (!img) return showToast('Rien à télécharger');
const canvas = document.createElement('canvas');
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
if (fmt === 'pdf') {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
const imgProps = pdf.getImageProperties(canvas.toDataURL('image/png'));
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;
pdf.addImage(canvas.toDataURL('image/png'), 'PNG', 0, 10, pdfWidth, pdfHeight);
pdf.save('qrcode.pdf');
} else {
const link = document.createElement('a');
link.download = `qrcode.${fmt}`;
link.href = canvas.toDataURL(`image/${fmt}`);
link.click();
}
showToast(`Téléchargé en ${fmt.toUpperCase()}`);
};
/* ================= LOGIQUE SCANNER ================= */
let isScanning = false;
let currentMode = 'file';
let videoStream = null;
let animationFrameId = null;
const videoElement = document.getElementById('scanVideo');
const canvasElement = document.getElementById('scanCanvas');
const canvasCtx = canvasElement ? canvasElement.getContext('2d') : null;
const permissionMsg = document.getElementById('camera-permission-msg');
window.setScannerMode = function(mode) {
resetScannerState();
currentMode = mode;
document.querySelectorAll('.tab-btn').forEach(b => {
b.classList.remove('active', 'text-blue-600', 'bg-blue-50', 'dark:text-blue-400', 'dark:bg-blue-900/30');
b.classList.add('text-gray-500', 'dark:text-gray-400');
});
const activeTab = document.getElementById(`tab-${mode}`);
if(activeTab) {
activeTab.classList.add('active', 'text-blue-600', 'bg-blue-50', 'dark:text-blue-400', 'dark:bg-blue-900/30');
activeTab.classList.remove('text-gray-500', 'dark:text-gray-400');
}
const camView = document.getElementById('camera-view');
const fileView = document.getElementById('file-view');
if(camView) camView.classList.toggle('hidden', mode !== 'camera');
if(fileView) fileView.classList.toggle('hidden', mode !== 'file');
if (mode === 'camera') startCamera();
else stopCamera();
}
function resetScannerState() {
const resOverlay = document.getElementById('resultOverlay');
if(resOverlay) resOverlay.classList.remove('visible');
const fInput = document.getElementById('fileInput');
if(fInput) fInput.value = '';
stopCamera();
if(permissionMsg) permissionMsg.classList.add('hidden');
}
window.startCamera = async function() {
if (isScanning && videoElement.srcObject) return;
try {
videoStream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: "environment" } });
videoElement.srcObject = videoStream;
videoElement.setAttribute("playsinline", true);
videoElement.onloadedmetadata = () => {
videoElement.play();
isScanning = true;
requestAnimationFrame(scanLoop);
};
} catch (err) {
console.error("Erreur Caméra", err);
if(permissionMsg) permissionMsg.classList.remove('hidden');
const errText = document.getElementById('cam-error-text');
if(errText) errText.innerText = "Accès refusé. Vérifiez vos permissions.";
isScanning = false;
}
}
function stopCamera() {
if (videoStream) {
videoStream.getTracks().forEach(track => track.stop());
videoStream = null;
}
if(videoElement) videoElement.srcObject = null;
isScanning = false;
if (animationFrameId) cancelAnimationFrame(animationFrameId);
}
function scanLoop() {
if (!isScanning) return;
if (videoElement.readyState === videoElement.HAVE_ENOUGH_DATA) {
canvasElement.width = videoElement.videoWidth;
canvasElement.height = videoElement.videoHeight;
canvasCtx.drawImage(videoElement, 0, 0, canvasElement.width, canvasElement.height);
const imageData = canvasCtx.getImageData(0, 0, canvasElement.width, canvasElement.height);
const code = jsQR(imageData.data, imageData.width, imageData.height, { inversionAttempts: "dontInvert" });
if (code && code.data) {
onScanSuccess(code.data);
isScanning = false;
}
}
if (isScanning) animationFrameId = requestAnimationFrame(scanLoop);
}
/* LOGIQUE FICHIER */
const dropZone = document.getElementById('dropZone');
const fileInput = document.getElementById('fileInput');
if(dropZone) {
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropZone.addEventListener(eventName, e => { e.preventDefault(); e.stopPropagation(); }, false);
});
['dragenter', 'dragover'].forEach(eventName => {
dropZone.addEventListener(eventName, () => dropZone.classList.add('border-blue-500', 'bg-blue-50', 'dark:bg-blue-900/20'), false);
});
['dragleave', 'drop'].forEach(eventName => {
dropZone.addEventListener(eventName, () => dropZone.classList.remove('border-blue-500', 'bg-blue-50', 'dark:bg-blue-900/20'), false);
});
dropZone.addEventListener('drop', (e) => handleFile(e.dataTransfer.files[0]));
}
if(fileInput) {
fileInput.addEventListener('change', (e) => handleFile(e.target.files[0]));
}
document.addEventListener('paste', (e) => {
if (currentMode === 'file') {
const items = (e.clipboardData || e.originalEvent.clipboardData).items;
for (let item of items) if (item.kind === 'file') handleFile(item.getAsFile());
}
});
// CORRECTION 2 : Utiliser UNIQUEMENT fallbackScan (jsQR pur) pour les fichiers.
// Cela évite que Html5Qrcode injecte du DOM dans "reader" et bloque la caméra plus tard.
function handleFile(file) {
if(!file) return;
fallbackScan(file);
}
function fallbackScan(file) {
const reader = new FileReader();
reader.onload = (e) => {
const img = new Image();
img.onload = () => {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const code = jsQR(imageData.data, canvas.width, canvas.height);
if (code) onScanSuccess(code.data);
else showToast("Aucun QR détecté");
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
function onScanSuccess(decodedText) {
let cleanText = decodedText;
if(decodedText.trim().startsWith('{') && decodedText.trim().endsWith('}')) {
try {
const parsed = JSON.parse(decodedText);
if(parsed.text) cleanText = parsed.text;
else if(parsed.url) cleanText = parsed.url;
} catch(e) {}
}
// Stocker le résultat pour le bouton copier
window.lastScannedText = cleanText;
showResult(cleanText);
}
function showResult(text) {
const overlay = document.getElementById('resultOverlay');
const content = document.getElementById('scanContent');
const btns = document.getElementById('actionButtons');
content.textContent = text;
const isUrl = /^(http|https):\/\/[^ "]+$/.test(text);
// Note: Le onclick appelle maintenant copyGlobal() qui lit window.lastScannedText
let btnHtml = `
<button onclick="copyGlobal()" class="col-span-${isUrl ? '1' : '2'} bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-200 py-2 rounded-lg font-medium hover:bg-gray-50 dark:hover:bg-gray-600">
<i class="fa-regular fa-copy mr-2"></i>Copier
</button>
`;
if(isUrl) {
btnHtml += `
<button onclick="window.open('${text}', '_blank')" class="bg-blue-600 hover:bg-blue-700 text-white py-2 rounded-lg font-medium">
<i class="fa-solid fa-external-link-alt mr-2"></i>Ouvrir
</button>
`;
}
btns.innerHTML = btnHtml;
overlay.classList.add('visible');
}
window.closeResult = function() {
document.getElementById('resultOverlay').classList.remove('visible');
if (currentMode === 'camera') {
isScanning = true;
requestAnimationFrame(scanLoop);
}
}
// Nouvelle fonction de copie robuste
window.copyGlobal = function() {
if (window.lastScannedText) {
navigator.clipboard.writeText(window.lastScannedText).then(() => showToast("Copié !"));
} else {
showToast("Rien à copier");
}
}
function showToast(msg) {
const toast = document.getElementById('toast');
toast.textContent = msg;
toast.style.opacity = '1';
setTimeout(() => toast.style.opacity = '0', 3000);
}
});
</script>
</body>
</html>