react-native-rectangle-doc-scanner 10.28.0 → 10.30.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.
|
@@ -3,6 +3,7 @@ package com.reactnativerectangledocscanner
|
|
|
3
3
|
import android.graphics.Bitmap
|
|
4
4
|
import android.graphics.Rect
|
|
5
5
|
import android.util.Log
|
|
6
|
+
import com.reactnativerectangledocscanner.BuildConfig
|
|
6
7
|
import org.opencv.android.Utils
|
|
7
8
|
import org.opencv.core.*
|
|
8
9
|
import org.opencv.imgproc.Imgproc
|
|
@@ -36,6 +37,7 @@ enum class RectangleQuality {
|
|
|
36
37
|
class DocumentDetector {
|
|
37
38
|
companion object {
|
|
38
39
|
private const val TAG = "DocumentDetector"
|
|
40
|
+
private var debugFrameCounter = 0
|
|
39
41
|
|
|
40
42
|
init {
|
|
41
43
|
try {
|
|
@@ -161,6 +163,7 @@ class DocumentDetector {
|
|
|
161
163
|
val cannyMat = Mat()
|
|
162
164
|
val morphMat = Mat()
|
|
163
165
|
val threshMat = Mat()
|
|
166
|
+
val debugStats = DebugStats()
|
|
164
167
|
|
|
165
168
|
try {
|
|
166
169
|
// Convert to grayscale
|
|
@@ -258,6 +261,8 @@ class DocumentDetector {
|
|
|
258
261
|
var bestScore = 0.0
|
|
259
262
|
val minArea = max(450.0, (srcMat.rows() * srcMat.cols()) * 0.0007)
|
|
260
263
|
|
|
264
|
+
debugStats.contours = contours.size
|
|
265
|
+
|
|
261
266
|
for (contour in contours) {
|
|
262
267
|
val contourArea = Imgproc.contourArea(contour)
|
|
263
268
|
if (contourArea < minArea) continue
|
|
@@ -284,6 +289,7 @@ class DocumentDetector {
|
|
|
284
289
|
val rectArea = rect.size.area()
|
|
285
290
|
val rectangularity = if (rectArea > 1.0) contourArea / rectArea else 0.0
|
|
286
291
|
if (rectangularity >= 0.6) {
|
|
292
|
+
debugStats.candidates += 1
|
|
287
293
|
val score = contourArea * rectangularity
|
|
288
294
|
if (score > bestScore) {
|
|
289
295
|
bestScore = score
|
|
@@ -299,6 +305,7 @@ class DocumentDetector {
|
|
|
299
305
|
if (rectArea > 1.0) {
|
|
300
306
|
val rectangularity = contourArea / rectArea
|
|
301
307
|
if (rectangularity >= 0.6) {
|
|
308
|
+
debugStats.candidates += 1
|
|
302
309
|
val boxPoints = Array(4) { Point() }
|
|
303
310
|
rotated.points(boxPoints)
|
|
304
311
|
val score = contourArea * rectangularity
|
|
@@ -317,6 +324,7 @@ class DocumentDetector {
|
|
|
317
324
|
|
|
318
325
|
hierarchy.release()
|
|
319
326
|
contours.forEach { it.release() }
|
|
327
|
+
debugStats.bestScore = bestScore
|
|
320
328
|
return largestRectangle
|
|
321
329
|
}
|
|
322
330
|
|
|
@@ -340,6 +348,19 @@ class DocumentDetector {
|
|
|
340
348
|
rectangle = findLargestRectangle(morphMat)
|
|
341
349
|
}
|
|
342
350
|
|
|
351
|
+
if (BuildConfig.DEBUG) {
|
|
352
|
+
debugFrameCounter = (debugFrameCounter + 1) % 15
|
|
353
|
+
if (debugFrameCounter == 0) {
|
|
354
|
+
Log.d(
|
|
355
|
+
TAG,
|
|
356
|
+
"[DEBUG] cannyLow=$cannyLow cannyHigh=$cannyHigh " +
|
|
357
|
+
"contours=${debugStats.contours} candidates=${debugStats.candidates} " +
|
|
358
|
+
"bestScore=${String.format(\"%.1f\", debugStats.bestScore)} " +
|
|
359
|
+
"hasRect=${rectangle != null}"
|
|
360
|
+
)
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
343
364
|
return rectangle
|
|
344
365
|
} finally {
|
|
345
366
|
grayMat.release()
|
|
@@ -350,6 +371,12 @@ class DocumentDetector {
|
|
|
350
371
|
}
|
|
351
372
|
}
|
|
352
373
|
|
|
374
|
+
private data class DebugStats(
|
|
375
|
+
var contours: Int = 0,
|
|
376
|
+
var candidates: Int = 0,
|
|
377
|
+
var bestScore: Double = 0.0
|
|
378
|
+
)
|
|
379
|
+
|
|
353
380
|
/**
|
|
354
381
|
* Order points in consistent order: topLeft, topRight, bottomLeft, bottomRight
|
|
355
382
|
*/
|
|
@@ -410,7 +437,7 @@ class DocumentDetector {
|
|
|
410
437
|
}
|
|
411
438
|
|
|
412
439
|
val minDim = kotlin.math.min(viewWidth.toDouble(), viewHeight.toDouble())
|
|
413
|
-
val angleThreshold = max(
|
|
440
|
+
val angleThreshold = max(90.0, minDim * 0.12)
|
|
414
441
|
|
|
415
442
|
val topYDiff = abs(rectangle.topRight.y - rectangle.topLeft.y)
|
|
416
443
|
val bottomYDiff = abs(rectangle.bottomLeft.y - rectangle.bottomRight.y)
|
|
@@ -421,7 +448,7 @@ class DocumentDetector {
|
|
|
421
448
|
return RectangleQuality.BAD_ANGLE
|
|
422
449
|
}
|
|
423
450
|
|
|
424
|
-
val margin = max(
|
|
451
|
+
val margin = max(80.0, minDim * 0.08)
|
|
425
452
|
if (rectangle.topLeft.y > margin ||
|
|
426
453
|
rectangle.topRight.y > margin ||
|
|
427
454
|
rectangle.bottomLeft.y < (viewHeight - margin) ||
|
package/package.json
CHANGED