File size: 13,746 Bytes
b6f8f72
b8fdd9a
b6f8f72
 
0304990
 
 
93135d9
0304990
 
 
 
 
2be7589
77eb5c8
 
 
b8fdd9a
 
4d3e6a7
 
 
b8fdd9a
 
 
 
6b3df27
311e78e
 
 
6b3df27
311e78e
 
 
93135d9
 
 
 
 
311e78e
 
77eb5c8
4d3e6a7
 
 
 
 
6578b6b
4d3e6a7
2be7589
0304990
93135d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2be7589
93135d9
 
 
 
 
 
 
 
 
 
 
 
 
6578b6b
93135d9
0304990
 
 
93135d9
 
0304990
 
 
6578b6b
 
 
 
 
93135d9
 
 
6578b6b
 
93135d9
 
 
6578b6b
 
 
93135d9
 
 
 
 
 
 
 
0304990
 
93135d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0304990
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6578b6b
77eb5c8
0304990
93135d9
b8fdd9a
2be7589
 
 
 
 
 
 
 
 
 
 
 
 
93135d9
 
 
2be7589
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77eb5c8
2ac657a
 
 
 
77eb5c8
 
 
2ac657a
77eb5c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6f8f72
6578b6b
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

document.addEventListener('DOMContentLoaded', function() {
    // Initialize empty results array
    const scanResults = [];
    
    // Camera and storage access variables
    let cameraStream = null;
    let isCameraActive = false;
    const cameraVideo = document.getElementById('cameraStream');
    const captureCanvas = document.getElementById('captureCanvas');
    const placeholderImage = document.getElementById('placeholderImage');
    const uploadButton = document.getElementById('uploadButton');
    const captureButton = document.getElementById('captureButton');
    const textPreview = document.querySelector('.text-preview');
    
    // Tab switching functionality
    const tabButtons = document.querySelectorAll('.tab-button');
    const tabContents = document.querySelectorAll('.tab-content');

    // Initialize tabs - show home tab by default
    document.getElementById('home').classList.add('active');
    
    tabButtons.forEach(button => {
        button.addEventListener('click', function(e) {
            e.preventDefault();
            const tabId = this.getAttribute('data-tab');

            // Remove active class from all tabs and buttons
            tabButtons.forEach(btn => btn.classList.remove('active'));
            tabContents.forEach(content => content.classList.remove('active'));

            // Add active class to clicked tab and button
            this.classList.add('active');
            document.getElementById(tabId).classList.add('active');
            
            // Stop camera when leaving home tab
            if (tabId !== 'home') {
                stopCamera();
            }
        });
    });

    // Initialize the first tab as active
    const initialTab = document.querySelector('.tab-button[data-tab="home"]');
    if (initialTab) {
        initialTab.classList.add('active');
    }

    // Request camera permission and initialize camera
    async function initializeCamera() {
        try {
            // Stop any existing camera stream
            stopCamera();
            
            // Try environment camera first, then user camera, then any camera
            const constraints = [
                { video: { facingMode: 'environment' } },
                { video: { facingMode: 'user' } },
                { video: true }
            ];
            
            let stream = null;
            for (const constraint of constraints) {
                try {
                    stream = await navigator.mediaDevices.getUserMedia(constraint);
                    break;
                } catch (err) {
                    console.warn(`Camera constraint failed:`, constraint, err);
                    continue;
                }
            }
            
            if (!stream) {
                throw new Error('No camera available');
            }
            
            cameraStream = stream;
            cameraVideo.srcObject = stream;
            isCameraActive = true;
            
            // Wait for video to load metadata
            await new Promise((resolve) => {
                cameraVideo.onloadedmetadata = resolve;
            });
            
            return true;
        } catch (error) {
            console.error('Error accessing camera:', error);
            alert('Camera access is required for capturing images. Please allow camera permissions and ensure a camera is connected.');
            isCameraActive = false;
            return false;
        }
    }

    // Stop camera stream
    function stopCamera() {
        if (cameraStream) {
            const tracks = cameraStream.getTracks();
            tracks.forEach(track => {
                track.stop();
            });
            cameraStream = null;
        }
        isCameraActive = false;
        cameraVideo.style.display = 'none';
        placeholderImage.style.display = 'block';
    }

    // Capture image from camera
    async function captureImage() {
        // Ensure camera is initialized
        if (!isCameraActive) {
            const initialized = await initializeCamera();
            if (!initialized) {
                alert('Failed to access camera. Please check permissions.');
                return;
            }
        }
        
        try {
            // Show camera stream and hide placeholder
            placeholderImage.style.display = 'none';
            cameraVideo.style.display = 'block';
            
            // Small delay to let camera stabilize
            await new Promise(resolve => setTimeout(resolve, 200));
            
            // Set canvas dimensions to match video
            captureCanvas.width = cameraVideo.videoWidth || 640;
            captureCanvas.height = cameraVideo.videoHeight || 480;
            
            const context = captureCanvas.getContext('2d');
            
            // Draw video frame to canvas
            context.drawImage(cameraVideo, 0, 0, captureCanvas.width, captureCanvas.height);
            
            // Get image data URL
            const imageData = captureCanvas.toDataURL('image/jpeg', 0.8);
            
            // Process the captured image
            processCapturedImage(imageData);
            
            // Stop camera after capture
            stopCamera();
        } catch (error) {
            console.error('Error capturing image:', error);
            alert('Failed to capture image. Please try again.');
            stopCamera();
        }
    }

    // Process captured image and show results
    function processCapturedImage(imageData) {
        // Show image preview
        const imagePreviewContainer = document.querySelector('.image-preview-container');
        const previewImage = document.getElementById('preview-image');
        previewImage.src = imageData;
        imagePreviewContainer.style.display = 'block';
        
        // Store and simulate OCR results with sample data
        const result = {
            type: 'scan',
            content: 'The quick brown fox jumps over the lazy dog\n1234567890',
            confidence: '98.7%',
            timestamp: new Date().toISOString(),
            imageData: imageData
        };
        scanResults.push(result);
        
        // Show results with image preview and processed text
        textPreview.innerHTML = `
            <div class="result-header">
                <div class="result-icon">
                    <i class="fas fa-camera"></i>
                </div>
                <h4>Scan Results</h4>
            </div>
            <div class="result-content">
                <p class="ocr-text">The quick brown fox jumps over the lazy dog</p>
                <p class="ocr-text">1234567890</p>
            </div>
            <div class="result-meta">
                <span class="confidence">Confidence: 98.7%</span>
            </div>
            <p class="timestamp">Processed at ${new Date().toLocaleTimeString()}</p>
        `;
        
        // Add processed text section after results
        const processedTextSection = document.createElement('div');
        processedTextSection.className = 'processed-text-section';
        processedTextSection.innerHTML = `
            <h4>Processed Text</h4>
            <div class="processed-text-content">
The quick brown fox jumps over the lazy dog
1234567890
            </div>
        `;
        
        textPreview.appendChild(processedTextSection);
    }

    // Capture button functionality
    captureButton.addEventListener('click', async function() {
        await captureImage();
    });

    // Upload button functionality
    uploadButton.addEventListener('click', function() {
        // Create file input for upload
        const fileInput = document.createElement('input');
        fileInput.type = 'file';
        fileInput.accept = 'image/*';
        fileInput.style.display = 'none';
        document.body.appendChild(fileInput);
        
        fileInput.addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                // Stop camera if active
                stopCamera();
                
                // Show upload processing
                textPreview.innerHTML = `
                    <div class="upload-animation">
                        <div class="progress-bar">
                            <div class="progress"></div>
                        </div>
                        <p><i class="fas fa-cloud-upload-alt fa-pulse"></i> Processing image...</p>
                    </div>
                `;
                
                const reader = new FileReader();
                reader.onload = function(event) {
                    setTimeout(() => {
                        // Show image preview
                        const imagePreviewContainer = document.querySelector('.image-preview-container');
                        const previewImage = document.getElementById('preview-image');
                        previewImage.src = event.target.result;
                        imagePreviewContainer.style.display = 'block';
                        
                        // Store and simulate OCR results with sample data
                        const result = {
                            type: 'document',
                            content: 'Invoice #INV-2023-0456\nClient: Acme Corporation\nTotal: $1,245.00\nDue Date: 12/15/2023',
                            timestamp: new Date().toISOString(),
                            imageData: event.target.result
                        };
                        scanResults.push(result);
                        
                        // Show results with image preview and processed text
                        textPreview.innerHTML = `
                            <div class="result-header">
                                <div class="result-icon">
                                    <i class="fas fa-file-alt"></i>
                                </div>
                                <h4>Document Analysis</h4>
                            </div>
                            <div class="result-content">
                                <p class="ocr-text">Invoice #INV-2023-0456</p>
                                <p class="ocr-text">Client: Acme Corporation</p>
                                <p class="ocr-text">Total: $1,245.00</p>
                                <p class="ocr-text">Due Date: 12/15/2023</p>
                            </div>
                            <p class="timestamp">Processed at ${new Date().toLocaleTimeString()}</p>
                        `;
                        
                        // Add processed text section after results
                        const processedTextSection = document.createElement('div');
                        processedTextSection.className = 'processed-text-section';
                        processedTextSection.innerHTML = `
                            <h4>Processed Text</h4>
                            <div class="processed-text-content">
Invoice #INV-2023-0456
Client: Acme Corporation
Total: $1,245.00
Due Date: 12/15/2023
                            </div>
                        `;
                        
                        textPreview.appendChild(processedTextSection);
                    }, 1500);
                };
                reader.readAsDataURL(file);
            }
        });
        
        fileInput.click();
    });

    // Function to close image preview
    window.closeImagePreview = function() {
        const imagePreviewContainer = document.querySelector('.image-preview-container');
        imagePreviewContainer.style.display = 'none';
    };
});
// Add scanning animation styles
const style = document.createElement('style');
style.textContent = `
    .scanning-animation {
        position: relative;
        height: 100%;
        overflow: hidden;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        gap: 15px;
    }
    .scan-line {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 2px;
        background: var(--primary-color);
        animation: scan 1.5s linear infinite;
    }
    @keyframes scan {
        0% { top: 0; opacity: 0; }
        10% { opacity: 1; }
        90% { opacity: 1; }
        100% { top: 100%; opacity: 0; }
    }
    .result-header {
        display: flex;
        align-items: center;
        gap: 10px;
        margin-bottom: 12px;
    }
    .result-icon {
        width: 30px;
        height: 30px;
        background: var(--secondary-color);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        color: var(--primary-color);
    }
    .result-content {
        margin: 10px 0;
    }
    .result-meta {
        margin-top: 8px;
        font-size: 0.8rem;
        color: var(--light-text);
    }
    .confidence {
        background: rgba(37, 99, 235, 0.1);
        padding: 4px 8px;
        border-radius: 4px;
    }
    .upload-animation {
        position: relative;
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        gap: 15px;
    }
    .progress-bar {
        width: 80%;
        height: 8px;
        background: var(--border-color);
        border-radius: 4px;
        overflow: hidden;
    }
    .progress {
        height: 100%;
        width: 0;
        background: linear-gradient(90deg, var(--primary-color), var(--primary-dark));
        animation: progress 2.5s ease-out forwards;
    }
    @keyframes progress {
        0% { width: 0; }
        100% { width: 100%; }
    }
`;
document.head.appendChild(style);