react-native-rectangle-doc-scanner 1.11.0 → 1.13.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.
@@ -226,15 +226,23 @@ class RNRDocScannerView: UIView, AVCaptureVideoDataOutputSampleBufferDelegate, A
226
226
  let request = VNDetectRectanglesRequest(completionHandler: requestHandler)
227
227
 
228
228
  request.maximumObservations = 3
229
- request.minimumConfidence = 0.55
230
- request.minimumAspectRatio = 0.1
231
- request.maximumAspectRatio = 2.0
229
+ request.minimumConfidence = 0.65
230
+ request.minimumAspectRatio = 0.12
231
+ request.maximumAspectRatio = 1.9
232
232
  request.minimumSize = 0.05
233
233
  if #available(iOS 13.0, *) {
234
- request.quadratureTolerance = 20
234
+ request.quadratureTolerance = 18
235
235
  }
236
236
 
237
- let handler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, orientation: orientation, options: [:])
237
+ var processedImage = CIImage(cvPixelBuffer: pixelBuffer)
238
+ processedImage = processedImage.applyingFilter("CIColorControls", parameters: [
239
+ kCIInputContrastKey: 1.35,
240
+ kCIInputBrightnessKey: 0.02,
241
+ kCIInputSaturationKey: 1.05,
242
+ ])
243
+ processedImage = processedImage.applyingFilter("CISharpenLuminance", parameters: [kCIInputSharpnessKey: 0.5])
244
+
245
+ let handler = VNImageRequestHandler(ciImage: processedImage, orientation: orientation, options: [:])
238
246
  do {
239
247
  try handler.perform([request])
240
248
  } catch {
@@ -282,12 +290,12 @@ class RNRDocScannerView: UIView, AVCaptureVideoDataOutputSampleBufferDelegate, A
282
290
  updateNativeOverlay(with: shouldDisplayOverlay ? observation : nil)
283
291
 
284
292
  payload = [
285
- "rectangleCoordinates": [
293
+ "rectangleCoordinates": shouldDisplayOverlay ? [
286
294
  "topLeft": ["x": points[0].x, "y": points[0].y],
287
295
  "topRight": ["x": points[1].x, "y": points[1].y],
288
296
  "bottomRight": ["x": points[2].x, "y": points[2].y],
289
297
  "bottomLeft": ["x": points[3].x, "y": points[3].y],
290
- ],
298
+ ] : NSNull(),
291
299
  "stableCounter": currentStableCounter,
292
300
  "frameWidth": frameSize.width,
293
301
  "frameHeight": frameSize.height,
@@ -339,7 +347,7 @@ class RNRDocScannerView: UIView, AVCaptureVideoDataOutputSampleBufferDelegate, A
339
347
  let points: [CGPoint]
340
348
  if let previous = self.smoothedOverlayPoints, previous.count == 4 {
341
349
  points = zip(previous, orderedPoints).map { prev, next in
342
- CGPoint(x: prev.x * 0.85 + next.x * 0.15, y: prev.y * 0.85 + next.y * 0.15)
350
+ CGPoint(x: prev.x * 0.7 + next.x * 0.3, y: prev.y * 0.7 + next.y * 0.3)
343
351
  }
344
352
  } else {
345
353
  points = orderedPoints
@@ -389,17 +397,51 @@ class RNRDocScannerView: UIView, AVCaptureVideoDataOutputSampleBufferDelegate, A
389
397
  private func orderPoints(_ points: [CGPoint]) -> [CGPoint] {
390
398
  guard points.count == 4 else { return points }
391
399
 
392
- let sortedByY = points.sorted { a, b in
393
- if a.y == b.y {
394
- return a.x < b.x
400
+ var topLeft = points[0]
401
+ var topRight = points[0]
402
+ var bottomRight = points[0]
403
+ var bottomLeft = points[0]
404
+
405
+ var minSum = CGFloat.greatestFiniteMagnitude
406
+ var maxSum = -CGFloat.greatestFiniteMagnitude
407
+ var minDiff = CGFloat.greatestFiniteMagnitude
408
+ var maxDiff = -CGFloat.greatestFiniteMagnitude
409
+
410
+ for point in points {
411
+ let sum = point.x + point.y
412
+ if sum < minSum {
413
+ minSum = sum
414
+ topLeft = point
415
+ }
416
+ if sum > maxSum {
417
+ maxSum = sum
418
+ bottomRight = point
419
+ }
420
+
421
+ let diff = point.x - point.y
422
+ if diff < minDiff {
423
+ minDiff = diff
424
+ bottomLeft = point
425
+ }
426
+ if diff > maxDiff {
427
+ maxDiff = diff
428
+ topRight = point
395
429
  }
396
- return a.y < b.y
397
430
  }
398
431
 
399
- let top = sortedByY.prefix(2).sorted { $0.x < $1.x }
400
- let bottom = sortedByY.suffix(2).sorted { $0.x < $1.x }
432
+ var ordered = [topLeft, topRight, bottomRight, bottomLeft]
433
+ if cross(ordered[0], ordered[1], ordered[2]) < 0 {
434
+ ordered = [topLeft, bottomLeft, bottomRight, topRight]
435
+ }
436
+ return ordered
437
+ }
401
438
 
402
- return [top[0], top[1], bottom[1], bottom[0]]
439
+ private func cross(_ a: CGPoint, _ b: CGPoint, _ c: CGPoint) -> CGFloat {
440
+ let abx = b.x - a.x
441
+ let aby = b.y - a.y
442
+ let acx = c.x - a.x
443
+ let acy = c.y - a.y
444
+ return abx * acy - aby * acx
403
445
  }
404
446
 
405
447
  // MARK: - Capture
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-rectangle-doc-scanner",
3
- "version": "1.11.0",
3
+ "version": "1.13.0",
4
4
  "description": "Native-backed document scanner for React Native with customizable overlays.",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",