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