Added camera capture logic

This commit is contained in:
dhanabalan
2025-10-17 09:36:02 +05:30
parent 04693f960a
commit d7b70f47ed

View File

@@ -44,19 +44,14 @@ document.addEventListener('DOMContentLoaded', () => {
<canvas x-ref="canvas" width="320" height="240" class="hidden"></canvas> <canvas x-ref="canvas" width="320" height="240" class="hidden"></canvas>
<img x-ref="snapshot" class="hidden border rounded max-w-full"> <img x-ref="snapshot" class="hidden border rounded max-w-full">
{{-- <div class="flex space-x-2 mt-2">
<x-filament::button color="primary" @click="capturePhoto" x-show="!photoTaken">Capture</x-filament::button>
<x-filament::button color="secondary" @click="retakePhoto" x-show="photoTaken">Retake</x-filament::button>
<x-filament::button color="gray" @click="switchCamera" x-show="!photoTaken">Switch Camera</x-filament::button>
</div> --}}
<div class="flex space-x-4 mt-2"> <div class="flex space-x-4 mt-2">
<x-filament::button color="primary" @click="capturePhoto" x-show="!photoTaken">Capture</x-filament::button> <x-filament::button color="primary" @click="capturePhoto" x-show="!photoTaken">Capture</x-filament::button>
<x-filament::button color="secondary" @click="retakePhoto" x-show="photoTaken">Retake</x-filament::button> <x-filament::button color="secondary" @click="retakePhoto" x-show="photoTaken">Retake</x-filament::button>
<x-filament::button color="gray" @click="switchCamera" x-show="!photoTaken">Switch Camera</x-filament::button> <x-filament::button color="gray" @click="switchCamera" x-show="!photoTaken">Switch Camera</x-filament::button>
</div> </div>
{{-- <input type="hidden" name="{{ $getName() }}" x-ref="hiddenInput"> --}}
<input type="hidden" name="{{ $getName() }}" x-ref="hiddenInput"> <input type="hidden" name="photo_data" x-ref="hiddenInput">
</div> </div>
<script> <script>
@@ -65,6 +60,7 @@ function cameraCapture() {
stream: null, stream: null,
currentFacingMode: 'user', // 'user' = front, 'environment' = back currentFacingMode: 'user', // 'user' = front, 'environment' = back
photoTaken: false, photoTaken: false,
recognizedText: '',
async initCamera() { async initCamera() {
try { try {
@@ -104,16 +100,29 @@ function cameraCapture() {
snapshot.src = dataUrl; snapshot.src = dataUrl;
snapshot.classList.remove('hidden'); snapshot.classList.remove('hidden');
video.classList.add('hidden');
this.photoTaken = true; this.photoTaken = true;
this.$refs.hiddenInput.value = dataUrl; this.$refs.hiddenInput.value = dataUrl;
}, },
async verifyPhoto() {
this.recognizedText = 'Processing...';
const snapshot = this.$refs.snapshot;
try {
const result = await Tesseract.recognize(snapshot.src, 'eng');
this.recognizedText = result.data.text.trim();
} catch (err) {
console.error('OCR Error:', err);
this.recognizedText = 'Error reading text from image.';
}
},
async retakePhoto() { async retakePhoto() {
this.photoTaken = false; this.photoTaken = false;
this.$refs.snapshot.classList.add('hidden'); this.$refs.snapshot.classList.add('hidden');
this.$refs.video.classList.remove('hidden'); //this.$refs.video.classList.remove('hidden');
await this.initCamera(); await this.initCamera();
} }
} }