@ayasofyazilim/ui 0.0.0
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/__mocks__/canvas.ts +8 -0
- package/components.json +21 -0
- package/eslint.config.js +4 -0
- package/jest-environment.js +37 -0
- package/jest.config.ts +47 -0
- package/jest.setup.ts +69 -0
- package/package.json +124 -0
- package/postcss.config.mjs +6 -0
- package/src/aria/index.tsx +1 -0
- package/src/aria/number-field.tsx +41 -0
- package/src/components/.gitkeep +0 -0
- package/src/components/accordion.tsx +66 -0
- package/src/components/alert-dialog.tsx +157 -0
- package/src/components/alert.tsx +70 -0
- package/src/components/aspect-ratio.tsx +11 -0
- package/src/components/avatar.tsx +53 -0
- package/src/components/badge.tsx +67 -0
- package/src/components/breadcrumb.tsx +109 -0
- package/src/components/button-group.tsx +83 -0
- package/src/components/button.tsx +68 -0
- package/src/components/calendar.tsx +219 -0
- package/src/components/card.tsx +92 -0
- package/src/components/carousel.tsx +241 -0
- package/src/components/chart.tsx +363 -0
- package/src/components/checkbox.tsx +32 -0
- package/src/components/collapsible.tsx +33 -0
- package/src/components/command.tsx +184 -0
- package/src/components/context-menu.tsx +252 -0
- package/src/components/dialog.tsx +144 -0
- package/src/components/drawer.tsx +135 -0
- package/src/components/dropdown-menu.tsx +258 -0
- package/src/components/empty.tsx +100 -0
- package/src/components/field.tsx +248 -0
- package/src/components/form.tsx +169 -0
- package/src/components/hover-card.tsx +44 -0
- package/src/components/input-group.tsx +170 -0
- package/src/components/input-otp.tsx +77 -0
- package/src/components/input.tsx +21 -0
- package/src/components/item.tsx +193 -0
- package/src/components/kbd.tsx +28 -0
- package/src/components/label.tsx +24 -0
- package/src/components/menubar.tsx +276 -0
- package/src/components/navigation-menu.tsx +168 -0
- package/src/components/pagination.tsx +130 -0
- package/src/components/popover.tsx +88 -0
- package/src/components/progress.tsx +31 -0
- package/src/components/radio-group.tsx +45 -0
- package/src/components/resizable.tsx +56 -0
- package/src/components/scroll-area.tsx +58 -0
- package/src/components/select.tsx +189 -0
- package/src/components/separator.tsx +28 -0
- package/src/components/sheet.tsx +140 -0
- package/src/components/sidebar.tsx +862 -0
- package/src/components/skeleton.tsx +13 -0
- package/src/components/slider.tsx +63 -0
- package/src/components/sonner.tsx +40 -0
- package/src/components/spinner.tsx +16 -0
- package/src/components/stepper.tsx +291 -0
- package/src/components/switch.tsx +31 -0
- package/src/components/table.tsx +133 -0
- package/src/components/tabs.tsx +66 -0
- package/src/components/textarea.tsx +18 -0
- package/src/components/toggle-group.tsx +83 -0
- package/src/components/toggle.tsx +47 -0
- package/src/components/tooltip.tsx +66 -0
- package/src/custom/action-button.tsx +48 -0
- package/src/custom/async-select.tsx +287 -0
- package/src/custom/awesome-not-found.tsx +116 -0
- package/src/custom/charts/area-chart.tsx +147 -0
- package/src/custom/charts/bar-chart.tsx +233 -0
- package/src/custom/charts/chart-card.tsx +103 -0
- package/src/custom/charts/index.tsx +16 -0
- package/src/custom/charts/pie-chart.tsx +168 -0
- package/src/custom/charts/radar-chart.tsx +126 -0
- package/src/custom/checkbox-tree.tsx +100 -0
- package/src/custom/combobox.tsx +296 -0
- package/src/custom/confirm-dialog.tsx +102 -0
- package/src/custom/country-selector.tsx +204 -0
- package/src/custom/date-picker/calendar-rac.tsx +109 -0
- package/src/custom/date-picker/datefield-rac.tsx +84 -0
- package/src/custom/date-picker/index.tsx +273 -0
- package/src/custom/date-picker/types/index.ts +4 -0
- package/src/custom/date-picker/utils/index.ts +42 -0
- package/src/custom/date-picker-old.tsx +50 -0
- package/src/custom/date-tooltip.tsx +98 -0
- package/src/custom/document-scanner/consts.ts +5 -0
- package/src/custom/document-scanner/corner-adjustment/action-buttons.tsx +33 -0
- package/src/custom/document-scanner/corner-adjustment/corner-handle.tsx +43 -0
- package/src/custom/document-scanner/corner-adjustment/hooks/use-corner-drag.ts +85 -0
- package/src/custom/document-scanner/corner-adjustment/index.tsx +125 -0
- package/src/custom/document-scanner/corner-adjustment/types.ts +53 -0
- package/src/custom/document-scanner/corner-adjustment/utils/clip-path.ts +22 -0
- package/src/custom/document-scanner/corner-adjustment/zoom-magnifier.tsx +115 -0
- package/src/custom/document-scanner/hooks/use-document-capture.ts +81 -0
- package/src/custom/document-scanner/hooks/use-document-scanner.ts +80 -0
- package/src/custom/document-scanner/hooks/use-perspective-crop.ts +38 -0
- package/src/custom/document-scanner/index.tsx +255 -0
- package/src/custom/document-scanner/lib.ts +407 -0
- package/src/custom/document-scanner/types.ts +205 -0
- package/src/custom/document-scanner/utils/perspective-correction.ts +139 -0
- package/src/custom/document-viewer/controllers.tsx +98 -0
- package/src/custom/document-viewer/index.tsx +43 -0
- package/src/custom/document-viewer/renderers/image.tsx +37 -0
- package/src/custom/document-viewer/renderers/index.tsx +2 -0
- package/src/custom/document-viewer/renderers/pdf.tsx +105 -0
- package/src/custom/email-input/domains.json +159 -0
- package/src/custom/email-input/email.tsx +229 -0
- package/src/custom/email-input/index.tsx +4 -0
- package/src/custom/email-input/types.ts +104 -0
- package/src/custom/file-uploader.tsx +541 -0
- package/src/custom/filter-component/fields/async-select.tsx +33 -0
- package/src/custom/filter-component/fields/date.tsx +60 -0
- package/src/custom/filter-component/fields/multi-select.tsx +30 -0
- package/src/custom/filter-component/index.tsx +217 -0
- package/src/custom/image-canvas.tsx +260 -0
- package/src/custom/json-editor.tsx +22 -0
- package/src/custom/master-data-grid/components/dialogs/column-settings-dialog.tsx +100 -0
- package/src/custom/master-data-grid/components/dialogs/index.ts +1 -0
- package/src/custom/master-data-grid/components/filters/client-filter.tsx +368 -0
- package/src/custom/master-data-grid/components/filters/filter-input.tsx +256 -0
- package/src/custom/master-data-grid/components/filters/index.ts +3 -0
- package/src/custom/master-data-grid/components/filters/inline-column-filter.tsx +233 -0
- package/src/custom/master-data-grid/components/filters/multi-filter-dialog.tsx +90 -0
- package/src/custom/master-data-grid/components/filters/server-filter.tsx +255 -0
- package/src/custom/master-data-grid/components/master-data-grid.tsx +472 -0
- package/src/custom/master-data-grid/components/pagination/index.ts +1 -0
- package/src/custom/master-data-grid/components/pagination/pagination.tsx +178 -0
- package/src/custom/master-data-grid/components/table/cell-renderer.tsx +634 -0
- package/src/custom/master-data-grid/components/table/header-cell.tsx +162 -0
- package/src/custom/master-data-grid/components/table/index.ts +4 -0
- package/src/custom/master-data-grid/components/table/table-body-renderer.tsx +113 -0
- package/src/custom/master-data-grid/components/table/virtual-body.tsx +138 -0
- package/src/custom/master-data-grid/components/toolbar/index.ts +1 -0
- package/src/custom/master-data-grid/components/toolbar/toolbar.tsx +314 -0
- package/src/custom/master-data-grid/hooks/index.ts +3 -0
- package/src/custom/master-data-grid/hooks/use-columns.tsx +332 -0
- package/src/custom/master-data-grid/hooks/use-editing.ts +106 -0
- package/src/custom/master-data-grid/hooks/use-table-state-reducer.ts +157 -0
- package/src/custom/master-data-grid/hooks/use-table-state.ts +31 -0
- package/src/custom/master-data-grid/index.ts +16 -0
- package/src/custom/master-data-grid/types.ts +466 -0
- package/src/custom/master-data-grid/utils/column-generator.tsx +306 -0
- package/src/custom/master-data-grid/utils/export-utils.ts +67 -0
- package/src/custom/master-data-grid/utils/filter-fns.ts +290 -0
- package/src/custom/master-data-grid/utils/index.ts +8 -0
- package/src/custom/master-data-grid/utils/pinning-utils.ts +88 -0
- package/src/custom/master-data-grid/utils/translation-utils.ts +42 -0
- package/src/custom/multi-select.tsx +432 -0
- package/src/custom/password-input.tsx +194 -0
- package/src/custom/phone-input.tsx +172 -0
- package/src/custom/schema-form/custom/index.tsx +1 -0
- package/src/custom/schema-form/custom/label.tsx +53 -0
- package/src/custom/schema-form/fields/base-input-field.tsx +82 -0
- package/src/custom/schema-form/fields/field.tsx +67 -0
- package/src/custom/schema-form/fields/index.tsx +5 -0
- package/src/custom/schema-form/fields/object.tsx +12 -0
- package/src/custom/schema-form/fields/table-array/array-field-item.tsx +90 -0
- package/src/custom/schema-form/fields/table-array/array-field-template.tsx +115 -0
- package/src/custom/schema-form/index.tsx +259 -0
- package/src/custom/schema-form/templates/description.tsx +20 -0
- package/src/custom/schema-form/templates/index.tsx +2 -0
- package/src/custom/schema-form/templates/submit.tsx +32 -0
- package/src/custom/schema-form/types.ts +64 -0
- package/src/custom/schema-form/utils/index.ts +4 -0
- package/src/custom/schema-form/utils/schema-dependency.ts +655 -0
- package/src/custom/schema-form/utils/schemas.ts +289 -0
- package/src/custom/schema-form/utils/validation.ts +23 -0
- package/src/custom/schema-form/widgets/boolean.tsx +77 -0
- package/src/custom/schema-form/widgets/combobox.tsx +274 -0
- package/src/custom/schema-form/widgets/date.tsx +59 -0
- package/src/custom/schema-form/widgets/email.tsx +34 -0
- package/src/custom/schema-form/widgets/index.tsx +10 -0
- package/src/custom/schema-form/widgets/password.tsx +40 -0
- package/src/custom/schema-form/widgets/phone.tsx +40 -0
- package/src/custom/schema-form/widgets/select.tsx +105 -0
- package/src/custom/schema-form/widgets/selectable.tsx +25 -0
- package/src/custom/schema-form/widgets/string-array.tsx +296 -0
- package/src/custom/schema-form/widgets/url.tsx +56 -0
- package/src/custom/section-layout-v2.tsx +212 -0
- package/src/custom/select-tabs.tsx +109 -0
- package/src/custom/selectable.tsx +316 -0
- package/src/custom/stepper.tsx +236 -0
- package/src/custom/tab-layout.tsx +213 -0
- package/src/custom/tanstack-table/fields/index.tsx +12 -0
- package/src/custom/tanstack-table/fields/tanstack-table-action-dialogs.tsx +89 -0
- package/src/custom/tanstack-table/fields/tanstack-table-column-header.tsx +66 -0
- package/src/custom/tanstack-table/fields/tanstack-table-filter-date.tsx +180 -0
- package/src/custom/tanstack-table/fields/tanstack-table-filter-faceted.tsx +158 -0
- package/src/custom/tanstack-table/fields/tanstack-table-filter-text.tsx +76 -0
- package/src/custom/tanstack-table/fields/tanstack-table-pagination.tsx +136 -0
- package/src/custom/tanstack-table/fields/tanstack-table-plain-table.tsx +142 -0
- package/src/custom/tanstack-table/fields/tanstack-table-row-actions-confirmation.tsx +77 -0
- package/src/custom/tanstack-table/fields/tanstack-table-row-actions-custom-dialog.tsx +87 -0
- package/src/custom/tanstack-table/fields/tanstack-table-row-actions.tsx +151 -0
- package/src/custom/tanstack-table/fields/tanstack-table-table-actions-custom-dialog.tsx +88 -0
- package/src/custom/tanstack-table/fields/tanstack-table-table-actions-schemaform-dialog.tsx +47 -0
- package/src/custom/tanstack-table/fields/tanstack-table-toolbar.tsx +143 -0
- package/src/custom/tanstack-table/fields/tanstack-table-view-options.tsx +171 -0
- package/src/custom/tanstack-table/index.tsx +244 -0
- package/src/custom/tanstack-table/types/index.ts +328 -0
- package/src/custom/tanstack-table/utils/cell-with-actions.tsx +21 -0
- package/src/custom/tanstack-table/utils/column-names.ts +26 -0
- package/src/custom/tanstack-table/utils/columns-by-row-data.tsx +312 -0
- package/src/custom/tanstack-table/utils/editable-columns-by-row-data.tsx +219 -0
- package/src/custom/tanstack-table/utils/faceted-boolean-options.tsx +22 -0
- package/src/custom/tanstack-table/utils/index.tsx +10 -0
- package/src/custom/tanstack-table/utils/pinning-styles.ts +57 -0
- package/src/custom/tanstack-table/utils/table.tsx +83 -0
- package/src/custom/tanstack-table/utils/test-conditions.ts +17 -0
- package/src/custom/timeline.tsx +208 -0
- package/src/custom/tree.tsx +200 -0
- package/src/custom/tscanify/browser.ts +66 -0
- package/src/custom/tscanify/index.ts +51 -0
- package/src/custom/tscanify/tscanify-browser.ts +522 -0
- package/src/custom/tscanify/tscanify.ts +262 -0
- package/src/custom/tscanify/types.ts +22 -0
- package/src/custom/webcam.tsx +737 -0
- package/src/hooks/.gitkeep +0 -0
- package/src/hooks/use-callback-ref.ts +27 -0
- package/src/hooks/use-controllable-state.ts +67 -0
- package/src/hooks/use-debounce.ts +19 -0
- package/src/hooks/use-is-visible.ts +23 -0
- package/src/hooks/use-media-query.ts +21 -0
- package/src/hooks/use-mobile.ts +21 -0
- package/src/hooks/use-on-window-resize.ts +15 -0
- package/src/hooks/use-scroll.tsx +22 -0
- package/src/lib/utils.ts +61 -0
- package/src/lib/zod.ts +2 -0
- package/src/styles/core.css +57 -0
- package/src/styles/globals.css +130 -0
- package/src/test/email-input.test.tsx +217 -0
- package/src/test/password-input.test.tsx +92 -0
- package/src/test/select-tabs.test.tsx +302 -0
- package/src/test/selectable.test.tsx +1093 -0
- package/tsconfig.json +13 -0
- package/tsconfig.lint.json +8 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/*! tscanify v1.0.0 | Based on jscanify v1.4.0 | (c) ColonelParrot and other contributors | MIT License */
|
|
2
|
+
|
|
3
|
+
import { Canvas, createCanvas, Image, ImageData } from "canvas";
|
|
4
|
+
import { JSDOM } from "jsdom";
|
|
5
|
+
import cv, { Mat } from "opencv-ts";
|
|
6
|
+
import { CornerPoints, HighlightOptions, Point } from "./types";
|
|
7
|
+
|
|
8
|
+
// Custom type to handle the difference between Node's Canvas and HTMLCanvasElement
|
|
9
|
+
type CanvasResult = Canvas | HTMLCanvasElement;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Installs DOM elements required for canvas operations
|
|
13
|
+
*/
|
|
14
|
+
function installDOM(): void {
|
|
15
|
+
const dom = new JSDOM();
|
|
16
|
+
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
+
(global as any).document = dom.window.document;
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
|
+
(global as any).Image = Image;
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
+
(global as any).HTMLCanvasElement = Canvas;
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
24
|
+
(global as any).ImageData = ImageData;
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
|
+
(global as any).HTMLImageElement = Image;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Calculates distance between two points.
|
|
31
|
+
* @param p1 point 1
|
|
32
|
+
* @param p2 point 2
|
|
33
|
+
* @returns distance between two points
|
|
34
|
+
*/
|
|
35
|
+
function distance(p1: Point, p2: Point): number {
|
|
36
|
+
return Math.hypot(p1.x - p2.x, p1.y - p2.y);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class TScanify {
|
|
40
|
+
constructor() {
|
|
41
|
+
installDOM();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Loads OpenCV module
|
|
46
|
+
* @param callback Function to call when OpenCV is initialized
|
|
47
|
+
*/
|
|
48
|
+
loadOpenCV(callback: (opencv: typeof cv) => void): void {
|
|
49
|
+
// opencv-ts uses onRuntimeInitialized
|
|
50
|
+
cv.onRuntimeInitialized = () => {
|
|
51
|
+
callback(cv);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Finds the contour of the paper within the image
|
|
57
|
+
* @param img image to process (Mat)
|
|
58
|
+
* @returns the biggest contour inside the image
|
|
59
|
+
*/
|
|
60
|
+
findPaperContour(img: Mat) {
|
|
61
|
+
const imgGray = new cv.Mat();
|
|
62
|
+
cv.Canny(img, imgGray, 50, 200);
|
|
63
|
+
|
|
64
|
+
const imgBlur = new cv.Mat();
|
|
65
|
+
cv.GaussianBlur(
|
|
66
|
+
imgGray,
|
|
67
|
+
imgBlur,
|
|
68
|
+
new cv.Size(3, 3),
|
|
69
|
+
0,
|
|
70
|
+
0,
|
|
71
|
+
cv.BORDER_DEFAULT
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
const imgThresh = new cv.Mat();
|
|
75
|
+
cv.threshold(imgBlur, imgThresh, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU);
|
|
76
|
+
|
|
77
|
+
const contours = new cv.MatVector();
|
|
78
|
+
const hierarchy = new cv.Mat();
|
|
79
|
+
|
|
80
|
+
cv.findContours(
|
|
81
|
+
imgThresh,
|
|
82
|
+
contours,
|
|
83
|
+
hierarchy,
|
|
84
|
+
cv.RETR_CCOMP,
|
|
85
|
+
cv.CHAIN_APPROX_SIMPLE
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
let maxArea = 0;
|
|
89
|
+
let maxContourIndex = -1;
|
|
90
|
+
for (let i = 0; i < contours.size(); ++i) {
|
|
91
|
+
const contourArea = cv.contourArea(contours.get(i));
|
|
92
|
+
if (contourArea > maxArea) {
|
|
93
|
+
maxArea = contourArea;
|
|
94
|
+
maxContourIndex = i;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const maxContour =
|
|
99
|
+
maxContourIndex >= 0 ? contours.get(maxContourIndex) : null;
|
|
100
|
+
|
|
101
|
+
imgGray.delete();
|
|
102
|
+
imgBlur.delete();
|
|
103
|
+
imgThresh.delete();
|
|
104
|
+
contours.delete();
|
|
105
|
+
hierarchy.delete();
|
|
106
|
+
return maxContour;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Highlights the paper detected inside the image.
|
|
111
|
+
* @param image image to process
|
|
112
|
+
* @param options options for highlighting. Accepts `color` and `thickness` parameter
|
|
113
|
+
* @returns Canvas with original image and paper highlighted
|
|
114
|
+
*/
|
|
115
|
+
highlightPaper(
|
|
116
|
+
image: HTMLImageElement,
|
|
117
|
+
options?: HighlightOptions
|
|
118
|
+
): CanvasResult {
|
|
119
|
+
options = options || {};
|
|
120
|
+
options.color = options.color || "orange";
|
|
121
|
+
options.thickness = options.thickness || 10;
|
|
122
|
+
|
|
123
|
+
// Get image dimensions (assuming image has width and height properties)
|
|
124
|
+
const width = image.width || 800;
|
|
125
|
+
const height = image.height || 600;
|
|
126
|
+
|
|
127
|
+
const canvas = createCanvas(width, height);
|
|
128
|
+
const ctx = canvas.getContext("2d");
|
|
129
|
+
const img = cv.imread(image);
|
|
130
|
+
|
|
131
|
+
const maxContour = this.findPaperContour(img);
|
|
132
|
+
// Convert the canvas to an HTML canvas id string for opencv-ts
|
|
133
|
+
const canvasId = "outputCanvas";
|
|
134
|
+
(canvas as unknown as HTMLCanvasElement).id = canvasId;
|
|
135
|
+
|
|
136
|
+
// Use the canvas id instead of the canvas object
|
|
137
|
+
cv.imshow(canvasId, img);
|
|
138
|
+
if (maxContour) {
|
|
139
|
+
const {
|
|
140
|
+
topLeftCorner,
|
|
141
|
+
topRightCorner,
|
|
142
|
+
bottomLeftCorner,
|
|
143
|
+
bottomRightCorner,
|
|
144
|
+
} = this.getCornerPoints(img);
|
|
145
|
+
|
|
146
|
+
ctx.strokeStyle = options.color!;
|
|
147
|
+
ctx.lineWidth = options.thickness!;
|
|
148
|
+
ctx.beginPath();
|
|
149
|
+
ctx.moveTo(topLeftCorner.x, topLeftCorner.y);
|
|
150
|
+
ctx.lineTo(topRightCorner.x, topRightCorner.y);
|
|
151
|
+
ctx.lineTo(bottomRightCorner.x, bottomRightCorner.y);
|
|
152
|
+
ctx.lineTo(bottomLeftCorner.x, bottomLeftCorner.y);
|
|
153
|
+
ctx.closePath();
|
|
154
|
+
ctx.stroke();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
img.delete();
|
|
158
|
+
return canvas;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Gets the corner points of a contour
|
|
163
|
+
* @param contour contour to find corners
|
|
164
|
+
* @param originalImage original image (for size reference)
|
|
165
|
+
* @returns object with corner points
|
|
166
|
+
*/
|
|
167
|
+
getCornerPoints(originalImage: Mat): CornerPoints {
|
|
168
|
+
// Implementation will depend on actual CV.js structure
|
|
169
|
+
// This is a placeholder that would need to be updated
|
|
170
|
+
return {
|
|
171
|
+
topLeftCorner: { x: 0, y: 0 },
|
|
172
|
+
topRightCorner: { x: originalImage.cols, y: 0 },
|
|
173
|
+
bottomLeftCorner: { x: 0, y: originalImage.rows },
|
|
174
|
+
bottomRightCorner: { x: originalImage.cols, y: originalImage.rows },
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Performs perspective transform to get a top-down view of the detected document
|
|
180
|
+
* @param image image containing document
|
|
181
|
+
* @returns Canvas with the document in a top-down perspective
|
|
182
|
+
*/
|
|
183
|
+
getPerspective(image: HTMLImageElement): CanvasResult {
|
|
184
|
+
// Get image dimensions (assuming image has width and height properties)
|
|
185
|
+
const width = image.width || 800;
|
|
186
|
+
const height = image.height || 600;
|
|
187
|
+
|
|
188
|
+
const canvas = createCanvas(width, height);
|
|
189
|
+
const img = cv.imread(image);
|
|
190
|
+
|
|
191
|
+
const maxContour = this.findPaperContour(img);
|
|
192
|
+
if (maxContour) {
|
|
193
|
+
const {
|
|
194
|
+
topLeftCorner,
|
|
195
|
+
topRightCorner,
|
|
196
|
+
bottomLeftCorner,
|
|
197
|
+
bottomRightCorner,
|
|
198
|
+
} = this.getCornerPoints(img);
|
|
199
|
+
|
|
200
|
+
// Calculate width and height for the transformed image
|
|
201
|
+
const width = Math.max(
|
|
202
|
+
distance(topLeftCorner, topRightCorner),
|
|
203
|
+
distance(bottomLeftCorner, bottomRightCorner)
|
|
204
|
+
);
|
|
205
|
+
const height = Math.max(
|
|
206
|
+
distance(topLeftCorner, bottomLeftCorner),
|
|
207
|
+
distance(topRightCorner, bottomRightCorner)
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
// Create source points matrix for perspective transform
|
|
211
|
+
const srcPoints = cv.matFromArray(4, 1, cv.CV_32FC2, [
|
|
212
|
+
topLeftCorner.x,
|
|
213
|
+
topLeftCorner.y,
|
|
214
|
+
topRightCorner.x,
|
|
215
|
+
topRightCorner.y,
|
|
216
|
+
bottomLeftCorner.x,
|
|
217
|
+
bottomLeftCorner.y,
|
|
218
|
+
bottomRightCorner.x,
|
|
219
|
+
bottomRightCorner.y,
|
|
220
|
+
]);
|
|
221
|
+
|
|
222
|
+
// Create destination points matrix for perspective transform
|
|
223
|
+
const dstPoints = cv.matFromArray(4, 1, cv.CV_32FC2, [
|
|
224
|
+
0,
|
|
225
|
+
0,
|
|
226
|
+
width,
|
|
227
|
+
0,
|
|
228
|
+
0,
|
|
229
|
+
height,
|
|
230
|
+
width,
|
|
231
|
+
height,
|
|
232
|
+
]);
|
|
233
|
+
|
|
234
|
+
// Perform perspective transform
|
|
235
|
+
const M = cv.getPerspectiveTransform(srcPoints, dstPoints);
|
|
236
|
+
|
|
237
|
+
// Clean up the temporary matrices when done
|
|
238
|
+
srcPoints.delete();
|
|
239
|
+
dstPoints.delete();
|
|
240
|
+
const dst = new cv.Mat();
|
|
241
|
+
|
|
242
|
+
cv.warpPerspective(img, dst, M, new cv.Size(width, height));
|
|
243
|
+
// Use the canvas id instead of the canvas object
|
|
244
|
+
const canvasId = "outputCanvas";
|
|
245
|
+
(canvas as unknown as HTMLCanvasElement).id = canvasId;
|
|
246
|
+
cv.imshow(canvasId, dst);
|
|
247
|
+
|
|
248
|
+
dst.delete();
|
|
249
|
+
M.delete();
|
|
250
|
+
} else {
|
|
251
|
+
// Use the canvas id instead of the canvas object
|
|
252
|
+
const canvasId = "outputCanvas";
|
|
253
|
+
(canvas as unknown as HTMLCanvasElement).id = canvasId;
|
|
254
|
+
cv.imshow(canvasId, img);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
img.delete();
|
|
258
|
+
return canvas;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export default TScanify;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Types for tscanify
|
|
2
|
+
|
|
3
|
+
import cv from "opencv-ts";
|
|
4
|
+
|
|
5
|
+
export interface Point {
|
|
6
|
+
x: number;
|
|
7
|
+
y: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface HighlightOptions {
|
|
11
|
+
color?: string;
|
|
12
|
+
thickness?: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface CornerPoints {
|
|
16
|
+
topLeftCorner: Point;
|
|
17
|
+
topRightCorner: Point;
|
|
18
|
+
bottomLeftCorner: Point;
|
|
19
|
+
bottomRightCorner: Point;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type WindowWithCV = Window & { cv: typeof cv };
|