rn-opencv-doc-perspective-correction 1.0.6 → 1.0.8
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/dist/index.d.ts +6 -2
- package/dist/index.js +31 -12
- package/package.json +1 -1
- package/src/index.ts +32 -8
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,10 @@ export type Point = {
|
|
|
5
5
|
export declare class DocumentScanner {
|
|
6
6
|
private static getDistance;
|
|
7
7
|
private static sortCorners;
|
|
8
|
-
static detectPageCorners(imageBase64: string
|
|
9
|
-
static applyPerspectiveCorrection(imageBase64: string, corners: Point[]
|
|
8
|
+
static detectPageCorners(imageBase64: string): Point[] | undefined;
|
|
9
|
+
static applyPerspectiveCorrection(imageBase64: string, corners: Point[]): string | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Xoay ảnh 90, -90 hoặc 180 độ
|
|
12
|
+
*/
|
|
13
|
+
static rotateImage(imageBase64: string, angle: 90 | -90 | 180): string | undefined;
|
|
10
14
|
}
|
package/dist/index.js
CHANGED
|
@@ -17,7 +17,7 @@ class DocumentScanner {
|
|
|
17
17
|
return angleA - angleB;
|
|
18
18
|
});
|
|
19
19
|
}
|
|
20
|
-
static detectPageCorners(imageBase64
|
|
20
|
+
static detectPageCorners(imageBase64) {
|
|
21
21
|
let src = null;
|
|
22
22
|
let gray = null;
|
|
23
23
|
let blurred = null;
|
|
@@ -45,8 +45,6 @@ class DocumentScanner {
|
|
|
45
45
|
const contoursArray = (contoursJS === null || contoursJS === void 0 ? void 0 : contoursJS.array) || [];
|
|
46
46
|
const contoursSize = contoursArray.length;
|
|
47
47
|
if (contoursSize === 0) {
|
|
48
|
-
if (onLog)
|
|
49
|
-
onLog(`[OpenCV] Không tìm thấy contours.`);
|
|
50
48
|
return undefined;
|
|
51
49
|
}
|
|
52
50
|
// First pass: extract all areas to sort them and minimize JSI calls
|
|
@@ -76,10 +74,6 @@ class DocumentScanner {
|
|
|
76
74
|
break; // Stop at the first 4-point polygon like python script
|
|
77
75
|
}
|
|
78
76
|
}
|
|
79
|
-
const logMsg = `[OpenCV] Contours: ${contoursSize}. Metrics pass: ${contourMetrics.length}. Poly detect: ${largestPoly ? 'Thành công' : 'Thất bại'}.`;
|
|
80
|
-
console.log(logMsg);
|
|
81
|
-
if (onLog)
|
|
82
|
-
onLog(logMsg);
|
|
83
77
|
if (largestPoly && largestPoly.length === 4) {
|
|
84
78
|
return this.sortCorners(largestPoly);
|
|
85
79
|
}
|
|
@@ -87,15 +81,13 @@ class DocumentScanner {
|
|
|
87
81
|
}
|
|
88
82
|
catch (e) {
|
|
89
83
|
console.error('Lỗi khi dò tìm góc tài liệu (OpenCV):', e);
|
|
90
|
-
if (onLog)
|
|
91
|
-
onLog(`[OpenCV Corner Detection Error]: ${e.message}`);
|
|
92
84
|
throw new Error(`[OpenCV Corner Detection Error]: ${e.message}`);
|
|
93
85
|
}
|
|
94
86
|
finally {
|
|
95
87
|
react_native_fast_opencv_1.OpenCV.clearBuffers();
|
|
96
88
|
}
|
|
97
89
|
}
|
|
98
|
-
static applyPerspectiveCorrection(imageBase64, corners
|
|
90
|
+
static applyPerspectiveCorrection(imageBase64, corners) {
|
|
99
91
|
let src = null;
|
|
100
92
|
let dst = null;
|
|
101
93
|
try {
|
|
@@ -138,8 +130,35 @@ class DocumentScanner {
|
|
|
138
130
|
}
|
|
139
131
|
catch (e) {
|
|
140
132
|
console.error('Lỗi khi bóp phối cảnh tài liệu (OpenCV):', e);
|
|
141
|
-
|
|
142
|
-
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
finally {
|
|
136
|
+
react_native_fast_opencv_1.OpenCV.clearBuffers();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Xoay ảnh 90, -90 hoặc 180 độ
|
|
141
|
+
*/
|
|
142
|
+
static rotateImage(imageBase64, angle) {
|
|
143
|
+
let src = null;
|
|
144
|
+
let dst = null;
|
|
145
|
+
try {
|
|
146
|
+
src = react_native_fast_opencv_1.OpenCV.base64ToMat(imageBase64);
|
|
147
|
+
dst = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Mat, 0, 0, react_native_fast_opencv_1.DataTypes.CV_8U);
|
|
148
|
+
let rotateCode = 0; // ROTATE_90_CLOCKWISE
|
|
149
|
+
if (angle === -90)
|
|
150
|
+
rotateCode = 2; // ROTATE_90_COUNTERCLOCKWISE
|
|
151
|
+
else if (angle === 180)
|
|
152
|
+
rotateCode = 1; // ROTATE_180
|
|
153
|
+
react_native_fast_opencv_1.OpenCV.invoke('rotate', src, dst, rotateCode);
|
|
154
|
+
const dstValue = react_native_fast_opencv_1.OpenCV.toJSValue(dst);
|
|
155
|
+
if (dstValue && dstValue.base64) {
|
|
156
|
+
return typeof dstValue.base64 === 'string' ? dstValue.base64 : String(dstValue.base64);
|
|
157
|
+
}
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
catch (e) {
|
|
161
|
+
console.error('Lỗi khi xoay ảnh (OpenCV):', e);
|
|
143
162
|
return undefined;
|
|
144
163
|
}
|
|
145
164
|
finally {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rn-opencv-doc-perspective-correction",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "A React Native library for document corner detection and perspective correction using react-native-fast-opencv",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
package/src/index.ts
CHANGED
|
@@ -23,7 +23,7 @@ export class DocumentScanner {
|
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
public static detectPageCorners(imageBase64: string
|
|
26
|
+
public static detectPageCorners(imageBase64: string): Point[] | undefined {
|
|
27
27
|
let src: OpenCVMat | null = null;
|
|
28
28
|
let gray: OpenCVMat | null = null;
|
|
29
29
|
let blurred: OpenCVMat | null = null;
|
|
@@ -59,7 +59,6 @@ export class DocumentScanner {
|
|
|
59
59
|
const contoursSize = contoursArray.length;
|
|
60
60
|
|
|
61
61
|
if (contoursSize === 0) {
|
|
62
|
-
if (onLog) onLog(`[OpenCV] Không tìm thấy contours.`);
|
|
63
62
|
return undefined;
|
|
64
63
|
}
|
|
65
64
|
|
|
@@ -96,9 +95,7 @@ export class DocumentScanner {
|
|
|
96
95
|
}
|
|
97
96
|
}
|
|
98
97
|
|
|
99
|
-
|
|
100
|
-
console.log(logMsg);
|
|
101
|
-
if (onLog) onLog(logMsg);
|
|
98
|
+
|
|
102
99
|
|
|
103
100
|
if (largestPoly && largestPoly.length === 4) {
|
|
104
101
|
return this.sortCorners(largestPoly);
|
|
@@ -106,14 +103,13 @@ export class DocumentScanner {
|
|
|
106
103
|
return undefined;
|
|
107
104
|
} catch (e: any) {
|
|
108
105
|
console.error('Lỗi khi dò tìm góc tài liệu (OpenCV):', e);
|
|
109
|
-
if (onLog) onLog(`[OpenCV Corner Detection Error]: ${e.message}`);
|
|
110
106
|
throw new Error(`[OpenCV Corner Detection Error]: ${e.message}`);
|
|
111
107
|
} finally {
|
|
112
108
|
OpenCV.clearBuffers();
|
|
113
109
|
}
|
|
114
110
|
}
|
|
115
111
|
|
|
116
|
-
public static applyPerspectiveCorrection(imageBase64: string, corners: Point[]
|
|
112
|
+
public static applyPerspectiveCorrection(imageBase64: string, corners: Point[]): string | undefined {
|
|
117
113
|
let src: OpenCVMat | null = null;
|
|
118
114
|
let dst: OpenCVMat | null = null;
|
|
119
115
|
|
|
@@ -169,7 +165,35 @@ export class DocumentScanner {
|
|
|
169
165
|
return undefined;
|
|
170
166
|
} catch (e) {
|
|
171
167
|
console.error('Lỗi khi bóp phối cảnh tài liệu (OpenCV):', e);
|
|
172
|
-
|
|
168
|
+
return undefined;
|
|
169
|
+
} finally {
|
|
170
|
+
OpenCV.clearBuffers();
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Xoay ảnh 90, -90 hoặc 180 độ
|
|
176
|
+
*/
|
|
177
|
+
public static rotateImage(imageBase64: string, angle: 90 | -90 | 180): string | undefined {
|
|
178
|
+
let src: OpenCVMat | null = null;
|
|
179
|
+
let dst: OpenCVMat | null = null;
|
|
180
|
+
try {
|
|
181
|
+
src = OpenCV.base64ToMat(imageBase64);
|
|
182
|
+
dst = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8U);
|
|
183
|
+
|
|
184
|
+
let rotateCode = 0; // ROTATE_90_CLOCKWISE
|
|
185
|
+
if (angle === -90) rotateCode = 2; // ROTATE_90_COUNTERCLOCKWISE
|
|
186
|
+
else if (angle === 180) rotateCode = 1; // ROTATE_180
|
|
187
|
+
|
|
188
|
+
OpenCV.invoke('rotate', src, dst, rotateCode);
|
|
189
|
+
|
|
190
|
+
const dstValue = OpenCV.toJSValue(dst);
|
|
191
|
+
if (dstValue && dstValue.base64) {
|
|
192
|
+
return typeof dstValue.base64 === 'string' ? dstValue.base64 : String(dstValue.base64);
|
|
193
|
+
}
|
|
194
|
+
return undefined;
|
|
195
|
+
} catch (e: any) {
|
|
196
|
+
console.error('Lỗi khi xoay ảnh (OpenCV):', e);
|
|
173
197
|
return undefined;
|
|
174
198
|
} finally {
|
|
175
199
|
OpenCV.clearBuffers();
|