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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="ar" dir="rtl">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>موندو لينجوا - نظام OCR</title>
|
7 |
+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
|
8 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/pdf.min.js"></script>
|
9 |
+
<style>
|
10 |
+
body {
|
11 |
+
font-family: Arial, sans-serif;
|
12 |
+
margin: 0;
|
13 |
+
padding: 20px;
|
14 |
+
background-color: #f8f9fa;
|
15 |
+
}
|
16 |
+
.header {
|
17 |
+
background-color: #3b82f6;
|
18 |
+
color: white;
|
19 |
+
padding: 15px;
|
20 |
+
border-radius: 10px;
|
21 |
+
margin-bottom: 20px;
|
22 |
+
text-align: center;
|
23 |
+
}
|
24 |
+
.container {
|
25 |
+
max-width: 800px;
|
26 |
+
margin: 0 auto;
|
27 |
+
}
|
28 |
+
.card {
|
29 |
+
background-color: white;
|
30 |
+
border-radius: 10px;
|
31 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
32 |
+
padding: 20px;
|
33 |
+
margin-bottom: 20px;
|
34 |
+
}
|
35 |
+
.pdf-grid {
|
36 |
+
display: grid;
|
37 |
+
grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
|
38 |
+
gap: 10px;
|
39 |
+
margin-top: 15px;
|
40 |
+
}
|
41 |
+
.pdf-page {
|
42 |
+
border: 1px solid #ddd;
|
43 |
+
border-radius: 5px;
|
44 |
+
padding: 5px;
|
45 |
+
position: relative;
|
46 |
+
cursor: pointer;
|
47 |
+
transition: all 0.2s;
|
48 |
+
}
|
49 |
+
.pdf-page:hover {
|
50 |
+
transform: translateY(-3px);
|
51 |
+
box-shadow: 0 3px 10px rgba(0,0,0,0.1);
|
52 |
+
}
|
53 |
+
.pdf-page.selected {
|
54 |
+
border: 2px solid #3b82f6;
|
55 |
+
}
|
56 |
+
.pdf-page img {
|
57 |
+
width: 100%;
|
58 |
+
height: auto;
|
59 |
+
border-radius: 3px;
|
60 |
+
}
|
61 |
+
.page-number {
|
62 |
+
position: absolute;
|
63 |
+
bottom: 0;
|
64 |
+
left: 0;
|
65 |
+
right: 0;
|
66 |
+
background-color: rgba(0,0,0,0.6);
|
67 |
+
color: white;
|
68 |
+
text-align: center;
|
69 |
+
font-size: 12px;
|
70 |
+
padding: 2px;
|
71 |
+
}
|
72 |
+
.btn-primary {
|
73 |
+
background-color: #3b82f6;
|
74 |
+
border-color: #3b82f6;
|
75 |
+
}
|
76 |
+
.btn-success {
|
77 |
+
background-color: #10b981;
|
78 |
+
border-color: #10b981;
|
79 |
+
}
|
80 |
+
.result-text {
|
81 |
+
max-height: 300px;
|
82 |
+
overflow-y: auto;
|
83 |
+
white-space: pre-wrap;
|
84 |
+
direction: rtl;
|
85 |
+
border: 1px solid #ddd;
|
86 |
+
padding: 10px;
|
87 |
+
border-radius: 5px;
|
88 |
+
background-color: #f8f9fa;
|
89 |
+
}
|
90 |
+
.spinner-border {
|
91 |
+
width: 1.5rem;
|
92 |
+
height: 1.5rem;
|
93 |
+
margin-left: 0.5rem;
|
94 |
+
}
|
95 |
+
.logo {
|
96 |
+
font-weight: bold;
|
97 |
+
font-size: 24px;
|
98 |
+
display: inline-block;
|
99 |
+
margin-bottom: 5px;
|
100 |
+
}
|
101 |
+
.logo span {
|
102 |
+
color: #bfdbfe;
|
103 |
+
}
|
104 |
+
</style>
|
105 |
+
</head>
|
106 |
+
<body>
|
107 |
+
<div class="container">
|
108 |
+
<div class="header">
|
109 |
+
<div class="logo">مـوندو <span>لينجـوا</span></div>
|
110 |
+
<h1>نظام التعرف الضوئي على النصوص</h1>
|
111 |
+
<p class="mb-0">استخراج النصوص من الصور والملفات متعددة الصفحات</p>
|
112 |
+
</div>
|
113 |
+
|
114 |
+
<!-- بطاقة تحميل الملف -->
|
115 |
+
<div class="card">
|
116 |
+
<h3>تحميل الملف</h3>
|
117 |
+
<div class="mb-3">
|
118 |
+
<input type="file" class="form-control" id="fileInput" accept=".pdf,.jpg,.jpeg,.png">
|
119 |
+
<div class="form-text">يمكنك تحميل ملف PDF (حتى 100 صفحة) أو صورة</div>
|
120 |
+
</div>
|
121 |
+
<button id="processBtn" class="btn btn-primary">معالجة الملف</button>
|
122 |
+
</div>
|
123 |
+
|
124 |
+
<!-- عرض حالة المعالجة -->
|
125 |
+
<div id="processingStatus" class="card d-none">
|
126 |
+
<h3>حالة المعالجة</h3>
|
127 |
+
<div class="alert alert-info">
|
128 |
+
<div class="d-flex align-items-center">
|
129 |
+
<div class="spinner-border text-primary" role="status"></div>
|
130 |
+
<span id="statusText" class="ms-2">جاري معالجة الملف...</span>
|
131 |
+
</div>
|
132 |
+
</div>
|
133 |
+
<div class="progress mt-2">
|
134 |
+
<div id="progressBar" class="progress-bar" role="progressbar" style="width: 0%"></div>
|
135 |
+
</div>
|
136 |
+
</div>
|
137 |
+
|
138 |
+
<!-- عرض صفحات PDF -->
|
139 |
+
<div id="pdfPagesCard" class="card d-none">
|
140 |
+
<h3>صفحات الملف</h3>
|
141 |
+
<p>اختر الصفحات التي تريد معالجتها (انقر للتحديد)</p>
|
142 |
+
|
143 |
+
<div class="mb-3">
|
144 |
+
<button id="selectAllBtn" class="btn btn-sm btn-outline-primary me-2">تحديد الكل</button>
|
145 |
+
<button id="deselectAllBtn" class="btn btn-sm btn-outline-secondary">إلغاء تحديد الكل</button>
|
146 |
+
</div>
|
147 |
+
|
148 |
+
<div id="pdfPagesContainer" class="pdf-grid"></div>
|
149 |
+
|
150 |
+
<div class="mt-3">
|
151 |
+
<button id="extractImagesBtn" class="btn btn-success me-2">تحويل إلى صور</button>
|
152 |
+
<button id="extractTextBtn" class="btn btn-primary">استخراج النص</button>
|
153 |
+
</div>
|
154 |
+
</div>
|
155 |
+
|
156 |
+
<!-- عرض النتائج -->
|
157 |
+
<div id="resultsCard" class="card d-none">
|
158 |
+
<h3>النص المستخرج</h3>
|
159 |
+
<div id="resultText" class="result-text mt-3">
|
160 |
+
لم يتم استخراج نص بعد.
|
161 |
+
</div>
|
162 |
+
<div class="mt-3">
|
163 |
+
<button id="copyTextBtn" class="btn btn-outline-primary me-2">نسخ النص</button>
|
164 |
+
<button id="downloadTextBtn" class="btn btn-success me-2">تنزيل النص</button>
|
165 |
+
<button id="downloadWordBtn" class="btn btn-success">تنزيل النص إلى Word</button>
|
166 |
+
</div>
|
167 |
+
</div>
|
168 |
+
</div>
|
169 |
+
|
170 |
+
<script>
|
171 |
+
// تهيئة PDF.js
|
172 |
+
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/pdf.worker.min.js';
|
173 |
+
|
174 |
+
// إعدادات API4AI OCR
|
175 |
+
const RAPIDAPI_KEY = 'eb11693cddmshb8bd157e05b74acp1f6aa4jsn4369fa546e55';
|
176 |
+
const OCR_API_URL = 'https://ocr43.p.rapidapi.com/v1/results';
|
177 |
+
|
178 |
+
// متغيرات عامة
|
179 |
+
let documentPages = [];
|
180 |
+
let selectedPages = [];
|
181 |
+
let extractedTexts = [];
|
182 |
+
|
183 |
+
// عناصر DOM
|
184 |
+
const fileInput = document.getElementById('fileInput');
|
185 |
+
const processBtn = document.getElementById('processBtn');
|
186 |
+
const processingStatus = document.getElementById('processingStatus');
|
187 |
+
const statusText = document.getElementById('statusText');
|
188 |
+
const progressBar = document.getElementById('progressBar');
|
189 |
+
const pdfPagesCard = document.getElementById('pdfPagesCard');
|
190 |
+
const pdfPagesContainer = document.getElementById('pdfPagesContainer');
|
191 |
+
const selectAllBtn = document.getElementById('selectAllBtn');
|
192 |
+
const deselectAllBtn = document.getElementById('deselectAllBtn');
|
193 |
+
const extractImagesBtn = document.getElementById('extractImagesBtn');
|
194 |
+
const extractTextBtn = document.getElementById('extractTextBtn');
|
195 |
+
const resultsCard = document.getElementById('resultsCard');
|
196 |
+
const resultText = document.getElementById('resultText');
|
197 |
+
const copyTextBtn = document.getElementById('copyTextBtn');
|
198 |
+
const downloadTextBtn = document.getElementById('downloadTextBtn');
|
199 |
+
const downloadWordBtn = document.getElementById('downloadWordBtn');
|
200 |
+
|
201 |
+
// إضافة مستمعات الأحداث
|
202 |
+
document.addEventListener('DOMContentLoaded', function() {
|
203 |
+
processBtn.addEventListener('click', processFile);
|
204 |
+
selectAllBtn.addEventListener('click', selectAllPages);
|
205 |
+
deselectAllBtn.addEventListener('click', deselectAllPages);
|
206 |
+
extractImagesBtn.addEventListener('click', extractImages);
|
207 |
+
extractTextBtn.addEventListener('click', extractText);
|
208 |
+
copyTextBtn.addEventListener('click', copyText);
|
209 |
+
downloadTextBtn.addEventListener('click', downloadText);
|
210 |
+
downloadWordBtn.addEventListener('click', downloadWord);
|
211 |
+
});
|
212 |
+
|
213 |
+
// معالجة الملف
|
214 |
+
async function processFile() {
|
215 |
+
if (!fileInput.files || fileInput.files.length === 0) {
|
216 |
+
alert('الرجاء اختيار ملف أولاً');
|
217 |
+
return;
|
218 |
+
}
|
219 |
+
|
220 |
+
const file = fileInput.files[0];
|
221 |
+
|
222 |
+
// إظهار حالة المعالجة
|
223 |
+
processingStatus.classList.remove('d-none');
|
224 |
+
statusText.textContent = 'جاري معالجة الملف...';
|
225 |
+
progressBar.style.width = '0%';
|
226 |
+
|
227 |
+
// إخفاء بطاقات أخرى
|
228 |
+
pdfPagesCard.classList.add('d-none');
|
229 |
+
resultsCard.classList.add('d-none');
|
230 |
+
|
231 |
+
// إفراغ مصفوفات الصفحات
|
232 |
+
documentPages = [];
|
233 |
+
selectedPages = [];
|
234 |
+
extractedTexts = [];
|
235 |
+
|
236 |
+
try {
|
237 |
+
const fileType = file.name.split('.').pop().toLowerCase();
|
238 |
+
|
239 |
+
if (fileType === 'pdf') {
|
240 |
+
await processPdf(file);
|
241 |
+
} else if (['jpg', 'jpeg', 'png'].includes(fileType)) {
|
242 |
+
await processImage(file);
|
243 |
+
} else {
|
244 |
+
throw new Error('نوع الملف غير مدعوم. يرجى اختيار ملف PDF أو صورة.');
|
245 |
+
}
|
246 |
+
|
247 |
+
// إظهار بطاقة صفحات PDF
|
248 |
+
pdfPagesCard.classList.remove('d-none');
|
249 |
+
|
250 |
+
// إخفاء حالة المعالجة
|
251 |
+
processingStatus.classList.add('d-none');
|
252 |
+
|
253 |
+
} catch (error) {
|
254 |
+
console.error('Error processing file:', error);
|
255 |
+
statusText.textContent = `خطأ: ${error.message}`;
|
256 |
+
}
|
257 |
+
}
|
258 |
+
|
259 |
+
// معالجة ملف PDF
|
260 |
+
async function processPdf(file) {
|
261 |
+
try {
|
262 |
+
// تحميل ملف PDF
|
263 |
+
const arrayBuffer = await file.arrayBuffer();
|
264 |
+
const pdfDoc = await pdfjsLib.getDocument({ data: arrayBuffer }).promise;
|
265 |
+
|
266 |
+
// التحقق من عدد الصفحات
|
267 |
+
const numPages = pdfDoc.numPages;
|
268 |
+
if (numPages > 100) {
|
269 |
+
alert('هذا الملف يحتوي على أكثر من 100 صفحة. سيتم معالجة أول 100 صفحة فقط.');
|
270 |
+
}
|
271 |
+
|
272 |
+
// مسح حاوية الصفحات
|
273 |
+
pdfPagesContainer.innerHTML = '';
|
274 |
+
|
275 |
+
// عرض أول 100 صفحة كصور مصغرة
|
276 |
+
const maxPages = Math.min(numPages, 100);
|
277 |
+
for (let i = 1; i <= maxPages; i++) {
|
278 |
+
// تحديث شريط التقدم
|
279 |
+
progressBar.style.width = `${(i / maxPages) * 100}%`;
|
280 |
+
statusText.textContent = `جاري معالجة الصفحة ${i} من ${maxPages}...`;
|
281 |
+
|
282 |
+
// تحويل الصف��ة إلى صورة
|
283 |
+
const pageImage = await convertPdfPageToImage(pdfDoc, i);
|
284 |
+
|
285 |
+
// إضافة الصفحة إلى المصفوفة
|
286 |
+
documentPages.push({
|
287 |
+
pageNumber: i,
|
288 |
+
imageData: pageImage.imageData,
|
289 |
+
width: pageImage.width,
|
290 |
+
height: pageImage.height
|
291 |
+
});
|
292 |
+
|
293 |
+
// إنشاء عنصر للصفحة في واجهة المستخدم
|
294 |
+
createPageElement(pageImage.imageData, i);
|
295 |
+
}
|
296 |
+
|
297 |
+
} catch (error) {
|
298 |
+
console.error('Error processing PDF:', error);
|
299 |
+
throw error;
|
300 |
+
}
|
301 |
+
}
|
302 |
+
|
303 |
+
// معالجة ملف صورة
|
304 |
+
async function processImage(file) {
|
305 |
+
try {
|
306 |
+
// قراءة الصورة كـ Data URL
|
307 |
+
const imageData = await readFileAsDataURL(file);
|
308 |
+
|
309 |
+
// إنشاء صورة لقياس أبعادها
|
310 |
+
const img = new Image();
|
311 |
+
await new Promise((resolve, reject) => {
|
312 |
+
img.onload = resolve;
|
313 |
+
img.onerror = reject;
|
314 |
+
img.src = imageData;
|
315 |
+
});
|
316 |
+
|
317 |
+
// مسح حاوية الصفحات
|
318 |
+
pdfPagesContainer.innerHTML = '';
|
319 |
+
|
320 |
+
// إضافة الصورة إلى المصفوفة
|
321 |
+
documentPages = [{
|
322 |
+
pageNumber: 1,
|
323 |
+
imageData: imageData,
|
324 |
+
width: img.width,
|
325 |
+
height: img.height
|
326 |
+
}];
|
327 |
+
|
328 |
+
// إنشاء عنصر للصورة في واجهة المستخدم
|
329 |
+
createPageElement(imageData, 1);
|
330 |
+
|
331 |
+
// تحديث شريط التقدم
|
332 |
+
progressBar.style.width = '100%';
|
333 |
+
statusText.textContent = 'تمت معالجة الصورة بنجاح';
|
334 |
+
|
335 |
+
} catch (error) {
|
336 |
+
console.error('Error processing image:', error);
|
337 |
+
throw error;
|
338 |
+
}
|
339 |
+
}
|
340 |
+
|
341 |
+
// تحويل صفحة PDF إلى صورة
|
342 |
+
async function convertPdfPageToImage(pdfDoc, pageNumber, scale = 1.5) {
|
343 |
+
try {
|
344 |
+
// الحصول على الصفحة من مستند PDF
|
345 |
+
const page = await pdfDoc.getPage(pageNumber);
|
346 |
+
|
347 |
+
// إنشاء عنصر canvas لرسم الصفحة
|
348 |
+
const canvas = document.createElement('canvas');
|
349 |
+
const context = canvas.getContext('2d');
|
350 |
+
|
351 |
+
// ضبط حجم الـ canvas بناءً على حجم الصفحة ومقياس التكبير
|
352 |
+
const viewport = page.getViewport({ scale });
|
353 |
+
canvas.width = viewport.width;
|
354 |
+
canvas.height = viewport.height;
|
355 |
+
|
356 |
+
// رسم الصفحة على الـ canvas
|
357 |
+
await page.render({
|
358 |
+
canvasContext: context,
|
359 |
+
viewport: viewport
|
360 |
+
}).promise;
|
361 |
+
|
362 |
+
// تحويل الـ canvas إلى صورة بصيغة PNG
|
363 |
+
return {
|
364 |
+
imageData: canvas.toDataURL('image/png'),
|
365 |
+
width: viewport.width,
|
366 |
+
height: viewport.height,
|
367 |
+
pageNumber: pageNumber
|
368 |
+
};
|
369 |
+
} catch (error) {
|
370 |
+
console.error(`Error converting PDF page ${pageNumber} to image:`, error);
|
371 |
+
throw error;
|
372 |
+
}
|
373 |
+
}
|
374 |
+
|
375 |
+
// إنشاء عنصر للصفحة في واجهة المستخدم
|
376 |
+
function createPageElement(imageData, pageNumber) {
|
377 |
+
const pageDiv = document.createElement('div');
|
378 |
+
pageDiv.className = 'pdf-page';
|
379 |
+
pageDiv.dataset.page = pageNumber;
|
380 |
+
|
381 |
+
const img = document.createElement('img');
|
382 |
+
img.src = imageData;
|
383 |
+
img.alt = `صفحة ${pageNumber}`;
|
384 |
+
|
385 |
+
const pageNumberDiv = document.createElement('div');
|
386 |
+
pageNumberDiv.className = 'page-number';
|
387 |
+
pageNumberDiv.textContent = `صفحة ${pageNumber}`;
|
388 |
+
|
389 |
+
pageDiv.appendChild(img);
|
390 |
+
pageDiv.appendChild(pageNumberDiv);
|
391 |
+
|
392 |
+
// إضافة حدث النقر للتحديد
|
393 |
+
pageDiv.addEventListener('click', function() {
|
394 |
+
this.classList.toggle('selected');
|
395 |
+
|
396 |
+
// تحديث مصفوفة الصفحات المحددة
|
397 |
+
const page = parseInt(this.dataset.page);
|
398 |
+
if (this.classList.contains('selected')) {
|
399 |
+
if (!selectedPages.includes(page)) {
|
400 |
+
selectedPages.push(page);
|
401 |
+
}
|
402 |
+
} else {
|
403 |
+
const index = selectedPages.indexOf(page);
|
404 |
+
if (index > -1) {
|
405 |
+
selectedPages.splice(index, 1);
|
406 |
+
}
|
407 |
+
}
|
408 |
+
});
|
409 |
+
|
410 |
+
pdfPagesContainer.appendChild(pageDiv);
|
411 |
+
}
|
412 |
+
|
413 |
+
// قراءة ملف كـ Data URL
|
414 |
+
function readFileAsDataURL(file) {
|
415 |
+
return new Promise((resolve, reject) => {
|
416 |
+
const reader = new FileReader();
|
417 |
+
reader.onload = e => resolve(e.target.result);
|
418 |
+
reader.onerror = reject;
|
419 |
+
reader.readAsDataURL(file);
|
420 |
+
});
|
421 |
+
}
|
422 |
+
|
423 |
+
// تحديد كل الصفحات
|
424 |
+
function selectAllPages() {
|
425 |
+
document.querySelectorAll('.pdf-page').forEach(page => {
|
426 |
+
page.classList.add('selected');
|
427 |
+
const pageNumber = parseInt(page.dataset.page);
|
428 |
+
if (!selectedPages.includes(pageNumber)) {
|
429 |
+
selectedPages.push(pageNumber);
|
430 |
+
}
|
431 |
+
});
|
432 |
+
}
|
433 |
+
|
434 |
+
// إلغاء تحديد كل الصفحات
|
435 |
+
function deselectAllPages() {
|
436 |
+
document.querySelectorAll('.pdf-page').forEach(page => {
|
437 |
+
page.classList.remove('selected');
|
438 |
+
});
|
439 |
+
selectedPages = [];
|
440 |
+
}
|
441 |
+
|
442 |
+
// استخراج الصور
|
443 |
+
function extractImages() {
|
444 |
+
if (documentPages.length === 0) {
|
445 |
+
alert('لا توجد صفحات للمعالجة');
|
446 |
+
return;
|
447 |
+
}
|
448 |
+
|
449 |
+
// اختيار الصفحات المحددة فقط أو كل الصفحات إذا لم يتم تحديد أي صفحة
|
450 |
+
const pagesToExtract = selectedPages.length > 0
|
451 |
+
? documentPages.filter(page => selectedPages.includes(page.pageNumber))
|
452 |
+
: documentPages;
|
453 |
+
|
454 |
+
if (pagesToExtract.length === 0) {
|
455 |
+
alert('الرجاء تحديد صفحة واحدة على الأقل');
|
456 |
+
return;
|
457 |
+
}
|
458 |
+
|
459 |
+
// إنشاء وتنزيل كل صورة
|
460 |
+
pagesToExtract.forEach(page => {
|
461 |
+
const a = document.createElement('a');
|
462 |
+
a.href = page.imageData;
|
463 |
+
a.download = `mondo_lingua_page_${page.pageNumber}.png`;
|
464 |
+
document.body.appendChild(a);
|
465 |
+
a.click();
|
466 |
+
document.body.removeChild(a);
|
467 |
+
});
|
468 |
+
|
469 |
+
// إظهار رسالة نجاح
|
470 |
+
alert(`تم استخراج ${pagesToExtract.length} صورة بنجاح`);
|
471 |
+
}
|
472 |
+
|
473 |
+
// استخراج النص
|
474 |
+
async function extractText() {
|
475 |
+
if (documentPages.length === 0) {
|
476 |
+
alert('لا توجد صفحات للمعالجة');
|
477 |
+
return;
|
478 |
+
}
|
479 |
+
|
480 |
+
// اختيار الصفحات المحددة فقط أو كل الصفحات إذا لم يتم تحديد أي صفحة
|
481 |
+
const pagesToProcess = selectedPages.length > 0
|
482 |
+
? documentPages.filter(page => selectedPages.includes(page.pageNumber))
|
483 |
+
: documentPages;
|
484 |
+
|
485 |
+
if (pagesToProcess.length === 0) {
|
486 |
+
alert('الرجاء تحديد صفحة واحدة على الأقل');
|
487 |
+
return;
|
488 |
+
}
|
489 |
+
|
490 |
+
// إظهار حالة المعالجة
|
491 |
+
processingStatus.classList.remove('d-none');
|
492 |
+
statusText.textContent = 'جاري استخراج النص...';
|
493 |
+
progressBar.style.width = '0%';
|
494 |
+
|
495 |
+
try {
|
496 |
+
extractedTexts = [];
|
497 |
+
|
498 |
+
// معالجة كل صفحة
|
499 |
+
for (let i = 0; i < pagesToProcess.length; i++) {
|
500 |
+
const page = pagesToProcess[i];
|
501 |
+
|
502 |
+
// تحديث التقدم
|
503 |
+
progressBar.style.width = `${((i + 1) / pagesToProcess.length) * 100}%`;
|
504 |
+
statusText.textContent = `جاري معالجة الصفحة ${i + 1} من ${pagesToProcess.length}...`;
|
505 |
+
|
506 |
+
// استخراج النص من الصورة باستخدام OCR API
|
507 |
+
const pageText = await extractTextFromImage(page.imageData, page.pageNumber);
|
508 |
+
extractedTexts.push(pageText);
|
509 |
+
}
|
510 |
+
|
511 |
+
// جمع النصوص المستخرجة
|
512 |
+
const combinedText = extractedTexts.join('\n\n');
|
513 |
+
|
514 |
+
// عرض النص المستخرج
|
515 |
+
resultText.textContent = combinedText;
|
516 |
+
resultsCard.classList.remove('d-none');
|
517 |
+
|
518 |
+
// إخفاء حالة المعالجة
|
519 |
+
processingStatus.classList.add('d-none');
|
520 |
+
|
521 |
+
// التمرير إلى النتائج
|
522 |
+
resultsCard.scrollIntoView({ behavior: 'smooth' });
|
523 |
+
|
524 |
+
} catch (error) {
|
525 |
+
console.error('Error extracting text:', error);
|
526 |
+
statusText.textContent = `خطأ: ${error.message}`;
|
527 |
+
}
|
528 |
+
}
|
529 |
+
|
530 |
+
// استخراج النص من صورة باستخدام API4AI OCR
|
531 |
+
async function extractTextFromImage(imageData, pageNumber) {
|
532 |
+
try {
|
533 |
+
// تحويل Data URL إلى Blob
|
534 |
+
const response = await fetch(imageData);
|
535 |
+
const blob = await response.blob();
|
536 |
+
|
537 |
+
// إنشاء FormData وإضافة الصورة
|
538 |
+
const formData = new FormData();
|
539 |
+
formData.append('image', blob, `page_${pageNumber}.png`);
|
540 |
+
|
541 |
+
// طلب OCR
|
542 |
+
const ocrResponse = await fetch(OCR_API_URL, {
|
543 |
+
method: 'POST',
|
544 |
+
headers: {
|
545 |
+
'X-RapidAPI-Key': RAPIDAPI_KEY,
|
546 |
+
'X-RapidAPI-Host': 'ocr43.p.rapidapi.com'
|
547 |
+
},
|
548 |
+
body: formData
|
549 |
+
});
|
550 |
+
|
551 |
+
if (!ocrResponse.ok) {
|
552 |
+
throw new Error(`فشل في طلب OCR: ${ocrResponse.status}`);
|
553 |
+
}
|
554 |
+
|
555 |
+
const data = await ocrResponse.json();
|
556 |
+
|
557 |
+
try {
|
558 |
+
// استخراج النص من الاستجابة
|
559 |
+
const text = data.results[0].entities[0].objects[0].entities[0].text;
|
560 |
+
return `=== صفحة ${pageNumber} ===\n${text}`;
|
561 |
+
} catch (e) {
|
562 |
+
console.error('Error parsing OCR response:', e);
|
563 |
+
return `=== صفحة ${pageNumber} ===\n[خطأ في معالجة النص]`;
|
564 |
+
}
|
565 |
+
} catch (error) {
|
566 |
+
console.error(`Error in OCR for page ${pageNumber}:`, error);
|
567 |
+
throw error;
|
568 |
+
}
|
569 |
+
}
|
570 |
+
|
571 |
+
// نسخ النص
|
572 |
+
function copyText() {
|
573 |
+
const text = resultText.textContent;
|
574 |
+
if (!text || text === 'لم يتم استخراج نص بعد.') {
|
575 |
+
alert('لا يوجد نص للنسخ');
|
576 |
+
return;
|
577 |
+
}
|
578 |
+
|
579 |
+
navigator.clipboard.writeText(text)
|
580 |
+
.then(() => alert('تم نسخ النص بنجاح'))
|
581 |
+
.catch(err => alert('حدث خطأ أثناء نسخ النص: ' + err));
|
582 |
+
}
|
583 |
+
|
584 |
+
// تنزيل النص
|
585 |
+
function downloadText() {
|
586 |
+
const text = resultText.textContent;
|
587 |
+
if (!text || text === 'لم يتم استخراج نص بعد.') {
|
588 |
+
alert('لا يوجد نص للتنزيل');
|
589 |
+
return;
|
590 |
+
}
|
591 |
+
|
592 |
+
const blob = new Blob([text], { type: 'text/plain;charset=utf-8' });
|
593 |
+
const url = URL.createObjectURL(blob);
|
594 |
+
const a = document.createElement('a');
|
595 |
+
a.href = url;
|
596 |
+
a.download = 'mondo_lingua_ocr_text.txt';
|
597 |
+
document.body.appendChild(a);
|
598 |
+
a.click();
|
599 |
+
document.body.removeChild(a);
|
600 |
+
URL.revokeObjectURL(url);
|
601 |
+
}
|
602 |
+
|
603 |
+
// تنزيل النص إلى Word
|
604 |
+
function downloadWord() {
|
605 |
+
const text = resultText.textContent;
|
606 |
+
if (!text || text === 'لم يتم استخراج نص بعد.') {
|
607 |
+
alert('لا يوجد نص للتنزيل');
|
608 |
+
return;
|
609 |
+
}
|
610 |
+
// تغليف النص ببنية HTML بسيطة لتكون قابلة للعرض في Word
|
611 |
+
const header = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'><head><meta charset='utf-8'><title>Document</title></head><body>";
|
612 |
+
const footer = "</body></html>";
|
613 |
+
const sourceHTML = header + text + footer;
|
614 |
+
const blob = new Blob(['\ufeff', sourceHTML], { type: 'application/msword' });
|
615 |
+
const url = URL.createObjectURL(blob);
|
616 |
+
const a = document.createElement('a');
|
617 |
+
a.href = url;
|
618 |
+
a.download = 'mondo_lingua_ocr_text.doc';
|
619 |
+
document.body.appendChild(a);
|
620 |
+
a.click();
|
621 |
+
document.body.removeChild(a);
|
622 |
+
URL.revokeObjectURL(url);
|
623 |
+
}
|
624 |
+
</script>
|
625 |
+
</body>
|
626 |
+
</html>
|