-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathqrcode.html
More file actions
412 lines (374 loc) · 12.8 KB
/
qrcode.html
File metadata and controls
412 lines (374 loc) · 12.8 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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>二维码工具</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jsqr@1.4.0/dist/jsQR.js"></script>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
background: #f5f5f5;
height: 100vh;
display: flex;
flex-direction: column;
}
header {
background: #fff;
border-bottom: 1px solid #e5e7eb;
padding: 0 24px;
height: 52px;
display: flex;
align-items: center;
gap: 10px;
flex-shrink: 0;
}
.home-link {
display: flex; align-items: center; justify-content: center;
width: 28px; height: 28px; border-radius: 6px;
color: #9ca3af; text-decoration: none; flex: none;
transition: background 0.15s, color 0.15s;
}
.home-link:hover { background: #f3f4f6; color: #374151; }
.header-title { font-size: 14px; font-weight: 600; color: #111; letter-spacing: 0.02em; }
.header-title span { color: #9ca3af; font-weight: 400; margin: 0 6px; }
.main {
display: grid;
grid-template-columns: 1fr 1fr;
flex: 1;
overflow: hidden;
gap: 1px;
background: #e5e7eb;
}
.pane {
background: #fff;
display: flex;
flex-direction: column;
overflow: hidden;
}
.pane-header {
padding: 10px 20px;
font-size: 11px;
font-weight: 600;
color: #9ca3af;
letter-spacing: 0.12em;
text-transform: uppercase;
border-bottom: 1px solid #f0f0f0;
flex-shrink: 0;
}
/* ── 生成:输入区固定,QR 区占满剩余 ── */
#gen-input {
height: 110px;
flex-shrink: 0;
padding: 16px 20px;
font-size: 13px;
font-family: 'JetBrains Mono', 'Menlo', monospace;
line-height: 1.7;
color: #374151;
border: none;
outline: none;
resize: none;
background: #fafafa;
border-bottom: 1px solid #f0f0f0;
}
#gen-input::placeholder { color: #d1d5db; }
.gen-qr-area {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 20px;
padding: 24px 20px;
}
.gen-hint {
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
color: #d1d5db;
font-size: 13px;
}
.gen-hint svg { width: 48px; height: 48px; opacity: 0.3; }
#qr-wrap { display: none; flex-direction: column; align-items: center; gap: 20px; }
#qr-div { line-height: 0; }
#qr-div img, #qr-div canvas { border-radius: 10px; display: block; box-shadow: 0 2px 12px rgba(0,0,0,0.08); }
.dl-btn {
padding: 8px 24px;
background: #000;
color: #fff;
border: none;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
cursor: pointer;
transition: background 0.15s;
}
.dl-btn:hover { background: #222; }
/* ── 解析:空状态 drop zone 占满,有结果后缩成紧凑条 ── */
.decode-pane {
flex: 1;
display: flex;
flex-direction: column;
overflow-y: auto;
min-height: 0;
}
.drop-zone {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 10px;
margin: 20px;
border: 2px dashed #e5e7eb;
border-radius: 12px;
color: #9ca3af;
font-size: 13px;
cursor: default;
transition: border-color 0.15s, background 0.15s, flex 0.2s, margin 0.2s, min-height 0.2s;
min-height: 160px;
}
.drop-zone.drag-over { border-color: #6b7280; background: #f9fafb; }
/* 紧凑模式:有解析结果后 */
.drop-zone.compact {
flex: none;
flex-direction: row;
min-height: 0;
height: 48px;
margin: 12px 20px;
gap: 10px;
justify-content: center;
border-radius: 8px;
font-size: 12px;
}
.drop-zone.compact .drop-icon,
.drop-zone.compact .drop-hint { display: none; }
.drop-zone svg { width: 32px; height: 32px; opacity: 0.4; }
.drop-hint { font-size: 11px; color: #d1d5db; }
.upload-btn {
padding: 5px 14px;
background: none;
color: #6b7280;
border: 1px solid #e5e7eb;
border-radius: 16px;
font-size: 12px;
cursor: pointer;
transition: border-color 0.15s, color 0.15s;
}
.upload-btn:hover { color: #111; border-color: #9ca3af; }
.decode-preview {
margin: 0 20px;
border-radius: 10px;
overflow: hidden;
background: #f5f5f5;
max-height: 220px;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
}
.decode-preview img { max-width: 100%; max-height: 220px; object-fit: contain; display: block; }
.decode-result {
margin: 14px 20px 24px;
flex-shrink: 0;
}
.decode-result-label {
font-size: 11px; font-weight: 600; color: #9ca3af;
letter-spacing: 0.1em; text-transform: uppercase; margin-bottom: 6px;
}
.decode-result-box {
position: relative;
background: #fafafa;
border: 1px solid #e5e7eb;
border-radius: 8px;
padding: 12px 80px 12px 14px;
font-size: 13px;
font-family: 'JetBrains Mono', monospace;
color: #374151;
line-height: 1.6;
word-break: break-all;
min-height: 44px;
}
.copy-result-btn {
position: absolute; top: 8px; right: 8px;
padding: 4px 12px;
background: #000; color: #fff;
border: none; border-radius: 14px;
font-size: 11px; font-weight: 600;
cursor: pointer; transition: background 0.15s;
}
.copy-result-btn:hover { background: #222; }
.decode-error { margin-top: 8px; font-size: 12px; color: #dc2626; }
</style>
</head>
<body>
<header>
<a class="home-link" href="index.html" title="工具箱首页">
<svg width="15" height="15" viewBox="0 0 15 15" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round">
<path d="M1.5 6.5L7.5 1.5L13.5 6.5V13.5H9.5V9.5H5.5V13.5H1.5V6.5Z"/>
</svg>
</a>
<div class="header-title">二维码工具 <span>·</span> 生成 & 解析</div>
</header>
<div class="main">
<!-- 左:文本 → 二维码 -->
<div class="pane">
<div class="pane-header">文本 → 二维码</div>
<textarea id="gen-input" placeholder="输入文字、链接、任意内容…"></textarea>
<div class="gen-qr-area">
<div class="gen-hint" id="gen-hint">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.2">
<rect x="3" y="3" width="7" height="7" rx="1"/><rect x="14" y="3" width="7" height="7" rx="1"/>
<rect x="3" y="14" width="7" height="7" rx="1"/>
<path d="M14 14h2v2h-2zm4 0h2v2h-2zm-4 4h2v2h-2zm4 0h2v2h-2z"/>
</svg>
<span>输入内容后自动生成</span>
</div>
<div id="qr-wrap">
<div id="qr-div"></div>
<button class="dl-btn" onclick="downloadQR()">下载 PNG</button>
</div>
</div>
</div>
<!-- 右:二维码 → 文本 -->
<div class="pane">
<div class="pane-header">二维码 → 文本</div>
<div class="decode-pane">
<div class="drop-zone" id="drop-zone">
<svg class="drop-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
<path d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 16M14 8h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"/>
</svg>
<span>拖入图片 · Ctrl+V 粘贴</span>
<span class="drop-hint">PNG · JPG · GIF · WebP</span>
<button class="upload-btn" id="upload-btn">点击上传</button>
</div>
<input type="file" id="file-input" accept="image/*" style="display:none">
<div id="decode-result-wrap" style="display:none">
<div class="decode-preview"><img id="decode-img" src="" alt=""></div>
<div class="decode-result">
<div class="decode-result-label">解析结果</div>
<div class="decode-result-box">
<span id="result-text"></span>
<button class="copy-result-btn" id="copy-result-btn" onclick="copyResult()">复制</button>
</div>
<div class="decode-error" id="decode-error" style="display:none"></div>
</div>
</div>
</div>
</div>
</div>
<script>
// ── 生成 ──
const genInput = document.getElementById('gen-input');
const genHint = document.getElementById('gen-hint');
const qrWrap = document.getElementById('qr-wrap');
const qrDiv = document.getElementById('qr-div');
let qrObj = null;
genInput.addEventListener('input', () => {
const text = genInput.value.trim();
if (!text) {
genHint.style.display = '';
qrWrap.style.display = 'none';
return;
}
genHint.style.display = 'none';
qrWrap.style.display = 'flex';
if (qrObj) {
qrObj.clear();
qrObj.makeCode(text);
} else {
qrObj = new QRCode(qrDiv, {
text,
width: 240,
height: 240,
colorDark: '#000000',
colorLight: '#ffffff',
correctLevel: QRCode.CorrectLevel.M,
});
}
});
function downloadQR() {
const canvas = qrDiv.querySelector('canvas');
const img = qrDiv.querySelector('img');
const a = document.createElement('a');
a.download = 'qrcode.png';
a.href = canvas ? canvas.toDataURL('image/png') : img.src;
a.click();
}
// ── 解析 ──
const dropZone = document.getElementById('drop-zone');
const fileInput = document.getElementById('file-input');
const decodeResultWrap = document.getElementById('decode-result-wrap');
const decodeImg = document.getElementById('decode-img');
const resultText = document.getElementById('result-text');
const decodeError = document.getElementById('decode-error');
document.getElementById('upload-btn').addEventListener('click', e => {
e.stopPropagation();
fileInput.click();
});
fileInput.addEventListener('change', () => {
if (fileInput.files[0]) decodeFile(fileInput.files[0]);
});
function decodeFile(file) {
if (!file || !file.type.startsWith('image/')) return;
const reader = new FileReader();
reader.onload = e => decodeImageSrc(e.target.result);
reader.readAsDataURL(file);
}
function decodeImageSrc(src) {
decodeImg.src = src;
decodeResultWrap.style.display = '';
decodeError.style.display = 'none';
resultText.textContent = '解析中…';
// 切换 drop zone 为紧凑条
dropZone.classList.add('compact');
const img = new Image();
img.onload = () => {
const canvas = document.createElement('canvas');
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const code = jsQR(imageData.data, imageData.width, imageData.height);
if (code) {
resultText.textContent = code.data;
decodeError.style.display = 'none';
} else {
resultText.textContent = '';
decodeError.style.display = '';
decodeError.textContent = '未能识别二维码,请确认图片清晰且包含完整二维码';
}
};
img.src = src;
}
// 拖拽
document.addEventListener('dragover', e => e.preventDefault());
dropZone.addEventListener('dragenter', e => { e.preventDefault(); dropZone.classList.add('drag-over'); });
dropZone.addEventListener('dragleave', e => {
if (!dropZone.contains(e.relatedTarget)) dropZone.classList.remove('drag-over');
});
dropZone.addEventListener('drop', e => {
e.preventDefault();
dropZone.classList.remove('drag-over');
if (e.dataTransfer.files[0]) decodeFile(e.dataTransfer.files[0]);
});
// 粘贴
document.addEventListener('paste', e => {
const item = Array.from(e.clipboardData.items).find(i => i.type.startsWith('image/'));
if (item) decodeFile(item.getAsFile());
});
async function copyResult() {
const text = resultText.textContent;
if (!text || text === '解析中…') return;
await navigator.clipboard.writeText(text);
const btn = document.getElementById('copy-result-btn');
btn.textContent = '✓';
setTimeout(() => btn.textContent = '复制', 1500);
}
</script>
</body>
</html>