Spaces:
Running
Running
IT SHOULD BE AN ANDROID AND IOS APP AND I WANT TO USE THE PHONE CAMERA TO TAKE PICTURE OF THE RECEIT AND IT TRANSORMS INTO INFORMATION TO BE STORED
7b78975 verified Download script.js from eddygonzalez/snapspend-wizard: direct link, hf CLI and curl.
- Browser
- Download file 6.81 kB
-
https://huggingface.co/spaces/eddygonzalez/snapspend-wizard/resolve/main/script.js
- Command line
-
hf download hf://spaces/eddygonzalez/snapspend-wizard/script.js
-
curl -L -o script.js https://huggingface.co/spaces/eddygonzalez/snapspend-wizard/resolve/main/script.js
6.81 kB
| document.addEventListener('DOMContentLoaded', function() { | |
| // Initialize camera functionality | |
| const scanBtn = document.getElementById('scanReceiptBtn'); | |
| const uploadBtn = document.getElementById('uploadReceiptBtn'); | |
| const retakeBtn = document.getElementById('retakeBtn'); | |
| const processBtn = document.getElementById('processBtn'); | |
| const receiptPreview = document.getElementById('receiptPreview'); | |
| const previewImage = document.getElementById('previewImage'); | |
| const receiptForm = document.getElementById('receiptForm'); | |
| if (scanBtn) { | |
| scanBtn.addEventListener('click', async () => { | |
| try { | |
| // Check for mobile device | |
| if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) { | |
| const stream = await navigator.mediaDevices.getUserMedia({ | |
| video: { facingMode: 'environment' }, | |
| audio: false | |
| }); | |
| // Create camera preview | |
| const video = document.createElement('video'); | |
| video.srcObject = stream; | |
| video.setAttribute('playsinline', ''); | |
| video.style.width = '100%'; | |
| video.style.height = 'auto'; | |
| // Replace button with camera preview | |
| scanBtn.replaceWith(video); | |
| video.play(); | |
| // Add capture button | |
| const captureBtn = document.createElement('button'); | |
| captureBtn.className = 'w-full bg-green-600 text-white py-3 px-4 rounded-lg hover:bg-green-700 transition duration-200 mt-2 flex items-center justify-center'; | |
| captureBtn.innerHTML = '<i data-feather="aperture" class="mr-2"></i> Capture'; | |
| video.after(captureBtn); | |
| feather.replace(); | |
| captureBtn.addEventListener('click', () => { | |
| // Create canvas to capture image | |
| const canvas = document.createElement('canvas'); | |
| canvas.width = video.videoWidth; | |
| canvas.height = video.videoHeight; | |
| canvas.getContext('2d').drawImage(video, 0, 0); | |
| // Stop camera stream | |
| stream.getTracks().forEach(track => track.stop()); | |
| // Show preview | |
| previewImage.src = canvas.toDataURL('image/jpeg'); | |
| receiptPreview.classList.remove('hidden'); | |
| video.remove(); | |
| captureBtn.remove(); | |
| }); | |
| } else { | |
| alert('Camera access is only available on mobile devices. Please use the upload option.'); | |
| } | |
| } catch (err) { | |
| console.error('Error accessing camera:', err); | |
| alert('Could not access camera. Please check permissions.'); | |
| } | |
| }); | |
| } | |
| if (uploadBtn) { | |
| uploadBtn.addEventListener('click', () => { | |
| const input = document.createElement('input'); | |
| input.type = 'file'; | |
| input.accept = 'image/*'; | |
| input.onchange = (e) => { | |
| const file = e.target.files[0]; | |
| if (file) { | |
| const reader = new FileReader(); | |
| reader.onload = (event) => { | |
| previewImage.src = event.target.result; | |
| receiptPreview.classList.remove('hidden'); | |
| }; | |
| reader.readAsDataURL(file); | |
| } | |
| }; | |
| input.click(); | |
| }); | |
| } | |
| if (retakeBtn) { | |
| retakeBtn.addEventListener('click', () => { | |
| receiptPreview.classList.add('hidden'); | |
| previewImage.src = ''; | |
| }); | |
| } | |
| if (processBtn) { | |
| processBtn.addEventListener('click', async () => { | |
| // Show loading state | |
| processBtn.innerHTML = '<i data-feather="loader" class="animate-spin mr-1"></i> Processing...'; | |
| feather.replace(); | |
| try { | |
| // In a real app, you would send the image to an OCR API here | |
| // For demo purposes, we'll simulate processing with a timeout | |
| await new Promise(resolve => setTimeout(resolve, 1500)); | |
| // Simulate extracted data | |
| document.querySelector('input[name="amount"]').value = '45.67'; | |
| document.querySelector('select[name="category"]').value = 'Food'; | |
| document.querySelector('input[name="date"]').value = new Date().toISOString().split('T')[0]; | |
| document.querySelector('input[name="merchant"]').value = 'Burger Palace'; | |
| receiptPreview.classList.add('hidden'); | |
| receiptForm.classList.remove('hidden'); | |
| } catch (err) { | |
| console.error('Error processing receipt:', err); | |
| alert('Failed to process receipt. Please try again.'); | |
| } finally { | |
| processBtn.innerHTML = '<i data-feather="zap" class="mr-1"></i> Process'; | |
| feather.replace(); | |
| } | |
| }); | |
| } | |
| // Form submission | |
| if (receiptForm) { | |
| receiptForm.addEventListener('submit', function(e) { | |
| e.preventDefault(); | |
| alert('Receipt saved successfully!'); | |
| receiptForm.reset(); | |
| receiptForm.classList.add('hidden'); | |
| }); | |
| } | |
| // Initialize tooltips | |
| const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]')); | |
| tooltipTriggerList.map(function (tooltipTriggerEl) { | |
| return new bootstrap.Tooltip(tooltipTriggerEl); | |
| }); | |
| }); | |
| // Chart data for the spending chart component | |
| const chartData = { | |
| labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'], | |
| datasets: [ | |
| { | |
| label: 'Food', | |
| data: [120, 190, 150, 200, 180, 220], | |
| backgroundColor: '#6366F1', | |
| }, | |
| { | |
| label: 'Transport', | |
| data: [80, 90, 70, 85, 75, 95], | |
| backgroundColor: '#10B981', | |
| }, | |
| { | |
| label: 'Shopping', | |
| data: [200, 150, 180, 220, 250, 200], | |
| backgroundColor: '#8B5CF6', | |
| }, | |
| { | |
| label: 'Entertainment', | |
| data: [50, 60, 70, 65, 80, 75], | |
| backgroundColor: '#F59E0B', | |
| } | |
| ] | |
| }; |