blob: 0cb43ace8f0c161b07d20da214dc1e03522ebccb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
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<string> => {
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<HTMLInputElement | null>(null);
const handleSuratJalanCapture = async (
event: React.ChangeEvent<HTMLInputElement>
) => {
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 (
<div className="p-4 py-8 items-center border-2 rounded-md shadow-sm w-[49%] text-center">
<input
ref={fileRef}
id="suratJalanInput"
type="file"
accept="image/*"
multiple
capture="environment"
className="hidden"
onChange={handleSuratJalanCapture}
/>
<label
htmlFor="suratJalanInput"
className="text-gray-600 cursor-pointer select-none"
>
<IconButton color="primary" component="span">
<PendingActions fontSize="large" />
</IconButton>
<br />
Foto Surat Jalan
</label>
</div>
);
};
export default SjCamera;
|