This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
211 lines (186 loc) · 8.21 KB
/
index.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
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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>抗OCR文本图像生成器</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 20px;
box-sizing: border-box;
}
#container {
max-width: 600px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
color: #333;
}
#controls {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 5px;
color: #666;
}
input {
width: 100%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
margin-top: 20px;
background-color: #5d8d5f;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #59815b;
}
canvas {
display: block;
margin: 0 auto;
border: 1px solid black;
margin-top: 20px;
border-radius: 4px;
}
</style>
</head>
<body>
<div id="controls">
<label for="inputText">反OCR源文本:</label>
<input type="text" id="inputText" placeholder="输入文本" value="鉴于对人类家庭所有成员的固有尊严及其平等的和不移的权利的承认,乃是世界自由、正义与和平的基础。任何人不得使为奴隶或奴役,人人生而自由,人人有权享有生命、自由和人身安全。">
<label for="maxChars">最大行字数:</label>
<input type="number" id="maxChars" value="26">
<label for="margin">边距:</label>
<input type="number" id="margin" value="10">
<label for="fontSize">字体大小:</label>
<input type="number" id="fontSize" value="35">
<label for="distortionLevel">失真级别:</label>
<input type="number" id="distortionLevel" value="2">
<label for="waveFrequency">波频:</label>
<input type="number" id="waveFrequency" value="4">
<label for="waveAmplitude">波幅:</label>
<input type="number" id="waveAmplitude" value="3">
<label for="pointCount">随机点数量:</label>
<input type="number" id="pointCount" value="100">
<label for="lineCount">随机线数量:</label>
<input type="number" id="lineCount" value="20">
<button id="generateButton">生成图像</button>
</div>
<canvas id="textCanvas"></canvas>
<script>
document.addEventListener('DOMContentLoaded', function() {
const generateButton = document.getElementById('generateButton');
generateButton.addEventListener('click', updateCanvas);
updateCanvas(); // 初次加载页面时生成图像
});
const canvas = document.getElementById('textCanvas');
const ctx = canvas.getContext('2d');
function updateCanvas() {
const text = document.getElementById('inputText').value;
const maxChars = parseInt(document.getElementById('maxChars').value);
const margin = parseInt(document.getElementById('margin').value);
const fontSize = parseInt(document.getElementById('fontSize').value);
const distortionLevel = parseInt(document.getElementById('distortionLevel').value);
const waveFrequency = parseInt(document.getElementById('waveFrequency').value);
const waveAmplitude = parseInt(document.getElementById('waveAmplitude').value);
const pointCount = parseInt(document.getElementById('pointCount').value);
const lineCount = parseInt(document.getElementById('lineCount').value);
drawDistortedText(text, maxChars, margin, fontSize, distortionLevel, waveFrequency, waveAmplitude, pointCount, lineCount);
}
function drawDistortedText(text, maxChars, margin, fontSize, distortionLevel, waveFrequency, waveAmplitude, pointCount, lineCount) {
const lines = wrapText(ctx, text, maxChars, fontSize);
const lineHeight = fontSize + 10;
canvas.width = maxChars * (fontSize / 2) + 2 * margin;
canvas.height = lines.length * lineHeight + 2 * margin;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.font = `${fontSize}px Arial`;
ctx.fillStyle = 'black';
let y = margin + fontSize;
for (const line of lines) {
ctx.fillText(line, margin, y);
y += lineHeight;
}
distortCanvas(ctx, canvas.width, canvas.height, distortionLevel, waveFrequency, waveAmplitude);
addRandomPoints(ctx, canvas.width, canvas.height, pointCount);
addRandomLines(ctx, canvas.width, canvas.height, lineCount);
}
function wrapText(ctx, text, maxChars, fontSize) {
const words = text.split('');
const lines = [];
let line = '';
for (const word of words) {
const testLine = line + word;
const metrics = ctx.measureText(testLine);
const testWidth = metrics.width;
if (testWidth > maxChars * (fontSize / 2) && line.length > 0) {
lines.push(line);
line = word;
} else {
line = testLine;
}
}
lines.push(line);
return lines;
}
function distortCanvas(ctx, width, height, distortionLevel, waveFrequency, waveAmplitude) {
const imageData = ctx.getImageData(0, 0, width, height);
const data = imageData.data;
const distortedData = new Uint8ClampedArray(data);
for (let y = 0; y < height; y++) {
const yDistortion = Math.sin(y / waveFrequency) * waveAmplitude;
for (let x = 0; x < width; x++) {
const xDistortion = Math.sin(x / waveFrequency) * waveAmplitude;
const dx = Math.floor(xDistortion + distortionLevel * (Math.random() - 0.5));
const dy = Math.floor(yDistortion + distortionLevel * (Math.random() - 0.5));
const sx = x + dx;
const sy = y + dy;
const si = (sx + sy * width) * 4;
const di = (x + y * width) * 4;
if (sx >= 0 && sx < width && sy >= 0 && sy < height) {
distortedData[di] = data[si];
distortedData[di + 1] = data[si + 1];
distortedData[di + 2] = data[si + 2];
distortedData[di + 3] = data[si + 3];
}
}
}
ctx.putImageData(new ImageData(distortedData, width, height), 0, 0);
}
function addRandomPoints(ctx, width, height, count) {
for (let i = 0; i < count; i++) {
const x = Math.random() * width;
const y = Math.random() * height;
ctx.fillRect(x, y, 2, 2); // 绘制一个2x2的点
}
}
function addRandomLines(ctx, width, height, count) {
for (let i = 0; i < count; i++) {
const x1 = Math.random() * width;
const y1 = Math.random() * height;
const x2 = Math.random() * width;
const y2 = Math.random() * height;
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
}
</script>
</body>
</html>