-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
95 lines (88 loc) · 3.99 KB
/
index.js
File metadata and controls
95 lines (88 loc) · 3.99 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
const fileInput = document.getElementById('fileInput');
const processButton = document.getElementById('processButton');
const output = document.getElementById('output');
const message = document.getElementById('message');
const copyButton = document.getElementById('copyButton');
const languageSelect = document.getElementById('languageSelect');
let selectedFile;
fileInput.addEventListener('change', (event) => {
selectedFile = event.target.files[0];
});
processButton.addEventListener('click', async () => {
if (selectedFile) {
const reader = new FileReader();
reader.onload = async (e) => {
const img = new Image();
img.src = e.target.result;
img.onload = async () => {
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
const dataUrl = canvas.toDataURL('image/png');
const text = await extractTextFromImage(dataUrl);
if (text) {
displayTextCharacterByCharacter(text);
output.classList.remove('hidden');
copyButton.classList.remove('hidden');
message.classList.add('hidden');
} else {
message.textContent = 'Fotoğraf üzerinde yazı bulunamadı.';
message.classList.remove('hidden');
output.classList.add('hidden');
copyButton.classList.add('hidden');
}
};
};
reader.readAsDataURL(selectedFile);
} else {
message.textContent = 'Üstündeki yazıyı metin olarak almak için önce bir fotoğraf yüklemelisiniz.';
message.classList.remove('hidden');
output.classList.add('hidden');
copyButton.classList.add('hidden');
}
});
function displayTextCharacterByCharacter(text) {
output.textContent = '';
let index = 0;
const interval = setInterval(() => {
if (index < text.length) {
output.textContent += text[index];
index++;
output.scrollTop = output.scrollHeight;
} else {
clearInterval(interval);
copyButton.classList.remove('hidden');
}
}, 30);
}
copyButton.addEventListener('click', () => {
const text = output.textContent;
navigator.clipboard.writeText(text).then(() => {
showAlert('Yazı kopyalandı!');
copyButton.remove();
}).catch(err => {
console.error('Yazı kopyalanamadı: ', err);
});
});
async function extractTextFromImage(dataUrl) {
const { createWorker } = Tesseract;
const worker = createWorker();
const selectedLanguage = languageSelect.value;
await worker.load();
await worker.loadLanguage(selectedLanguage);
await worker.initialize(selectedLanguage);
const { data: { text } } = await worker.recognize(dataUrl);
await worker.terminate();
return text;
}
function showAlert(message) {
const alertBox = document.createElement('div');
alertBox.className = 'custom-alert';
alertBox.textContent = message;
document.body.appendChild(alertBox);
setTimeout(() => {
alertBox.remove();
}, 3000);
}