Skip to content

Commit ff38692

Browse files
authored
1 parent 0d07f53 commit ff38692

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

qr.html

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Enhanced QR Code Decoder</title>
7+
<script src="https://cdn.jsdelivr.net/npm/jsqr@1.4.0/dist/jsQR.min.js"></script>
8+
<style>
9+
body {
10+
font-family: Arial, sans-serif;
11+
max-width: 800px;
12+
margin: 0 auto;
13+
padding: 20px;
14+
}
15+
#dropArea {
16+
border: 2px dashed #ccc;
17+
border-radius: 20px;
18+
width: 100%;
19+
margin: 20px auto;
20+
padding: 20px;
21+
text-align: center;
22+
}
23+
#dropArea.highlight {
24+
border-color: purple;
25+
}
26+
#fileElem {
27+
display: none;
28+
}
29+
#result {
30+
margin-top: 20px;
31+
padding: 10px;
32+
border: 1px solid #ccc;
33+
border-radius: 5px;
34+
}
35+
</style>
36+
</head>
37+
<body>
38+
<h1>Enhanced QR Code Decoder</h1>
39+
<p>Upload, drag & drop, or paste a QR code image:</p>
40+
<div id="dropArea">
41+
<input type="file" id="fileElem" accept="image/*">
42+
<label for="fileElem">Select a file or drag & drop here</label>
43+
</div>
44+
<div id="result"></div>
45+
46+
<script>
47+
// Ensure jsQR is loaded before proceeding
48+
window.onload = function() {
49+
if (typeof jsQR === 'undefined') {
50+
document.getElementById('result').innerHTML = "Error: QR code library not loaded. Please check your internet connection and refresh the page.";
51+
return;
52+
}
53+
54+
const dropArea = document.getElementById('dropArea');
55+
const fileElem = document.getElementById('fileElem');
56+
const result = document.getElementById('result');
57+
58+
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
59+
dropArea.addEventListener(eventName, preventDefaults, false);
60+
});
61+
62+
function preventDefaults(e) {
63+
e.preventDefault();
64+
e.stopPropagation();
65+
}
66+
67+
['dragenter', 'dragover'].forEach(eventName => {
68+
dropArea.addEventListener(eventName, highlight, false);
69+
});
70+
71+
['dragleave', 'drop'].forEach(eventName => {
72+
dropArea.addEventListener(eventName, unhighlight, false);
73+
});
74+
75+
function highlight(e) {
76+
dropArea.classList.add('highlight');
77+
}
78+
79+
function unhighlight(e) {
80+
dropArea.classList.remove('highlight');
81+
}
82+
83+
dropArea.addEventListener('drop', handleDrop, false);
84+
fileElem.addEventListener('change', handleFiles, false);
85+
document.addEventListener('paste', handlePaste, false);
86+
87+
function handleDrop(e) {
88+
const dt = e.dataTransfer;
89+
const files = dt.files;
90+
handleFiles(files);
91+
}
92+
93+
function handleFiles(files) {
94+
const file = files instanceof FileList ? files[0] : this.files[0];
95+
if (file) {
96+
decodeQR(file);
97+
}
98+
}
99+
100+
function handlePaste(e) {
101+
const items = e.clipboardData.items;
102+
for (let i = 0; i < items.length; i++) {
103+
if (items[i].type.indexOf('image') !== -1) {
104+
const blob = items[i].getAsFile();
105+
decodeQR(blob);
106+
break;
107+
}
108+
}
109+
}
110+
111+
function decodeQR(file) {
112+
const reader = new FileReader();
113+
reader.onload = function(e) {
114+
const img = new Image();
115+
img.onload = function() {
116+
const canvas = document.createElement('canvas');
117+
const context = canvas.getContext('2d');
118+
canvas.width = img.width;
119+
canvas.height = img.height;
120+
context.drawImage(img, 0, 0, img.width, img.height);
121+
const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
122+
const code = jsQR(imageData.data, imageData.width, imageData.height);
123+
124+
if (code) {
125+
let content = code.data;
126+
if (isValidUrl(content)) {
127+
content = `<a href="${content}" target="_blank">${content}</a>`;
128+
}
129+
result.innerHTML = `Decoded content: ${content}`;
130+
} else {
131+
result.innerHTML = "No QR code found in the image.";
132+
}
133+
};
134+
img.src = e.target.result;
135+
};
136+
reader.readAsDataURL(file);
137+
}
138+
139+
function isValidUrl(string) {
140+
try {
141+
new URL(string);
142+
return true;
143+
} catch (_) {
144+
return false;
145+
}
146+
}
147+
};
148+
</script>
149+
</body>
150+
</html>

0 commit comments

Comments
 (0)