From c02762ae157d167ae27bf34f6014abcb49deecc1 Mon Sep 17 00:00:00 2001 From: Mqdd Date: Mon, 15 Dec 2025 09:58:17 +0700 Subject: scanner using zxing --- app/lib/camera/component/scannerBarcode.tsx | 95 ++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 30 deletions(-) (limited to 'app/lib/camera') diff --git a/app/lib/camera/component/scannerBarcode.tsx b/app/lib/camera/component/scannerBarcode.tsx index 6319a84..18f953d 100644 --- a/app/lib/camera/component/scannerBarcode.tsx +++ b/app/lib/camera/component/scannerBarcode.tsx @@ -1,52 +1,87 @@ import { QrCode2 } from "@mui/icons-material"; import { Button, TextField } from "@mui/material"; -import dynamic from "next/dynamic"; -import React, { useState } from "react"; +import { BrowserMultiFormatReader } from "@zxing/browser"; +import React, { useEffect, useRef, useState } from "react"; import useCameraStore from "../hooks/useCameraStore"; -const BarcodeScannerComponent = dynamic( - () => import("react-qr-barcode-scanner"), - { ssr: false } -); const BarcodeScanner: React.FC = () => { const { barcode, setBarcode } = useCameraStore(); const [isCameraActive, setIsCameraActive] = useState(false); + const videoRef = useRef(null); + const inputRef = useRef(null); + + const stopCamera = () => { + const video = videoRef.current; + if (!video || !video.srcObject) return; + + const stream = video.srcObject as MediaStream; + stream.getTracks().forEach((track) => track.stop()); + video.srcObject = null; + }; + + useEffect(() => { + if (!isCameraActive || !videoRef.current) return; + + const reader = new BrowserMultiFormatReader(); + let cancelled = false; + + reader + .decodeOnceFromVideoDevice(undefined, videoRef.current) + .then((result) => { + if (cancelled) return; + + setBarcode(result.getText()); + setIsCameraActive(false); + stopCamera(); + + // 🔥 fokus ke input biar bisa diedit + setTimeout(() => { + inputRef.current?.focus(); + }, 100); + }) + .catch(() => { + // ignore cancel / permission stop + }); + + return () => { + cancelled = true; + stopCamera(); + }; + }, [isCameraActive, setBarcode]); + return (
- + {isCameraActive && ( - { - if (result) { - setBarcode(result.getText()); - setIsCameraActive(false); - } - }} - /> + <> +

+ Arahkan barcode ke tengah layar +

+
-- cgit v1.2.3