@olenbetong/synergi-react 4.1.0 → 4.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/components/PdfViewer/PdfViewer.js +22 -3
- package/package.json +7 -7
- package/src/chrome/AppShell/index.tsx +29 -29
- package/src/chrome/openReport.ts +34 -34
- package/src/colorScheme.ts +81 -81
- package/src/components/DateNavigator/index.tsx +186 -186
- package/src/components/FilePond.tsx +312 -312
- package/src/components/ImageEditor/ImageEditor.tsx +343 -343
- package/src/components/ImageEditor/index.ts +9 -9
- package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +165 -165
- package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +48 -48
- package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +403 -403
- package/src/components/ImageEditor/plugins/redactPlugin/RedactToolbar.tsx +47 -47
- package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +23 -23
- package/src/components/ImageEditor/plugins/zoomPlugin/ZoomControls.tsx +40 -40
- package/src/components/ImageEditor/useEdgeDetection.ts +127 -127
- package/src/components/ImageEditor/useImageHistory.ts +197 -197
- package/src/components/ImageEditor/useOpenCV.ts +184 -184
- package/src/components/LinkedCardList/index.scss +95 -95
- package/src/components/MergeProgressDialog/index.tsx +249 -249
- package/src/components/PageBanner/index.tsx +80 -80
- package/src/components/PdfViewer/PdfViewer.tsx +158 -139
- package/src/components/ProgressBar/index.scss +96 -96
- package/src/components/SplitContainer/index.tsx +333 -333
- package/src/components/Toolbar/index.tsx +212 -212
- package/src/designsystemet-web-types.d.ts +24 -24
- package/src/elements/ElementDamage/DamageFormButtons.tsx +35 -35
- package/src/elements/ElementDamage/DescriptionInput.tsx +120 -120
- package/src/elements/ElementDamage/DuplicateDamage/DesktopView.tsx +82 -82
- package/src/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.tsx +67 -67
- package/src/elements/ElementDamage/DuplicateDamage/PhoneView.tsx +45 -45
- package/src/elements/ElementDamage/ElementNumberTextField.tsx +41 -41
- package/src/elements/ElementDamage/RegisterFix.tsx +100 -100
- package/src/elements/ElementDamage/SubmitButtons.tsx +40 -40
- package/src/elements/ElementDamage/TransportElementSelect.tsx +52 -52
- package/src/elements/ElementLookup.tsx +123 -123
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoder.ts +32 -32
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/dbr.ts +118 -118
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/native.ts +103 -103
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +95 -95
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders.test.ts +118 -118
- package/src/elements/ElementLookupDialog/BarcodeScanner/duplicateFilter.ts +21 -21
- package/src/elements/ElementLookupDialog/BarcodeScanner/index.tsx +90 -90
- package/src/elements/ElementLookupDialog/BarcodeScanner/useScanner.ts +280 -280
- package/src/elements/ElementLookupDialog/HIDScanner/index.tsx +67 -67
- package/src/elements/ElementLookupDialog/Manual/ManualForm.tsx +141 -141
- package/src/elements/ElementLookupDialog/index.tsx +226 -226
- package/src/elements/ElementLookupDialog/utils/Giai.ts +209 -209
- package/src/elements/ProjectSelect.tsx +130 -130
- package/src/elements/index.ts +15 -15
- package/src/filter/index.ts +34 -34
- package/src/i18n.ts +13 -13
- package/src/index.ts +77 -77
- package/src/mui/MuiThemeProvider.tsx +174 -174
- package/src/mui/index.ts +1 -1
- package/src/test/setup.ts +8 -8
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
import { Button, Spinner } from "@digdir/designsystemet-react";
|
|
2
|
-
import { CheckmarkIcon, EraserIcon, XMarkIcon } from "@navikt/aksel-icons";
|
|
3
|
-
import { useState } from "react";
|
|
4
|
-
|
|
5
|
-
import { useRedactContext } from "./RedactContext.js";
|
|
6
|
-
import { getLocalizedString } from "../../../../i18n.js";
|
|
7
|
-
|
|
8
|
-
/** Toolbar shown while the redact tool is active. Reads state from RedactContext. */
|
|
9
|
-
export function RedactToolbar() {
|
|
10
|
-
let { rects, clearAll, applyRedact, onApply, onCancel } = useRedactContext();
|
|
11
|
-
let [applying, setApplying] = useState(false);
|
|
12
|
-
|
|
13
|
-
async function handleApply() {
|
|
14
|
-
if (rects.length === 0) {
|
|
15
|
-
onCancel();
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
setApplying(true);
|
|
19
|
-
try {
|
|
20
|
-
let result = await applyRedact();
|
|
21
|
-
if (result) onApply(result.blob, result.actions);
|
|
22
|
-
} finally {
|
|
23
|
-
setApplying(false);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return (
|
|
28
|
-
<>
|
|
29
|
-
<div className="image-editor__toolbar-group">
|
|
30
|
-
<Button variant="tertiary" data-size="sm" onClick={onCancel} disabled={applying}>
|
|
31
|
-
<XMarkIcon aria-hidden />
|
|
32
|
-
{getLocalizedString("Cancel")}
|
|
33
|
-
</Button>
|
|
34
|
-
<Button variant="tertiary" data-size="sm" onClick={clearAll} disabled={applying || rects.length === 0}>
|
|
35
|
-
<EraserIcon aria-hidden />
|
|
36
|
-
{getLocalizedString("Clear all")}
|
|
37
|
-
</Button>
|
|
38
|
-
</div>
|
|
39
|
-
<div className="image-editor__toolbar-group">
|
|
40
|
-
<Button variant="primary" data-size="sm" onClick={handleApply} disabled={applying}>
|
|
41
|
-
{applying ? <Spinner aria-hidden data-size="xs" /> : <CheckmarkIcon aria-hidden />}
|
|
42
|
-
{rects.length === 0 ? getLocalizedString("Done") : getLocalizedString("Apply redact")}
|
|
43
|
-
</Button>
|
|
44
|
-
</div>
|
|
45
|
-
</>
|
|
46
|
-
);
|
|
47
|
-
}
|
|
1
|
+
import { Button, Spinner } from "@digdir/designsystemet-react";
|
|
2
|
+
import { CheckmarkIcon, EraserIcon, XMarkIcon } from "@navikt/aksel-icons";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
|
|
5
|
+
import { useRedactContext } from "./RedactContext.js";
|
|
6
|
+
import { getLocalizedString } from "../../../../i18n.js";
|
|
7
|
+
|
|
8
|
+
/** Toolbar shown while the redact tool is active. Reads state from RedactContext. */
|
|
9
|
+
export function RedactToolbar() {
|
|
10
|
+
let { rects, clearAll, applyRedact, onApply, onCancel } = useRedactContext();
|
|
11
|
+
let [applying, setApplying] = useState(false);
|
|
12
|
+
|
|
13
|
+
async function handleApply() {
|
|
14
|
+
if (rects.length === 0) {
|
|
15
|
+
onCancel();
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
setApplying(true);
|
|
19
|
+
try {
|
|
20
|
+
let result = await applyRedact();
|
|
21
|
+
if (result) onApply(result.blob, result.actions);
|
|
22
|
+
} finally {
|
|
23
|
+
setApplying(false);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return (
|
|
28
|
+
<>
|
|
29
|
+
<div className="image-editor__toolbar-group">
|
|
30
|
+
<Button variant="tertiary" data-size="sm" onClick={onCancel} disabled={applying}>
|
|
31
|
+
<XMarkIcon aria-hidden />
|
|
32
|
+
{getLocalizedString("Cancel")}
|
|
33
|
+
</Button>
|
|
34
|
+
<Button variant="tertiary" data-size="sm" onClick={clearAll} disabled={applying || rects.length === 0}>
|
|
35
|
+
<EraserIcon aria-hidden />
|
|
36
|
+
{getLocalizedString("Clear all")}
|
|
37
|
+
</Button>
|
|
38
|
+
</div>
|
|
39
|
+
<div className="image-editor__toolbar-group">
|
|
40
|
+
<Button variant="primary" data-size="sm" onClick={handleApply} disabled={applying}>
|
|
41
|
+
{applying ? <Spinner aria-hidden data-size="xs" /> : <CheckmarkIcon aria-hidden />}
|
|
42
|
+
{rects.length === 0 ? getLocalizedString("Done") : getLocalizedString("Apply redact")}
|
|
43
|
+
</Button>
|
|
44
|
+
</div>
|
|
45
|
+
</>
|
|
46
|
+
);
|
|
47
|
+
}
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import { createContext, type ReactNode, use } from "react";
|
|
2
|
-
import { useZoomPan, type ZoomPanResult } from "./useZoomPan.js";
|
|
3
|
-
|
|
4
|
-
let ZoomContext = createContext<ZoomPanResult | null>(null);
|
|
5
|
-
|
|
6
|
-
export function useZoom(): ZoomPanResult {
|
|
7
|
-
let ctx = use(ZoomContext);
|
|
8
|
-
if (!ctx) throw new Error("useZoom must be used within ZoomProvider");
|
|
9
|
-
return ctx;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Current zoom factor, or 1 when no ZoomProvider is present.
|
|
14
|
-
* Lets tools convert screen-space coordinates to CSS pixels without requiring the zoom plugin.
|
|
15
|
-
*/
|
|
16
|
-
export function useZoomLevel(): number {
|
|
17
|
-
return use(ZoomContext)?.zoom ?? 1;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function ZoomProvider({ children }: { children: ReactNode }) {
|
|
21
|
-
let state = useZoomPan();
|
|
22
|
-
return <ZoomContext value={state}>{children}</ZoomContext>;
|
|
23
|
-
}
|
|
1
|
+
import { createContext, type ReactNode, use } from "react";
|
|
2
|
+
import { useZoomPan, type ZoomPanResult } from "./useZoomPan.js";
|
|
3
|
+
|
|
4
|
+
let ZoomContext = createContext<ZoomPanResult | null>(null);
|
|
5
|
+
|
|
6
|
+
export function useZoom(): ZoomPanResult {
|
|
7
|
+
let ctx = use(ZoomContext);
|
|
8
|
+
if (!ctx) throw new Error("useZoom must be used within ZoomProvider");
|
|
9
|
+
return ctx;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Current zoom factor, or 1 when no ZoomProvider is present.
|
|
14
|
+
* Lets tools convert screen-space coordinates to CSS pixels without requiring the zoom plugin.
|
|
15
|
+
*/
|
|
16
|
+
export function useZoomLevel(): number {
|
|
17
|
+
return use(ZoomContext)?.zoom ?? 1;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function ZoomProvider({ children }: { children: ReactNode }) {
|
|
21
|
+
let state = useZoomPan();
|
|
22
|
+
return <ZoomContext value={state}>{children}</ZoomContext>;
|
|
23
|
+
}
|
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
import { ShrinkIcon, ZoomMinusIcon, ZoomPlusIcon } from "@navikt/aksel-icons";
|
|
2
|
-
import { ResponsiveButton } from "../../ResponsiveButton.js";
|
|
3
|
-
import { useZoom } from "./ZoomContext.js";
|
|
4
|
-
import { getLocalizedString } from "../../../../i18n.js";
|
|
5
|
-
|
|
6
|
-
interface Props {
|
|
7
|
-
disabled?: boolean;
|
|
8
|
-
isSmall?: boolean;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function ZoomControls({ disabled, isSmall = false }: Props) {
|
|
12
|
-
let { zoom, zoomIn, zoomOut, resetZoom, canZoomIn, canZoomOut, isZoomed } = useZoom();
|
|
13
|
-
|
|
14
|
-
return (
|
|
15
|
-
<>
|
|
16
|
-
<ResponsiveButton
|
|
17
|
-
icon={<ZoomMinusIcon aria-hidden />}
|
|
18
|
-
label={getLocalizedString("Zoom out")}
|
|
19
|
-
onClick={zoomOut}
|
|
20
|
-
disabled={disabled || !canZoomOut}
|
|
21
|
-
isSmall={isSmall}
|
|
22
|
-
/>
|
|
23
|
-
<span className="image-editor__zoom-level">{Math.round(zoom * 100)}%</span>
|
|
24
|
-
<ResponsiveButton
|
|
25
|
-
icon={<ZoomPlusIcon aria-hidden />}
|
|
26
|
-
label={getLocalizedString("Zoom in")}
|
|
27
|
-
onClick={zoomIn}
|
|
28
|
-
disabled={disabled || !canZoomIn}
|
|
29
|
-
isSmall={isSmall}
|
|
30
|
-
/>
|
|
31
|
-
<ResponsiveButton
|
|
32
|
-
disabled={!isZoomed}
|
|
33
|
-
icon={<ShrinkIcon aria-hidden />}
|
|
34
|
-
label={getLocalizedString("Reset zoom")}
|
|
35
|
-
onClick={resetZoom}
|
|
36
|
-
isSmall={isSmall}
|
|
37
|
-
/>
|
|
38
|
-
</>
|
|
39
|
-
);
|
|
40
|
-
}
|
|
1
|
+
import { ShrinkIcon, ZoomMinusIcon, ZoomPlusIcon } from "@navikt/aksel-icons";
|
|
2
|
+
import { ResponsiveButton } from "../../ResponsiveButton.js";
|
|
3
|
+
import { useZoom } from "./ZoomContext.js";
|
|
4
|
+
import { getLocalizedString } from "../../../../i18n.js";
|
|
5
|
+
|
|
6
|
+
interface Props {
|
|
7
|
+
disabled?: boolean;
|
|
8
|
+
isSmall?: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function ZoomControls({ disabled, isSmall = false }: Props) {
|
|
12
|
+
let { zoom, zoomIn, zoomOut, resetZoom, canZoomIn, canZoomOut, isZoomed } = useZoom();
|
|
13
|
+
|
|
14
|
+
return (
|
|
15
|
+
<>
|
|
16
|
+
<ResponsiveButton
|
|
17
|
+
icon={<ZoomMinusIcon aria-hidden />}
|
|
18
|
+
label={getLocalizedString("Zoom out")}
|
|
19
|
+
onClick={zoomOut}
|
|
20
|
+
disabled={disabled || !canZoomOut}
|
|
21
|
+
isSmall={isSmall}
|
|
22
|
+
/>
|
|
23
|
+
<span className="image-editor__zoom-level">{Math.round(zoom * 100)}%</span>
|
|
24
|
+
<ResponsiveButton
|
|
25
|
+
icon={<ZoomPlusIcon aria-hidden />}
|
|
26
|
+
label={getLocalizedString("Zoom in")}
|
|
27
|
+
onClick={zoomIn}
|
|
28
|
+
disabled={disabled || !canZoomIn}
|
|
29
|
+
isSmall={isSmall}
|
|
30
|
+
/>
|
|
31
|
+
<ResponsiveButton
|
|
32
|
+
disabled={!isZoomed}
|
|
33
|
+
icon={<ShrinkIcon aria-hidden />}
|
|
34
|
+
label={getLocalizedString("Reset zoom")}
|
|
35
|
+
onClick={resetZoom}
|
|
36
|
+
isSmall={isSmall}
|
|
37
|
+
/>
|
|
38
|
+
</>
|
|
39
|
+
);
|
|
40
|
+
}
|
|
@@ -1,127 +1,127 @@
|
|
|
1
|
-
import { useEffect, useState } from "react";
|
|
2
|
-
|
|
3
|
-
export interface Point {
|
|
4
|
-
x: number;
|
|
5
|
-
y: number;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
/** TL, TR, BR, BL */
|
|
9
|
-
export type Quad = [Point, Point, Point, Point];
|
|
10
|
-
|
|
11
|
-
export function fullImageQuad(width: number, height: number): Quad {
|
|
12
|
-
return [
|
|
13
|
-
{ x: 0, y: 0 },
|
|
14
|
-
{ x: width, y: 0 },
|
|
15
|
-
{ x: width, y: height },
|
|
16
|
-
{ x: 0, y: height },
|
|
17
|
-
];
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function findLargestQuad(cv: any, src: any, minAreaFraction = 0.1): Quad | null {
|
|
21
|
-
let gray = new cv.Mat();
|
|
22
|
-
let blurred = new cv.Mat();
|
|
23
|
-
let edges = new cv.Mat();
|
|
24
|
-
let dilated = new cv.Mat();
|
|
25
|
-
let contours = new cv.MatVector();
|
|
26
|
-
let hierarchy = new cv.Mat();
|
|
27
|
-
|
|
28
|
-
try {
|
|
29
|
-
cv.cvtColor(src, gray, cv.COLOR_RGBA2GRAY);
|
|
30
|
-
cv.GaussianBlur(gray, blurred, new cv.Size(5, 5), 0);
|
|
31
|
-
// Lower thresholds catch document-background boundaries even in low-contrast images
|
|
32
|
-
cv.Canny(blurred, edges, 40, 120);
|
|
33
|
-
|
|
34
|
-
// Dilate to close small gaps along document edges
|
|
35
|
-
let kernel = cv.getStructuringElement(cv.MORPH_RECT, new cv.Size(3, 3));
|
|
36
|
-
cv.dilate(edges, dilated, kernel);
|
|
37
|
-
kernel.delete();
|
|
38
|
-
|
|
39
|
-
cv.findContours(dilated, contours, hierarchy, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE);
|
|
40
|
-
|
|
41
|
-
let best: unknown = null;
|
|
42
|
-
let bestArea = 0;
|
|
43
|
-
let minArea = src.cols * src.rows * minAreaFraction;
|
|
44
|
-
|
|
45
|
-
for (let i = 0; i < contours.size(); i++) {
|
|
46
|
-
let contour = contours.get(i);
|
|
47
|
-
let peri = cv.arcLength(contour, true);
|
|
48
|
-
let approx = new cv.Mat();
|
|
49
|
-
// Larger epsilon is more aggressive — helps when the outline isn't perfectly straight
|
|
50
|
-
cv.approxPolyDP(contour, approx, 0.04 * peri, true);
|
|
51
|
-
|
|
52
|
-
if (approx.rows === 4) {
|
|
53
|
-
let area = cv.contourArea(approx);
|
|
54
|
-
if (area > bestArea && area > minArea) {
|
|
55
|
-
bestArea = area;
|
|
56
|
-
(best as any)?.delete();
|
|
57
|
-
best = approx;
|
|
58
|
-
} else {
|
|
59
|
-
approx.delete();
|
|
60
|
-
}
|
|
61
|
-
} else {
|
|
62
|
-
approx.delete();
|
|
63
|
-
}
|
|
64
|
-
contour.delete();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
let quad: Quad | null = null;
|
|
68
|
-
if (best) {
|
|
69
|
-
let mat = best as any;
|
|
70
|
-
let pts: Point[] = [];
|
|
71
|
-
for (let i = 0; i < 4; i++) {
|
|
72
|
-
pts.push({ x: mat.data32S[i * 2], y: mat.data32S[i * 2 + 1] });
|
|
73
|
-
}
|
|
74
|
-
mat.delete();
|
|
75
|
-
|
|
76
|
-
// Sort into TL, TR, BR, BL
|
|
77
|
-
pts.sort((a, b) => a.x + a.y - (b.x + b.y));
|
|
78
|
-
let tl = pts[0];
|
|
79
|
-
let br = pts[3];
|
|
80
|
-
let remaining = [pts[1], pts[2]];
|
|
81
|
-
let tr = remaining[0].x > remaining[1].x ? remaining[0] : remaining[1];
|
|
82
|
-
let bl = remaining[0].x < remaining[1].x ? remaining[0] : remaining[1];
|
|
83
|
-
quad = [tl, tr, br, bl];
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return quad;
|
|
87
|
-
} finally {
|
|
88
|
-
gray.delete();
|
|
89
|
-
blurred.delete();
|
|
90
|
-
edges.delete();
|
|
91
|
-
dilated.delete();
|
|
92
|
-
contours.delete();
|
|
93
|
-
hierarchy.delete();
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
interface UseEdgeDetectionResult {
|
|
98
|
-
corners: Quad | null;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Runs Canny edge detection on the image to suggest document corners.
|
|
103
|
-
* Falls back to full-image corners if no quadrilateral is found.
|
|
104
|
-
*/
|
|
105
|
-
export function useEdgeDetection(cv: any, canvas: HTMLCanvasElement | null, enabled: boolean): UseEdgeDetectionResult {
|
|
106
|
-
let [corners, setCorners] = useState<Quad | null>(null);
|
|
107
|
-
|
|
108
|
-
useEffect(() => {
|
|
109
|
-
if (!enabled || !cv || !canvas) return;
|
|
110
|
-
|
|
111
|
-
try {
|
|
112
|
-
let src = cv.imread(canvas);
|
|
113
|
-
let quad: Quad | null;
|
|
114
|
-
try {
|
|
115
|
-
quad = findLargestQuad(cv, src);
|
|
116
|
-
} finally {
|
|
117
|
-
src.delete();
|
|
118
|
-
}
|
|
119
|
-
setCorners(quad ?? fullImageQuad(canvas.width, canvas.height));
|
|
120
|
-
} catch (err) {
|
|
121
|
-
console.warn("[ImageEditor] Edge detection failed, falling back to the full image:", err);
|
|
122
|
-
setCorners(fullImageQuad(canvas.width, canvas.height));
|
|
123
|
-
}
|
|
124
|
-
}, [cv, canvas, enabled]);
|
|
125
|
-
|
|
126
|
-
return { corners };
|
|
127
|
-
}
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
export interface Point {
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** TL, TR, BR, BL */
|
|
9
|
+
export type Quad = [Point, Point, Point, Point];
|
|
10
|
+
|
|
11
|
+
export function fullImageQuad(width: number, height: number): Quad {
|
|
12
|
+
return [
|
|
13
|
+
{ x: 0, y: 0 },
|
|
14
|
+
{ x: width, y: 0 },
|
|
15
|
+
{ x: width, y: height },
|
|
16
|
+
{ x: 0, y: height },
|
|
17
|
+
];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function findLargestQuad(cv: any, src: any, minAreaFraction = 0.1): Quad | null {
|
|
21
|
+
let gray = new cv.Mat();
|
|
22
|
+
let blurred = new cv.Mat();
|
|
23
|
+
let edges = new cv.Mat();
|
|
24
|
+
let dilated = new cv.Mat();
|
|
25
|
+
let contours = new cv.MatVector();
|
|
26
|
+
let hierarchy = new cv.Mat();
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
cv.cvtColor(src, gray, cv.COLOR_RGBA2GRAY);
|
|
30
|
+
cv.GaussianBlur(gray, blurred, new cv.Size(5, 5), 0);
|
|
31
|
+
// Lower thresholds catch document-background boundaries even in low-contrast images
|
|
32
|
+
cv.Canny(blurred, edges, 40, 120);
|
|
33
|
+
|
|
34
|
+
// Dilate to close small gaps along document edges
|
|
35
|
+
let kernel = cv.getStructuringElement(cv.MORPH_RECT, new cv.Size(3, 3));
|
|
36
|
+
cv.dilate(edges, dilated, kernel);
|
|
37
|
+
kernel.delete();
|
|
38
|
+
|
|
39
|
+
cv.findContours(dilated, contours, hierarchy, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE);
|
|
40
|
+
|
|
41
|
+
let best: unknown = null;
|
|
42
|
+
let bestArea = 0;
|
|
43
|
+
let minArea = src.cols * src.rows * minAreaFraction;
|
|
44
|
+
|
|
45
|
+
for (let i = 0; i < contours.size(); i++) {
|
|
46
|
+
let contour = contours.get(i);
|
|
47
|
+
let peri = cv.arcLength(contour, true);
|
|
48
|
+
let approx = new cv.Mat();
|
|
49
|
+
// Larger epsilon is more aggressive — helps when the outline isn't perfectly straight
|
|
50
|
+
cv.approxPolyDP(contour, approx, 0.04 * peri, true);
|
|
51
|
+
|
|
52
|
+
if (approx.rows === 4) {
|
|
53
|
+
let area = cv.contourArea(approx);
|
|
54
|
+
if (area > bestArea && area > minArea) {
|
|
55
|
+
bestArea = area;
|
|
56
|
+
(best as any)?.delete();
|
|
57
|
+
best = approx;
|
|
58
|
+
} else {
|
|
59
|
+
approx.delete();
|
|
60
|
+
}
|
|
61
|
+
} else {
|
|
62
|
+
approx.delete();
|
|
63
|
+
}
|
|
64
|
+
contour.delete();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let quad: Quad | null = null;
|
|
68
|
+
if (best) {
|
|
69
|
+
let mat = best as any;
|
|
70
|
+
let pts: Point[] = [];
|
|
71
|
+
for (let i = 0; i < 4; i++) {
|
|
72
|
+
pts.push({ x: mat.data32S[i * 2], y: mat.data32S[i * 2 + 1] });
|
|
73
|
+
}
|
|
74
|
+
mat.delete();
|
|
75
|
+
|
|
76
|
+
// Sort into TL, TR, BR, BL
|
|
77
|
+
pts.sort((a, b) => a.x + a.y - (b.x + b.y));
|
|
78
|
+
let tl = pts[0];
|
|
79
|
+
let br = pts[3];
|
|
80
|
+
let remaining = [pts[1], pts[2]];
|
|
81
|
+
let tr = remaining[0].x > remaining[1].x ? remaining[0] : remaining[1];
|
|
82
|
+
let bl = remaining[0].x < remaining[1].x ? remaining[0] : remaining[1];
|
|
83
|
+
quad = [tl, tr, br, bl];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return quad;
|
|
87
|
+
} finally {
|
|
88
|
+
gray.delete();
|
|
89
|
+
blurred.delete();
|
|
90
|
+
edges.delete();
|
|
91
|
+
dilated.delete();
|
|
92
|
+
contours.delete();
|
|
93
|
+
hierarchy.delete();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
interface UseEdgeDetectionResult {
|
|
98
|
+
corners: Quad | null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Runs Canny edge detection on the image to suggest document corners.
|
|
103
|
+
* Falls back to full-image corners if no quadrilateral is found.
|
|
104
|
+
*/
|
|
105
|
+
export function useEdgeDetection(cv: any, canvas: HTMLCanvasElement | null, enabled: boolean): UseEdgeDetectionResult {
|
|
106
|
+
let [corners, setCorners] = useState<Quad | null>(null);
|
|
107
|
+
|
|
108
|
+
useEffect(() => {
|
|
109
|
+
if (!enabled || !cv || !canvas) return;
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
let src = cv.imread(canvas);
|
|
113
|
+
let quad: Quad | null;
|
|
114
|
+
try {
|
|
115
|
+
quad = findLargestQuad(cv, src);
|
|
116
|
+
} finally {
|
|
117
|
+
src.delete();
|
|
118
|
+
}
|
|
119
|
+
setCorners(quad ?? fullImageQuad(canvas.width, canvas.height));
|
|
120
|
+
} catch (err) {
|
|
121
|
+
console.warn("[ImageEditor] Edge detection failed, falling back to the full image:", err);
|
|
122
|
+
setCorners(fullImageQuad(canvas.width, canvas.height));
|
|
123
|
+
}
|
|
124
|
+
}, [cv, canvas, enabled]);
|
|
125
|
+
|
|
126
|
+
return { corners };
|
|
127
|
+
}
|