-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathqr.html
154 lines (139 loc) · 5.27 KB
/
qr.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>QR Code Decoder</title>
<script src="https://cdn.jsdelivr.net/npm/jsqr@1.4.0/dist/jsQR.min.js"></script>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
#dropArea {
border: 2px dashed #ccc;
border-radius: 20px;
width: 100%;
margin: 20px auto;
padding: 20px;
text-align: center;
}
#dropArea.highlight {
border-color: purple;
}
#fileElem {
display: none;
}
#result {
margin-top: 20px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
</style>
</head>
<body>
<h1>QR Code Decoder</h1>
<p>Uses <a href="https://github.com/cozmo/jsQR/">jsQR</a> by Cosmo Wolfe</p>
<p>Upload, drag & drop, or paste a QR code image:</p>
<div id="dropArea">
<input type="file" id="fileElem" accept="image/*">
<label for="fileElem">Select a file or drag & drop here</label>
</div>
<div id="result"></div>
<script>
// Ensure jsQR is loaded before proceeding
window.onload = function() {
if (typeof jsQR === 'undefined') {
document.getElementById('result').innerHTML = "Error: QR code library not loaded. Please check your internet connection and refresh the page.";
return;
}
const dropArea = document.getElementById('dropArea');
const fileElem = document.getElementById('fileElem');
const result = document.getElementById('result');
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, preventDefaults, false);
});
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
['dragenter', 'dragover'].forEach(eventName => {
dropArea.addEventListener(eventName, highlight, false);
});
['dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, unhighlight, false);
});
function highlight(e) {
dropArea.classList.add('highlight');
}
function unhighlight(e) {
dropArea.classList.remove('highlight');
}
dropArea.addEventListener('drop', handleDrop, false);
fileElem.addEventListener('change', handleFiles, false);
document.addEventListener('paste', handlePaste, false);
function handleDrop(e) {
const dt = e.dataTransfer;
const files = dt.files;
handleFiles(files);
}
function handleFiles(files) {
const file = files instanceof FileList ? files[0] : this.files[0];
if (file) {
decodeQR(file);
}
}
function handlePaste(e) {
const items = e.clipboardData.items;
for (let i = 0; i < items.length; i++) {
if (items[i].type.indexOf('image') !== -1) {
const blob = items[i].getAsFile();
decodeQR(blob);
break;
}
}
}
function decodeQR(file) {
const reader = new FileReader();
reader.onload = function(e) {
const img = new Image();
img.onload = function() {
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
context.drawImage(img, 0, 0, img.width, img.height);
const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
const code = jsQR(imageData.data, imageData.width, imageData.height);
if (code) {
let content = code.data;
if (isValidUrl(content)) {
content = `<a href="${content}" target="_blank">${content}</a>`;
}
result.innerHTML = `Decoded content: ${content}`;
} else {
result.innerHTML = "No QR code found in the image.";
}
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
function isValidUrl(string) {
try {
new URL(string);
return true;
} catch (_) {
return false;
}
}
};
</script>
</body>
</html>