From b1c7a4df910a311137a882062052ccd4880c9988 Mon Sep 17 00:00:00 2001 From: Pradeeppon01 Date: Wed, 3 Jul 2024 23:13:35 +0530 Subject: [PATCH] latest --- package-lock.json | 11 +++ package.json | 1 + src/Components/BarcodeScanner.jsx | 155 ++++++++++++++++++++++-------- 3 files changed, 126 insertions(+), 41 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5997d8d..8eedf4e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "antd": "^5.17.3", "bootstrap": "^5.3.3", "dotenv": "^16.4.5", + "html5-qrcode": "^2.1.5", "react": "^18.3.1", "react-bootstrap": "^2.10.2", "react-dom": "^18.3.1", @@ -3592,6 +3593,11 @@ "react-is": "^16.7.0" } }, + "node_modules/html5-qrcode": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/html5-qrcode/-/html5-qrcode-2.1.5.tgz", + "integrity": "sha512-3cOA0lPIcKtMd7Sz9BZm5ERAokv5uj35zT3o59LMVF6wLesYJ4WZaD28Z0OPnsfxe4dHGFgZ3RZ1si8f2AfOGw==" + }, "node_modules/ignore": { "version": "5.3.1", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.1.tgz", @@ -8679,6 +8685,11 @@ "react-is": "^16.7.0" } }, + "html5-qrcode": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/html5-qrcode/-/html5-qrcode-2.1.5.tgz", + "integrity": "sha512-3cOA0lPIcKtMd7Sz9BZm5ERAokv5uj35zT3o59LMVF6wLesYJ4WZaD28Z0OPnsfxe4dHGFgZ3RZ1si8f2AfOGw==" + }, "ignore": { "version": "5.3.1", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.1.tgz", diff --git a/package.json b/package.json index 84f0b13..9417ff9 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "antd": "^5.17.3", "bootstrap": "^5.3.3", "dotenv": "^16.4.5", + "html5-qrcode": "^2.1.5", "react": "^18.3.1", "react-bootstrap": "^2.10.2", "react-dom": "^18.3.1", diff --git a/src/Components/BarcodeScanner.jsx b/src/Components/BarcodeScanner.jsx index dc70a50..d99cf71 100644 --- a/src/Components/BarcodeScanner.jsx +++ b/src/Components/BarcodeScanner.jsx @@ -1,50 +1,123 @@ -import React, { useState, useRef, useCallback } from 'react'; -import Webcam from 'react-webcam'; -import { QrReader } from 'react-qr-barcode-scanner'; +import React, { useState, useEffect } from "react"; +import { Html5QrcodeScanner } from "html5-qrcode"; +import { Box } from "@mui/material"; +import LoadingContainer from "./LoadingContainer"; const BarcodeScanner = () => { - const [hasCameraPermission, setHasCameraPermission] = useState(false); - const [barcodeData, setBarcodeData] = useState(null); - const webcamRef = useRef(null); + const [scanResult, setScanResult] = useState(null); + const [isLoading, setIsLoading] = useState(false); + const [barcodeInfo, setBarcodeInfo] = useState([]); + const [marksData, setMarksData] = useState([]); - const handleUserMedia = useCallback(() => { - setHasCameraPermission(true); + useEffect(() => { + const scanner = new Html5QrcodeScanner("reader", { + qrbox: { + width: 250, + height: 250, + }, + fps: 5, + }); + + const fetchBarcodeData = (result) => { + setIsLoading(true); + try { + const payload = { + qrcodeValue: result, + }; + fetch( + `${ + import.meta.env.VITE_REACT_APP_BACKEND_URL + }/fetchQrcodeScannedInfo`, + { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(payload), + } + ) + .then((response) => response.json()) + .then((responseData) => { + setIsLoading(false); + if (responseData.status === "success") { + setBarcodeInfo(responseData.results); + setMarksData(responseData?.marks); + } + }); + } catch (error) { + setIsLoading(false); + throw new Error(error); + } + }; + + const success = (result) => { + scanner.clear(); + setScanResult(result); + fetchBarcodeData(result); + }; + + const error = (err) => { + console.log("Error: ", err); + }; + + // Ensure the element is in the DOM before initializing the scanner + if (document.getElementById("reader")) { + scanner.render(success, error); + } + + // Cleanup the scanner on component unmount + return () => { + scanner.clear(); + }; }, []); - const handleScan = (result) => { - if (result) { - setBarcodeData(result.text); - } - }; - - const handleError = (error) => { - console.error(error); - }; - return ( -
-

Barcode Scanner

- {!hasCameraPermission ? ( - - ) : ( - - )} - {barcodeData && ( -
-

Scanned Barcode:

-

{barcodeData}

-
- )} -
+ + +

Welcome to exampaper.vidh.ai

+
+ + + {scanResult ? ( +
QR : {scanResult}
+ ) : ( +
+ )} +
+ + {scanResult ? ( +
QR : {scanResult}
+ ) : ( +
+ )} +
+
+ + {barcodeInfo.length > 0 && ( + +
Candidate Name: {barcodeInfo[0]?.candidate_name}
+
Register Name : {barcodeInfo[0]?.register_number}
+
Subject Code : {barcodeInfo[0]?.subject_code}
+
Exam center code : {barcodeInfo[0]?.exam_centre_code}
+
Exam center : {barcodeInfo[0]?.exam_center}
+
+ )} +
+ + {scanResult ? (marksData.length > 0 && barcodeInfo.length > 0 ? ( + +
Marks : {marksData[0]?.marks}
+
File Scanned Date : {marksData[0]?.file_scanned_date}
+
Cover QR code : {marksData[0]?.cover_barcode}
+
+ ) : ( + +
Marks Data Not Found ..
+
+ )):null} +
+ {isLoading && } +
); };