changed logic in ocr
This commit is contained in:
@@ -39,9 +39,9 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
});
|
});
|
||||||
</script> --}}
|
</script> --}}
|
||||||
|
|
||||||
{{-- logic --}}
|
|
||||||
|
|
||||||
{{-- <div x-data="cameraCapture()" x-init="initCamera()" class="space-y-2">
|
|
||||||
|
<div x-data="cameraCapture()" x-init="initCamera()" class="space-y-2">
|
||||||
<video x-ref="video" width="320" height="240" autoplay playsinline class="border rounded"></video>
|
<video x-ref="video" width="320" height="240" autoplay playsinline class="border rounded"></video>
|
||||||
<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">
|
||||||
@@ -53,23 +53,9 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{{-- <input type="hidden" name="{{ $getName() }}" x-ref="hiddenInput"> --}}
|
{{-- <input type="hidden" name="{{ $getName() }}" x-ref="hiddenInput"> --}}
|
||||||
{{-- <input type="hidden" x-ref="hiddenInput" x-model="photo1">
|
<input type="hidden" name="photo_data" x-ref="hiddenInput">
|
||||||
</div> --}}
|
|
||||||
|
|
||||||
<div x-data="cameraCapture()" x-init="initCamera()" class="space-y-2">
|
|
||||||
<video x-ref="video" width="320" height="240" autoplay playsinline class="border rounded"></video>
|
|
||||||
<canvas x-ref="canvas" width="320" height="240" class="hidden border rounded"></canvas>
|
|
||||||
|
|
||||||
<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="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>
|
||||||
|
|
||||||
<input type="hidden" x-ref="hiddenInput" x-model="photo1">
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
function cameraCapture() {
|
function cameraCapture() {
|
||||||
return {
|
return {
|
||||||
@@ -100,54 +86,40 @@ function cameraCapture() {
|
|||||||
await this.initCamera();
|
await this.initCamera();
|
||||||
},
|
},
|
||||||
|
|
||||||
// capturePhoto() {
|
|
||||||
// const video = this.$refs.video;
|
|
||||||
// const canvas = this.$refs.canvas;
|
|
||||||
// const snapshot = this.$refs.snapshot;
|
|
||||||
// const context = canvas.getContext('2d');
|
|
||||||
|
|
||||||
// context.drawImage(video, 0, 0, canvas.width, canvas.height);
|
|
||||||
// const dataUrl = canvas.toDataURL('image/png');
|
|
||||||
|
|
||||||
// // stop camera stream after capture
|
|
||||||
// if (this.stream) {
|
|
||||||
// this.stream.getTracks().forEach(track => track.stop());
|
|
||||||
// }
|
|
||||||
|
|
||||||
// snapshot.src = dataUrl;
|
|
||||||
// snapshot.classList.remove('hidden');
|
|
||||||
// this.photoTaken = true;
|
|
||||||
|
|
||||||
// this.$refs.hiddenInput.value = dataUrl;
|
|
||||||
// //@this.set('photo1', dataUrl);
|
|
||||||
// },
|
|
||||||
capturePhoto() {
|
capturePhoto() {
|
||||||
const video = this.$refs.video;
|
const video = this.$refs.video;
|
||||||
const canvas = this.$refs.canvas;
|
const canvas = this.$refs.canvas;
|
||||||
|
const snapshot = this.$refs.snapshot;
|
||||||
const context = canvas.getContext('2d');
|
const context = canvas.getContext('2d');
|
||||||
|
|
||||||
// draw video frame to canvas
|
|
||||||
context.drawImage(video, 0, 0, canvas.width, canvas.height);
|
context.drawImage(video, 0, 0, canvas.width, canvas.height);
|
||||||
|
|
||||||
// convert to data URL
|
|
||||||
const dataUrl = canvas.toDataURL('image/png');
|
const dataUrl = canvas.toDataURL('image/png');
|
||||||
|
|
||||||
// store to Livewire
|
// stop camera stream after capture
|
||||||
@this.set('photo1', dataUrl);
|
|
||||||
|
|
||||||
// stop camera
|
|
||||||
if (this.stream) {
|
if (this.stream) {
|
||||||
this.stream.getTracks().forEach(track => track.stop());
|
this.stream.getTracks().forEach(track => track.stop());
|
||||||
this.stream = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
snapshot.src = dataUrl;
|
||||||
|
snapshot.classList.remove('hidden');
|
||||||
this.photoTaken = true;
|
this.photoTaken = true;
|
||||||
|
|
||||||
// hide video and show canvas in the same place
|
this.$refs.hiddenInput.value = dataUrl;
|
||||||
video.classList.add('hidden');
|
|
||||||
canvas.classList.remove('hidden');
|
|
||||||
},
|
},
|
||||||
|
|
||||||
|
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;
|
||||||
|
|||||||
Reference in New Issue
Block a user