import React, { useRef } from "react"; import useCameraStore from "../hooks/useCameraStore"; import { IconButton } from "@mui/material"; import { PendingActions } from "@mui/icons-material"; const compressImage = ( file: File, maxWidth = 1600, quality = 0.6 ): Promise => { return new Promise((resolve) => { const img = new Image(); const reader = new FileReader(); reader.onload = () => { img.src = reader.result as string; }; img.onload = () => { const scale = Math.min(1, maxWidth / img.width); const canvas = document.createElement("canvas"); canvas.width = img.width * scale; canvas.height = img.height * scale; const ctx = canvas.getContext("2d")!; ctx.drawImage(img, 0, 0, canvas.width, canvas.height); resolve(canvas.toDataURL("image/jpeg", quality)); }; reader.readAsDataURL(file); }); }; const SjCamera: React.FC = () => { const { setImageSj } = useCameraStore(); const fileRef = useRef(null); const handleSuratJalanCapture = async ( event: React.ChangeEvent ) => { const imgs = await Promise.all( Array.from(event.target.files ?? []).map((file) => compressImage(file, 1600, 0.6) ) ); imgs.forEach((img) => setImageSj(img)); if (fileRef.current) fileRef.current.value = ""; }; return (
); }; export default SjCamera;