mirror of
				https://github.com/StefBuwalda/cal_counter.git
				synced 2025-10-30 03:10:00 +00:00 
			
		
		
		
	Implemented routes and forms for adding and viewing food items by barcode, including templates for displaying and entering nutritional information. Enhanced the scan workflow to redirect to food item details or entry form. Added admin ability to delete food items. Improved UI for login and scan pages. Updated FoodItem model and form fields for consistency and accuracy.
		
			
				
	
	
		
			58 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			58 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
| {% extends "base.html" %}
 | |
| 
 | |
| {% block content %}
 | |
| <div class="container py-5">
 | |
|     <div class="text-center mb-4">
 | |
|         <h1 class="fw-bold">📷 ZXing Barcode Scanner</h1>
 | |
|         <p class="text-muted">Use your camera to scan barcodes in real time.</p>
 | |
|     </div>
 | |
| 
 | |
|     <div class="d-flex justify-content-center mb-4">
 | |
|         <video id="video" class="border rounded shadow-sm" style="width: 100%; max-width: 500px;" autoplay
 | |
|             muted></video>
 | |
|     </div>
 | |
| 
 | |
|     <div class="d-flex justify-content-center">
 | |
|         <button id="startButton" class="btn btn-primary px-4">Start Scanning</button>
 | |
|         <button id="stopButton" class="btn btn-danger px-4 ms-3">Stop</button>
 | |
|     </div>
 | |
| </div>
 | |
| 
 | |
| 
 | |
| <script type="module">
 | |
|     import { BrowserMultiFormatReader } from 'https://cdn.jsdelivr.net/npm/@zxing/library@0.21.3/+esm';
 | |
| 
 | |
|     // constants
 | |
|     const codeReader = new BrowserMultiFormatReader();
 | |
|     const videoElement = document.getElementById('video');
 | |
| 
 | |
| 
 | |
|     // Start scanning for barcode
 | |
|     document.getElementById('startButton').addEventListener('click', async () => {
 | |
|         console.log('[DEBUG] Start button clicked')
 | |
|         try {
 | |
|             await navigator.mediaDevices.getUserMedia({ video: true });
 | |
|             // Use stream with video.srcObject = stream
 | |
|         } catch (err) {
 | |
|             alert("No camera found or no camera permission");
 | |
|             console.error("Could not access the camera:", err);
 | |
|             return;
 | |
|         }
 | |
|         console.log('[DEBUG] Permission given and at least one device present');
 | |
|         const devices = await codeReader.listVideoInputDevices();
 | |
|         console.log('[DEBUG] Cameras found:', devices);
 | |
|         const selectedDeviceId = devices[0]?.deviceId;
 | |
|         await codeReader.decodeFromVideoDevice(selectedDeviceId, videoElement, async (result, err) => {
 | |
|             if (result) {
 | |
|                 const codeText = result.getText();
 | |
|                 const baseURL = "{{url_for('food_item', barcode='0')}}"
 | |
|                 window.location.href = baseURL.replace("0", encodeURIComponent(codeText))
 | |
|             }
 | |
|         });
 | |
|     });
 | |
| 
 | |
|     document.getElementById('stopButton').addEventListener('click', () => {
 | |
|         codeReader.reset();
 | |
|     });
 | |
| </script>
 | |
| {% endblock %} |