-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
382 lines (322 loc) · 17.6 KB
/
index.html
File metadata and controls
382 lines (322 loc) · 17.6 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
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scanner Documenti Web</title>
<script src="https://cdn.tailwindcss.com"></script>
<!-- Carichiamo OpenCV.js per i calcoli della matrice di trasformazione -->
<script async src="https://docs.opencv.org/4.8.0/opencv.js" onload="onOpenCvReady();" type="text/javascript"></script>
<!-- Carichiamo jsPDF per la generazione dei PDF -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
<style>
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
.canvas-container {
position: relative;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
border: 2px dashed #cbd5e1;
background-color: #f1f5f9;
overflow: hidden;
display: flex;
justify-content: center;
align-items: center;
}
canvas { max-width: 100%; height: auto; }
.loading-overlay {
position: fixed; top: 0; left: 0; width: 100%; height: 100%;
background: rgba(255,255,255,0.9); z-index: 50;
display: flex; justify-content: center; align-items: center; flex-direction: column;
}
</style>
</head>
<body class="bg-slate-100 min-h-screen flex flex-col items-center py-8">
<!-- Overlay di caricamento OpenCV -->
<div id="loading" class="loading-overlay">
<div class="animate-spin rounded-full h-16 w-16 border-t-4 border-b-4 border-blue-500 mb-4"></div>
<h2 class="text-xl font-semibold text-slate-700">Caricamento Librerie Grafiche...</h2>
<p class="text-slate-500">Attendere prego (richiede connessione internet)</p>
</div>
<div class="w-full max-w-5xl px-4">
<!-- Header -->
<div class="bg-white rounded-xl shadow-md p-4 sm:p-6 mb-6">
<h1 class="text-xl sm:text-2xl font-bold text-slate-800 mb-2">Allinea i documenti. Su richiesta di Elix17.</h1>
<p class="text-slate-600">Carica un'immagine, clicca i 4 angoli del foglio e ottieni la scansione allineata.</p>
<!-- Controlli -->
<div class="mt-6 flex flex-wrap gap-4 items-center">
<label class="w-full sm:w-auto flex justify-center bg-blue-600 hover:bg-blue-700 text-white font-semibold py-2 px-6 rounded-lg cursor-pointer transition shadow">
<span>📂 Carica Immagine</span>
<input type="file" id="fileInput" accept="image/*" class="hidden" onchange="loadImage(event)">
</label>
<button onclick="resetPoints()" class="w-full sm:w-auto bg-orange-500 hover:bg-orange-600 text-white font-semibold py-2 px-6 rounded-lg transition shadow">
🔄 Resetta Punti
</button>
<button onclick="processImage()" id="btnProcess" disabled class="w-full sm:w-auto bg-green-600 hover:bg-green-700 disabled:bg-slate-300 disabled:cursor-not-allowed text-white font-semibold py-2 px-6 rounded-lg transition shadow">
✨ Raddrizza Immagine
</button>
<span id="statusText" class="ml-auto text-sm font-medium text-slate-500">
Stato: In attesa immagine...
</span>
</div>
</div>
<!-- Area di Lavoro -->
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
<!-- Colonna Sinistra: Input -->
<div class="bg-white p-4 rounded-xl shadow-md">
<h3 class="text-lg font-semibold text-slate-700 mb-3 border-b pb-2">1. Seleziona i 4 angoli</h3>
<div class="canvas-container rounded-lg min-h-[350px] lg:min-h-[400px]" id="inputContainer">
<canvas id="canvasInput"></canvas>
<div id="placeholder" class="absolute text-slate-400 text-center">
<svg class="w-16 h-16 mx-auto mb-2 opacity-50" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"></path></svg>
Nessuna immagine caricata
</div>
</div>
<p class="text-xs text-slate-500 mt-2">Clicca nell'ordine: Alto-SX, Alto-DX, Basso-DX, Basso-SX</p>
</div>
<!-- Colonna Destra: Output -->
<div class="bg-white p-4 rounded-xl shadow-md">
<h3 class="text-lg font-semibold text-slate-700 mb-3 border-b pb-2">2. Risultato</h3>
<div class="canvas-container rounded-lg min-h-[350px] lg:min-h-[400px] bg-slate-50">
<canvas id="canvasOutput"></canvas>
<div id="outputPlaceholder" class="absolute text-slate-400 text-center">
Risultato qui
</div>
</div>
<!-- Area Download Modificata -->
<div class="mt-4 flex flex-col sm:flex-row justify-end gap-2 items-center">
<select id="downloadFormat" class="w-full sm:w-auto bg-slate-100 border border-slate-300 text-slate-700 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block p-2.5 outline-none font-medium">
<option value="png">Formato: PNG (Immagine)</option>
<option value="pdf">Formato: PDF (Documento)</option>
</select>
<button id="btnDownload" onclick="downloadResult()" disabled class="w-full sm:w-auto bg-indigo-600 hover:bg-indigo-700 disabled:bg-slate-300 disabled:cursor-not-allowed text-white font-semibold py-2 px-6 rounded-lg transition shadow">
💾 Scarica
</button>
</div>
</div>
</div>
<!-- NUOVO FOOTER CON CONTATORE -->
<footer class="mt-12 mb-6 border-t border-slate-300 pt-6 text-center">
<p class="text-slate-500 text-sm mb-2">Progetto Open Source basato su OpenCV.js</p>
<div class="flex justify-center items-center gap-2">
<span class="text-xs text-slate-400 font-semibold uppercase tracking-wider">Visite Totali:</span>
<!-- INIZIO AREA CONTATORE -->
<!-- Sostituisci questo blocco <img> con il codice che ottieni da hitwebcounter.com -->
<a href="https://www.hitwebcounter.com/" target="_blank">
<img src="https://hitwebcounter.com/counter/counter.php?page=21462281&style=0003&nbdigits=1&type=page&initCount=0" title="Free Tools" Alt="Free Tools" border="0" /></a>
<!-- FINE AREA CONTATORE -->
</div>
<!-- Script Logica -->
<script>
let imgElement = null;
let points = [];
let srcMat = null;
let scaleFactor = 1;
// Elementi DOM
const canvasInput = document.getElementById('canvasInput');
const ctxInput = canvasInput.getContext('2d');
const canvasOutput = document.getElementById('canvasOutput');
const statusText = document.getElementById('statusText');
function onOpenCvReady() {
document.getElementById('loading').style.display = 'none';
statusText.innerText = "Stato: Pronto. Carica un'immagine.";
console.log("OpenCV.js is ready");
}
function loadImage(event) {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (e) => {
imgElement = new Image();
imgElement.onload = () => {
// Reset
points = [];
document.getElementById('placeholder').style.display = 'none';
if(srcMat) srcMat.delete();
// Setup OpenCV mat
srcMat = cv.imread(imgElement);
// Adatta canvas all'immagine ma mantieni dimensioni visibili ragionevoli
resizeCanvas();
draw();
statusText.innerText = "Stato: Seleziona il 1° angolo (Alto-Sinistra)";
document.getElementById('btnProcess').disabled = true;
};
imgElement.src = e.target.result;
};
reader.readAsDataURL(file);
}
function resizeCanvas() {
// Logica responsive: riduciamo l'immagine per farla stare nel canvas se troppo grande
const container = document.getElementById('inputContainer');
const maxWidth = container.clientWidth;
const maxHeight = 600; // Altezza massima arbitraria
const imgW = imgElement.width;
const imgH = imgElement.height;
// Calcola fattore di scala
const scaleW = maxWidth / imgW;
const scaleH = maxHeight / imgH;
scaleFactor = Math.min(scaleW, scaleH);
// Se l'immagine è più piccola del contenitore, non ingrandirla (max scale 1)
if (scaleFactor > 1) scaleFactor = 1;
canvasInput.width = imgW * scaleFactor;
canvasInput.height = imgH * scaleFactor;
}
// Gestione Click
function handleCanvasInteraction(e) {
if (!imgElement || points.length >= 4) return;
// Previene il comportamento di default (es. scroll su mobile)
if (e.type === 'touchstart') {
e.preventDefault();
}
const rect = canvasInput.getBoundingClientRect();
const touch = e.touches ? e.touches[0] : null;
const clientX = touch ? touch.clientX : e.clientX;
const clientY = touch ? touch.clientY : e.clientY;
const x = (clientX - rect.left);
const y = (clientY - rect.top);
// Ignora i click fuori dal canvas
if (x < 0 || y < 0 || x > rect.width || y > rect.height) return;
// Salviamo sia le coordinate del canvas (per disegnare) sia quelle reali (per i calcoli)
const realX = x / scaleFactor;
const realY = y / scaleFactor;
points.push({x: x, y: y, realX: realX, realY: realY});
draw();
updateStatus();
}
canvasInput.addEventListener('mousedown', handleCanvasInteraction);
canvasInput.addEventListener('touchstart', handleCanvasInteraction);
function updateStatus() {
const labels = ["Alto-Destra", "Basso-Destra", "Basso-Sinistra", "Finito!"];
if (points.length < 4) {
statusText.innerText = `Stato: Seleziona ${labels[points.length-1] || "Alto-Sinistra"}`;
} else {
statusText.innerText = "Stato: Pronto per elaborare.";
document.getElementById('btnProcess').disabled = false;
}
}
function draw() {
// Pulisci e disegna immagine
ctxInput.clearRect(0, 0, canvasInput.width, canvasInput.height);
ctxInput.drawImage(imgElement, 0, 0, canvasInput.width, canvasInput.height);
// Disegna punti e linee
ctxInput.strokeStyle = 'red';
ctxInput.lineWidth = 2;
ctxInput.fillStyle = 'yellow';
for (let i = 0; i < points.length; i++) {
// Cerchio
ctxInput.beginPath();
ctxInput.arc(points[i].x, points[i].y, 5, 0, 2 * Math.PI);
ctxInput.fill();
ctxInput.stroke();
// Numero
ctxInput.font = "16px Arial";
ctxInput.fillStyle = "white";
ctxInput.fillText(i+1, points[i].x + 8, points[i].y - 8);
ctxInput.fillStyle = "yellow"; // reset
// Linea
if (i > 0) {
ctxInput.beginPath();
ctxInput.moveTo(points[i-1].x, points[i-1].y);
ctxInput.lineTo(points[i].x, points[i].y);
ctxInput.stroke();
}
}
// Chiudi il poligono se sono 4 punti
if (points.length === 4) {
ctxInput.beginPath();
ctxInput.moveTo(points[3].x, points[3].y);
ctxInput.lineTo(points[0].x, points[0].y);
ctxInput.stroke();
}
}
function resetPoints() {
points = [];
document.getElementById('btnProcess').disabled = true;
document.getElementById('btnDownload').disabled = true;
statusText.innerText = "Stato: Punti resettati.";
if(imgElement) draw();
// Pulisci output
const ctxOut = canvasOutput.getContext('2d');
ctxOut.clearRect(0, 0, canvasOutput.width, canvasOutput.height);
document.getElementById('outputPlaceholder').style.display = 'block';
}
function processImage() {
if (points.length !== 4) return;
try {
// Ordina i punti (Logica simile a Python)
let srcTri = cv.matFromArray(4, 1, cv.CV_32FC2, [
points[0].realX, points[0].realY,
points[1].realX, points[1].realY,
points[2].realX, points[2].realY,
points[3].realX, points[3].realY
]);
// Calcola larghezza e altezza del nuovo documento
const widthA = Math.hypot(points[2].realX - points[3].realX, points[2].realY - points[3].realY);
const widthB = Math.hypot(points[1].realX - points[0].realX, points[1].realY - points[0].realY);
const maxWidth = Math.max(widthA, widthB);
const heightA = Math.hypot(points[1].realX - points[2].realX, points[1].realY - points[2].realY);
const heightB = Math.hypot(points[0].realX - points[3].realX, points[0].realY - points[3].realY);
const maxHeight = Math.max(heightA, heightB);
// Destinazione
let dstTri = cv.matFromArray(4, 1, cv.CV_32FC2, [
0, 0,
maxWidth, 0,
maxWidth, maxHeight,
0, maxHeight
]);
// Calcola matrice e applica warp
let M = cv.getPerspectiveTransform(srcTri, dstTri);
let dst = new cv.Mat();
let dsize = new cv.Size(maxWidth, maxHeight);
cv.warpPerspective(srcMat, dst, M, dsize, cv.INTER_LINEAR, cv.BORDER_CONSTANT, new cv.Scalar());
// Mostra risultato
document.getElementById('outputPlaceholder').style.display = 'none';
cv.imshow('canvasOutput', dst);
document.getElementById('btnDownload').disabled = false;
// Cleanup memoria OpenCV
srcTri.delete(); dstTri.delete(); M.delete(); dst.delete();
statusText.innerText = "Stato: Completato! Scegli formato e scarica.";
} catch (err) {
console.error(err);
alert("Errore durante l'elaborazione. Assicurati di aver cliccato i punti nell'ordine corretto.");
}
}
async function downloadResult() {
const format = document.getElementById('downloadFormat').value;
if (format === 'png') {
const link = document.createElement('a');
link.download = 'scansione_raddrizzata.png';
link.href = canvasOutput.toDataURL("image/png");
link.click();
} else if (format === 'pdf') {
// Logica PDF
try {
const { jsPDF } = window.jspdf;
// Ottieni dimensioni immagine
const imgWidth = canvasOutput.width;
const imgHeight = canvasOutput.height;
// Determina orientamento
const orientation = imgWidth > imgHeight ? 'l' : 'p';
// Crea PDF con le dimensioni esatte dell'immagine
// Questo evita stiramenti o bordi bianchi indesiderati
const doc = new jsPDF({
orientation: orientation,
unit: 'px',
format: [imgWidth, imgHeight]
});
// Ottieni dati immagine (JPEG è più leggero per PDF)
const imgData = canvasOutput.toDataURL("image/jpeg", 0.95);
// Aggiungi immagine al PDF (x, y, width, height)
doc.addImage(imgData, 'JPEG', 0, 0, imgWidth, imgHeight);
// Salva
doc.save('scansione_raddrizzata.pdf');
} catch (e) {
console.error(e);
alert("Errore nella generazione del PDF: " + e.message);
}
}
}
</script>
<footer class="text-center text-sm text-slate-500 py-4 mt-8">
<p>Creato da <a href="https://github.com/danielelozzi" target="_blank" class="text-blue-600 hover:underline">Daddz17</a></p>
</footer>
</body>
</html>