-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQrCodeGenerator.html
More file actions
465 lines (404 loc) · 14.1 KB
/
Copy pathQrCodeGenerator.html
File metadata and controls
465 lines (404 loc) · 14.1 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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>二维码生成器 - JustHTML 只需网页</title>
<script>
/**
* 动态加载(强制去除缓存) CSS 的函数
* @param {string} src - 要加载的 CSS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadStyle(src) {
return new Promise((resolve, reject) => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
link.async = true;
link.onload = resolve;
link.onerror = reject;
document.head.appendChild(link);
});
}
/**
* 动态加载(强制去除缓存) JS 的函数
* @param {string} src - 要加载的 JS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
script.async = true;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
</script>
<script>
loadStyle('../assets/style.css')
let scriptArray = [
"../assets/AddHeader_Tools.js",
"../assets/AddFooter.js",
"../assets/AddIco.js",
"../assets/AutoRun.js",
]
scriptArray.forEach(script => {
loadScript(script);
});
</script>
<script src="../package/Vue/code/vue.global.prod.js"></script>
<script type="module" src="../package/Sober/code/main.js"></script>
<style>
.qr-controls {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 15px;
margin: 15px 0;
}
.control-group {
display: flex;
flex-direction: column;
}
.control-group label {
margin-bottom: 5px;
color: var(--accent-color);
}
.control-group input, .control-group select {
padding: 8px;
}
.qr-container {
display: flex;
flex-direction: column;
align-items: center;
margin: 20px 0;
}
.qr-placeholder {
width: 200px;
height: 200px;
display: flex;
align-items: center;
justify-content: center;
background-color: var(--bg-tertiary);
border: 2px dashed var(--border-color);
margin: 20px auto;
}
.qr-placeholder p {
color: var(--text-secondary);
text-align: center;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin: 15px 0;
justify-content: center;
}
.info-box {
background-color: var(--bg-tertiary);
padding: 10px;
border-radius: var(--border-radius);
margin: 10px 0;
}
.download-options {
display: flex;
gap: 10px;
margin-top: 15px;
flex-wrap: wrap;
justify-content: center;
}
.size-preview {
margin-top: 15px;
text-align: center;
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<a href="../index.html" class="back-link">← 返回首页</a>
<div class="tool-section">
<h2>二维码生成器</h2>
<p>生成自定义二维码</p>
<div class="info-box">
<p><strong>功能说明:</strong></p>
<p>• 支持生成各种尺寸的二维码</p>
<p>• 可自定义颜色和边距</p>
<p>• 支持下载PNG/JPG/SVG格式</p>
</div>
<div>
<textarea
v-model="qrContent"
placeholder="输入要编码的内容,如网址、文本等..."
rows="4"
style="width: 100%;"
></textarea>
<div class="qr-controls">
<div class="control-group">
<label for="qrSize">二维码尺寸</label>
<input
type="range"
id="qrSize"
v-model.number="qrSize"
min="100"
max="500"
@change="generateQR"
>
<span>{{ qrSize }}px</span>
</div>
<div class="control-group">
<label for="foregroundColor">前景色</label>
<input
type="color"
id="foregroundColor"
v-model="foregroundColor"
@change="generateQR"
>
</div>
<div class="control-group">
<label for="backgroundColor">背景色</label>
<input
type="color"
id="backgroundColor"
v-model="backgroundColor"
@change="generateQR"
>
</div>
<div class="control-group">
<label for="marginSize">边距大小</label>
<select id="marginSize" v-model="marginSize" @change="generateQR">
<option value="0">无边距</option>
<option value="1">小边距</option>
<option value="2">中边距</option>
<option value="4">大边距</option>
</select>
</div>
</div>
<div class="qr-container">
<div class="qr-placeholder" v-if="!qrCode">
<p>输入内容并点击<br>"生成二维码"</p>
</div>
<canvas
ref="qrCanvasRef"
id="qrCanvas"
:width="qrSize"
:height="qrSize"
v-show="qrCode"
></canvas>
<div class="size-preview" v-if="qrCode">
<p>尺寸: {{ qrSize }}×{{ qrSize }} 像素</p>
</div>
</div>
<div class="controls">
<button @click="generateQR">生成二维码</button>
<button @click="clearQR">清空</button>
<button @click="fillSample">填放示例</button>
</div>
<div class="download-options" v-if="qrCode">
<button @click="downloadQR('png')">下载PNG</button>
<button @click="downloadQR('jpg')">下载JPG</button>
<button @click="downloadQR('svg')">下载SVG</button>
<button @click="copyQR">复制到剪贴板</button>
</div>
</div>
</div>
</div>
</div>
<script>
const { createApp, ref, onMounted } = Vue;
createApp({
setup() {
const qrContent = ref('');
const qrSize = ref(200);
const foregroundColor = ref('#000000');
const backgroundColor = ref('#ffffff');
const marginSize = ref(1);
const qrCode = ref('');
const qrCanvasRef = ref(null);
// 简单的二维码生成算法(模拟)
const generateQR = () => {
if (!qrContent.value) {
alert('请输入要编码的内容');
return;
}
// 创建canvas元素
const canvas = document.createElement('canvas');
canvas.width = qrSize.value;
canvas.height = qrSize.value;
const ctx = canvas.getContext('2d');
// 填充背景
ctx.fillStyle = backgroundColor.value;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 简化的二维码绘制逻辑(实际的二维码生成需要复杂的算法)
// 这里只是模拟显示
ctx.fillStyle = foregroundColor.value;
// 绘制二维码边框定位标志
drawPositionMarker(ctx, 0, 0, qrSize.value);
drawPositionMarker(ctx, qrSize.value - 60, 0, qrSize.value);
drawPositionMarker(ctx, 0, qrSize.value - 60, qrSize.value);
// 绘制模拟的数据块
drawSimulatedData(ctx, qrSize.value);
// 将canvas内容转为base64
qrCode.value = canvas.toDataURL('image/png');
// 将内容绘制到页面上的canvas
const pageCanvas = document.getElementById('qrCanvas');
if (pageCanvas) {
const pageCtx = pageCanvas.getContext('2d');
pageCtx.clearRect(0, 0, pageCanvas.width, pageCanvas.height);
// 填充背景
pageCtx.fillStyle = backgroundColor.value;
pageCtx.fillRect(0, 0, pageCanvas.width, pageCanvas.height);
// 绘制二维码
pageCtx.fillStyle = foregroundColor.value;
drawPositionMarker(pageCtx, 0, 0, qrSize.value);
drawPositionMarker(pageCtx, qrSize.value - 60, 0, qrSize.value);
drawPositionMarker(pageCtx, 0, qrSize.value - 60, qrSize.value);
drawSimulatedData(pageCtx, qrSize.value);
}
};
// 绘制定位标记
const drawPositionMarker = (ctx, x, y, size) => {
const blockSize = size / 20;
const markerSize = 7 * blockSize;
// 外层正方形
ctx.fillStyle = foregroundColor.value;
ctx.fillRect(x, y, markerSize, markerSize);
// 内层正方形
ctx.fillStyle = backgroundColor.value;
ctx.fillRect(x + blockSize, y + blockSize, 5 * blockSize, 5 * blockSize);
// 中心方块
ctx.fillStyle = foregroundColor.value;
ctx.fillRect(x + blockSize * 2, y + blockSize * 2, 3 * blockSize, 3 * blockSize);
};
// 绘制模拟数据
const drawSimulatedData = (ctx, size) => {
const blockSize = size / 20;
ctx.fillStyle = foregroundColor.value;
// 绘制一些随机方块模拟二维码数据
for (let i = 0; i < 20; i++) {
for (let j = 0; j < 20; j++) {
// 避开定位标记区域
if (!((i < 7 && j < 7) || (i > 12 && j < 7) || (i < 7 && j > 12))) {
// 随机决定是否绘制方块(模拟二维码数据)
if (Math.random() > 0.5) {
ctx.fillRect(j * blockSize, i * blockSize, blockSize, blockSize);
}
}
}
}
};
// 下载二维码
const downloadQR = (format) => {
if (!qrCode.value) {
alert('请先生成二维码');
return;
}
// 实际实现中,我们会生成真实的二维码
// 这里只是模拟
const canvas = document.createElement('canvas');
canvas.width = qrSize.value;
canvas.height = qrSize.value;
const ctx = canvas.getContext('2d');
// 填充背景
ctx.fillStyle = backgroundColor.value;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 绘制二维码
ctx.fillStyle = foregroundColor.value;
drawPositionMarker(ctx, 0, 0, qrSize.value);
drawPositionMarker(ctx, qrSize.value - 60, 0, qrSize.value);
drawPositionMarker(ctx, 0, qrSize.value - 60, qrSize.value);
drawSimulatedData(ctx, qrSize.value);
let mimeType;
let fileExtension;
switch(format) {
case 'png':
mimeType = 'image/png';
fileExtension = 'png';
break;
case 'jpg':
mimeType = 'image/jpeg';
fileExtension = 'jpg';
break;
case 'svg':
// SVG需要特殊的生成方法,这里只是模拟
alert('SVG格式的二维码生成需要更复杂的实现,这里仅为演示');
return;
default:
mimeType = 'image/png';
fileExtension = 'png';
}
const url = canvas.toDataURL(mimeType);
const a = document.createElement('a');
a.href = url;
a.download = `qrcode.${fileExtension}`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
// 复制到剪贴板
const copyQR = () => {
if (!qrCode.value) {
alert('请先生成二维码');
return;
}
// 创建一个临时的image元素
const img = document.createElement('img');
img.src = qrCode.value;
// 将图片添加到DOM中(隐藏)
img.style.position = 'absolute';
img.style.left = '-9999px';
document.body.appendChild(img);
// 选择图片
const range = document.createRange();
range.selectNode(img);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
// 复制到剪贴板
try {
document.execCommand('copy');
alert('二维码已复制到剪贴板!');
} catch (err) {
console.error('复制失败: ', err);
alert('复制失败,请手动保存图片');
}
// 清理
window.getSelection().removeAllRanges();
document.body.removeChild(img);
};
// 清空二维码
const clearQR = () => {
qrContent.value = '';
qrCode.value = '';
// 清空canvas
const canvas = document.getElementById('qrCanvas');
if (canvas) {
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
};
// 填放示例内容
const fillSample = () => {
qrContent.value = 'https://www.example.com';
generateQR();
};
return {
qrContent,
qrSize,
foregroundColor,
backgroundColor,
marginSize,
qrCode,
qrCanvasRef,
generateQR,
downloadQR,
copyQR,
clearQR,
fillSample
};
}
}).mount('#app');
</script>
</body>
</html>