@onekeyfe/react-native-qr-scanner 6.8.0-alpha.540 → 6.8.0-alpha.545
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/android/src/main/java/com/margelo/nitro/qrscanner/QrCameraZoom.kt +15 -0
- package/android/src/main/java/com/margelo/nitro/qrscanner/QrScanner.kt +93 -2
- package/android/src/test/java/com/margelo/nitro/qrscanner/QrCameraZoomTest.kt +24 -0
- package/ios/QrCameraControl.swift +57 -0
- package/ios/QrScanner.swift +136 -12
- package/package.json +1 -1
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
package com.margelo.nitro.qrscanner
|
|
2
|
+
|
|
3
|
+
/** Pinch zoom rules for the live scanner, kept pure for unit tests. */
|
|
4
|
+
object QrCameraZoom {
|
|
5
|
+
/**
|
|
6
|
+
* Zoom never goes past this ratio: beyond it the digital crop is too soft
|
|
7
|
+
* for dense codes. CameraX ratio 1.0 is the default (wide) lens.
|
|
8
|
+
*/
|
|
9
|
+
const val MAX_RATIO = 8f
|
|
10
|
+
|
|
11
|
+
fun clampRatio(requested: Float, minRatio: Float, maxRatio: Float): Float {
|
|
12
|
+
val upper = maxOf(minRatio, minOf(maxRatio, MAX_RATIO))
|
|
13
|
+
return requested.coerceIn(minRatio, upper)
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -2,14 +2,22 @@ package com.margelo.nitro.qrscanner
|
|
|
2
2
|
|
|
3
3
|
import android.Manifest
|
|
4
4
|
import android.content.pm.PackageManager
|
|
5
|
+
import android.annotation.SuppressLint
|
|
5
6
|
import android.graphics.Color
|
|
7
|
+
import android.graphics.drawable.GradientDrawable
|
|
6
8
|
import android.os.Handler
|
|
7
9
|
import android.os.Looper
|
|
8
10
|
import android.os.SystemClock
|
|
9
11
|
import android.util.Size
|
|
12
|
+
import android.view.GestureDetector
|
|
13
|
+
import android.view.MotionEvent
|
|
14
|
+
import android.view.ScaleGestureDetector
|
|
10
15
|
import android.view.View
|
|
16
|
+
import android.widget.FrameLayout
|
|
11
17
|
import androidx.annotation.OptIn
|
|
18
|
+
import androidx.camera.core.Camera
|
|
12
19
|
import androidx.camera.core.CameraSelector
|
|
20
|
+
import androidx.camera.core.FocusMeteringAction
|
|
13
21
|
import androidx.camera.core.ExperimentalGetImage
|
|
14
22
|
import androidx.camera.core.ImageAnalysis
|
|
15
23
|
import androidx.camera.core.ImageProxy
|
|
@@ -31,6 +39,7 @@ import com.google.mlkit.vision.barcode.BarcodeScanning
|
|
|
31
39
|
import com.google.mlkit.vision.barcode.common.Barcode
|
|
32
40
|
import com.google.mlkit.vision.common.InputImage
|
|
33
41
|
import java.util.concurrent.Executors
|
|
42
|
+
import java.util.concurrent.TimeUnit
|
|
34
43
|
|
|
35
44
|
/**
|
|
36
45
|
* CameraX preview plus ML Kit QR detection. Native code reports raw text only;
|
|
@@ -40,6 +49,10 @@ import java.util.concurrent.Executors
|
|
|
40
49
|
class HybridQrScanner(val context: ThemedReactContext) : HybridQrScannerSpec(), LifecycleOwner {
|
|
41
50
|
private companion object {
|
|
42
51
|
const val PERMISSION_REQUEST_CODE = 0x5152
|
|
52
|
+
const val FOCUS_INDICATOR_DP = 72
|
|
53
|
+
// A tapped focus point is held this long, then CameraX returns to
|
|
54
|
+
// continuous focus over the whole frame.
|
|
55
|
+
const val FOCUS_HOLD_SECONDS = 5L
|
|
43
56
|
|
|
44
57
|
// Shared and never shut down: ML Kit completion listeners may still be
|
|
45
58
|
// queued on it after a view is dropped.
|
|
@@ -53,8 +66,25 @@ class HybridQrScanner(val context: ThemedReactContext) : HybridQrScannerSpec(),
|
|
|
53
66
|
scaleType = PreviewView.ScaleType.FILL_CENTER
|
|
54
67
|
setBackgroundColor(Color.BLACK)
|
|
55
68
|
}
|
|
69
|
+
private val density = context.resources.displayMetrics.density
|
|
70
|
+
private val focusIndicator = View(context).apply {
|
|
71
|
+
val size = (FOCUS_INDICATOR_DP * density).toInt()
|
|
72
|
+
layoutParams = FrameLayout.LayoutParams(size, size)
|
|
73
|
+
background = GradientDrawable().apply {
|
|
74
|
+
setStroke((1.5f * density).toInt().coerceAtLeast(1), Color.WHITE)
|
|
75
|
+
cornerRadius = 8 * density
|
|
76
|
+
}
|
|
77
|
+
alpha = 0f
|
|
78
|
+
isClickable = false
|
|
79
|
+
}
|
|
80
|
+
private val container = FrameLayout(context).apply {
|
|
81
|
+
addView(previewView, FrameLayout.LayoutParams(
|
|
82
|
+
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT,
|
|
83
|
+
))
|
|
84
|
+
addView(focusIndicator)
|
|
85
|
+
}
|
|
56
86
|
override val view: View
|
|
57
|
-
get() =
|
|
87
|
+
get() = container
|
|
58
88
|
|
|
59
89
|
// Props
|
|
60
90
|
override var active: Boolean = false
|
|
@@ -74,6 +104,9 @@ class HybridQrScanner(val context: ThemedReactContext) : HybridQrScannerSpec(),
|
|
|
74
104
|
BarcodeScannerOptions.Builder().setBarcodeFormats(Barcode.FORMAT_QR_CODE).build(),
|
|
75
105
|
)
|
|
76
106
|
private var cameraProvider: ProcessCameraProvider? = null
|
|
107
|
+
/** The bound camera while running. Main thread only. */
|
|
108
|
+
private var camera: Camera? = null
|
|
109
|
+
private var pinchRatio = 1f
|
|
77
110
|
private var running = false
|
|
78
111
|
private var hostResumed = true
|
|
79
112
|
private var dropped = false
|
|
@@ -90,6 +123,63 @@ class HybridQrScanner(val context: ThemedReactContext) : HybridQrScannerSpec(),
|
|
|
90
123
|
lifecycleRegistry.currentState = Lifecycle.State.CREATED
|
|
91
124
|
(context.currentActivity as? LifecycleOwner)?.lifecycle?.addObserver(hostObserver)
|
|
92
125
|
}
|
|
126
|
+
installGestures()
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
@SuppressLint("ClickableViewAccessibility")
|
|
130
|
+
private fun installGestures() {
|
|
131
|
+
val scaleDetector = ScaleGestureDetector(context, object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
|
|
132
|
+
override fun onScaleBegin(detector: ScaleGestureDetector): Boolean {
|
|
133
|
+
val zoom = camera?.cameraInfo?.zoomState?.value ?: return false
|
|
134
|
+
pinchRatio = zoom.zoomRatio
|
|
135
|
+
return true
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
override fun onScale(detector: ScaleGestureDetector): Boolean {
|
|
139
|
+
val camera = camera ?: return false
|
|
140
|
+
val zoom = camera.cameraInfo.zoomState.value ?: return false
|
|
141
|
+
pinchRatio *= detector.scaleFactor
|
|
142
|
+
pinchRatio = QrCameraZoom.clampRatio(pinchRatio, zoom.minZoomRatio, zoom.maxZoomRatio)
|
|
143
|
+
camera.cameraControl.setZoomRatio(pinchRatio)
|
|
144
|
+
return true
|
|
145
|
+
}
|
|
146
|
+
})
|
|
147
|
+
val tapDetector = GestureDetector(context, object : GestureDetector.SimpleOnGestureListener() {
|
|
148
|
+
override fun onDown(event: MotionEvent): Boolean = true
|
|
149
|
+
|
|
150
|
+
override fun onSingleTapUp(event: MotionEvent): Boolean {
|
|
151
|
+
focusAt(event.x, event.y)
|
|
152
|
+
return true
|
|
153
|
+
}
|
|
154
|
+
})
|
|
155
|
+
container.setOnTouchListener { _, event ->
|
|
156
|
+
scaleDetector.onTouchEvent(event)
|
|
157
|
+
if (!scaleDetector.isInProgress) tapDetector.onTouchEvent(event)
|
|
158
|
+
true
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private fun focusAt(x: Float, y: Float) {
|
|
163
|
+
val camera = camera ?: return
|
|
164
|
+
showFocusIndicator(x, y)
|
|
165
|
+
val point = previewView.meteringPointFactory.createPoint(x, y)
|
|
166
|
+
val action = FocusMeteringAction.Builder(point, FocusMeteringAction.FLAG_AF or FocusMeteringAction.FLAG_AE)
|
|
167
|
+
.setAutoCancelDuration(FOCUS_HOLD_SECONDS, TimeUnit.SECONDS)
|
|
168
|
+
.build()
|
|
169
|
+
// Rejected when the camera cannot meter that point; scanning goes on.
|
|
170
|
+
camera.cameraControl.startFocusAndMetering(action)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private fun showFocusIndicator(x: Float, y: Float) {
|
|
174
|
+
focusIndicator.animate().cancel()
|
|
175
|
+
focusIndicator.x = x - focusIndicator.width / 2f
|
|
176
|
+
focusIndicator.y = y - focusIndicator.height / 2f
|
|
177
|
+
focusIndicator.alpha = 1f
|
|
178
|
+
focusIndicator.scaleX = 1.25f
|
|
179
|
+
focusIndicator.scaleY = 1.25f
|
|
180
|
+
focusIndicator.animate().scaleX(1f).scaleY(1f).setDuration(200).withEndAction {
|
|
181
|
+
focusIndicator.animate().alpha(0f).setStartDelay(800).setDuration(300)
|
|
182
|
+
}
|
|
93
183
|
}
|
|
94
184
|
|
|
95
185
|
override fun afterUpdate() {
|
|
@@ -184,7 +274,7 @@ class HybridQrScanner(val context: ThemedReactContext) : HybridQrScannerSpec(),
|
|
|
184
274
|
analysis.setAnalyzer(analysisExecutor, ::analyze)
|
|
185
275
|
try {
|
|
186
276
|
provider.unbindAll()
|
|
187
|
-
provider.bindToLifecycle(this, selector, preview, analysis)
|
|
277
|
+
camera = provider.bindToLifecycle(this, selector, preview, analysis)
|
|
188
278
|
} catch (error: Exception) {
|
|
189
279
|
emitError("camera-busy", error.message ?: "The camera could not start", generation)
|
|
190
280
|
return
|
|
@@ -201,6 +291,7 @@ class HybridQrScanner(val context: ThemedReactContext) : HybridQrScannerSpec(),
|
|
|
201
291
|
gate.setCapturing(false)
|
|
202
292
|
if (!running) return
|
|
203
293
|
cameraProvider?.unbindAll()
|
|
294
|
+
camera = null
|
|
204
295
|
running = false
|
|
205
296
|
syncLifecycle()
|
|
206
297
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
package com.margelo.nitro.qrscanner
|
|
2
|
+
|
|
3
|
+
import org.junit.Assert.assertEquals
|
|
4
|
+
import org.junit.Test
|
|
5
|
+
|
|
6
|
+
class QrCameraZoomTest {
|
|
7
|
+
@Test
|
|
8
|
+
fun capsZoomAtEightTimes() {
|
|
9
|
+
assertEquals(8f, QrCameraZoom.clampRatio(30f, 1f, 10f), 0f)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
@Test
|
|
13
|
+
fun keepsTheDeviceRange() {
|
|
14
|
+
// Logical cameras with an ultra-wide lens allow ratios below 1.
|
|
15
|
+
assertEquals(0.6f, QrCameraZoom.clampRatio(0.2f, 0.6f, 10f), 0f)
|
|
16
|
+
assertEquals(4f, QrCameraZoom.clampRatio(6f, 1f, 4f), 0f)
|
|
17
|
+
assertEquals(2.5f, QrCameraZoom.clampRatio(2.5f, 1f, 10f), 0f)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
@Test
|
|
21
|
+
fun aFixedFocalLengthCameraStaysAtOne() {
|
|
22
|
+
assertEquals(1f, QrCameraZoom.clampRatio(3f, 1f, 1f), 0f)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import AVFoundation
|
|
2
|
+
import CoreGraphics
|
|
3
|
+
|
|
4
|
+
/// Camera selection and zoom rules for the live scanner. Pure functions over
|
|
5
|
+
/// device facts, so they are unit-tested without camera hardware.
|
|
6
|
+
enum QrCameraControl {
|
|
7
|
+
/// Zoom never goes past this multiple of the 1x (wide-angle) factor:
|
|
8
|
+
/// beyond it the digital crop is too soft for dense codes.
|
|
9
|
+
static let maxZoomMultiple: CGFloat = 8
|
|
10
|
+
|
|
11
|
+
/// Back-camera device types in order of preference. Virtual devices come
|
|
12
|
+
/// first: they switch to the ultra-wide lens by themselves when a code is
|
|
13
|
+
/// held closer than the wide lens can focus (iPhone 13 Pro and later).
|
|
14
|
+
static let preferredBackDeviceTypes: [AVCaptureDevice.DeviceType] = [
|
|
15
|
+
.builtInTripleCamera,
|
|
16
|
+
.builtInDualWideCamera,
|
|
17
|
+
.builtInDualCamera,
|
|
18
|
+
.builtInWideAngleCamera,
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
/// Zoom factor at which a virtual device shows its wide-angle lens, which is
|
|
22
|
+
/// what users know as 1x. `switchOverFactors[i]` is where constituent i+1
|
|
23
|
+
/// takes over, so the wide lens at index n starts at `switchOverFactors[n-1]`.
|
|
24
|
+
static func wideAngleZoomFactor(
|
|
25
|
+
constituentTypes: [AVCaptureDevice.DeviceType], switchOverFactors: [CGFloat]
|
|
26
|
+
) -> CGFloat {
|
|
27
|
+
guard let wideIndex = constituentTypes.firstIndex(of: .builtInWideAngleCamera),
|
|
28
|
+
wideIndex >= 1, wideIndex - 1 < switchOverFactors.count
|
|
29
|
+
else { return 1 }
|
|
30
|
+
return switchOverFactors[wideIndex - 1]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/// Clamps a pinch-requested zoom to the device range and the 8x-of-1x cap.
|
|
34
|
+
static func clampZoom(
|
|
35
|
+
_ requested: CGFloat, minFactor: CGFloat, maxFactor: CGFloat, wideFactor: CGFloat
|
|
36
|
+
) -> CGFloat {
|
|
37
|
+
let upper = max(minFactor, min(maxFactor, wideFactor * maxZoomMultiple))
|
|
38
|
+
return min(max(requested, minFactor), upper)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static func preferredBackCamera() -> AVCaptureDevice? {
|
|
42
|
+
for type in preferredBackDeviceTypes {
|
|
43
|
+
if let device = AVCaptureDevice.default(type, for: .video, position: .back) {
|
|
44
|
+
return device
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return AVCaptureDevice.default(for: .video)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static func wideAngleZoomFactor(for device: AVCaptureDevice) -> CGFloat {
|
|
51
|
+
wideAngleZoomFactor(
|
|
52
|
+
constituentTypes: device.constituentDevices.map(\.deviceType),
|
|
53
|
+
switchOverFactors: device.virtualDeviceSwitchOverVideoZoomFactors.map {
|
|
54
|
+
CGFloat(truncating: $0)
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
}
|
package/ios/QrScanner.swift
CHANGED
|
@@ -10,10 +10,35 @@ final class QrPreviewView: UIView {
|
|
|
10
10
|
layer as! AVCaptureVideoPreviewLayer
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
private let focusIndicator: UIView = {
|
|
14
|
+
let view = UIView(frame: CGRect(x: 0, y: 0, width: 72, height: 72))
|
|
15
|
+
view.layer.borderColor = UIColor.white.cgColor
|
|
16
|
+
view.layer.borderWidth = 1.5
|
|
17
|
+
view.layer.cornerRadius = 8
|
|
18
|
+
view.isUserInteractionEnabled = false
|
|
19
|
+
view.alpha = 0
|
|
20
|
+
return view
|
|
21
|
+
}()
|
|
22
|
+
|
|
13
23
|
override init(frame: CGRect) {
|
|
14
24
|
super.init(frame: frame)
|
|
15
25
|
backgroundColor = .black
|
|
16
26
|
previewLayer.videoGravity = .resizeAspectFill
|
|
27
|
+
addSubview(focusIndicator)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/// Shows the tap-to-focus box at `point` and fades it out.
|
|
31
|
+
func showFocusIndicator(at point: CGPoint) {
|
|
32
|
+
focusIndicator.layer.removeAllAnimations()
|
|
33
|
+
focusIndicator.center = point
|
|
34
|
+
focusIndicator.alpha = 1
|
|
35
|
+
focusIndicator.transform = CGAffineTransform(scaleX: 1.25, y: 1.25)
|
|
36
|
+
UIView.animate(withDuration: 0.2) {
|
|
37
|
+
self.focusIndicator.transform = .identity
|
|
38
|
+
}
|
|
39
|
+
UIView.animate(withDuration: 0.3, delay: 0.8, options: [.beginFromCurrentState]) {
|
|
40
|
+
self.focusIndicator.alpha = 0
|
|
41
|
+
}
|
|
17
42
|
}
|
|
18
43
|
|
|
19
44
|
required init?(coder: NSCoder) {
|
|
@@ -37,6 +62,11 @@ class HybridQrScanner: HybridQrScannerSpec {
|
|
|
37
62
|
private let session = AVCaptureSession()
|
|
38
63
|
private var isConfigured = false
|
|
39
64
|
private var running = false
|
|
65
|
+
/// The configured input device. Owned by `sessionQueue`.
|
|
66
|
+
private var device: AVCaptureDevice?
|
|
67
|
+
/// Zoom factor when the current pinch began. Owned by `sessionQueue`.
|
|
68
|
+
private var pinchStartZoom: CGFloat = 1
|
|
69
|
+
private lazy var gestureHandler = GestureHandler(owner: self)
|
|
40
70
|
private var observers: [NSObjectProtocol] = []
|
|
41
71
|
private lazy var metadataDelegate = MetadataDelegate(owner: self)
|
|
42
72
|
|
|
@@ -48,6 +78,11 @@ class HybridQrScanner: HybridQrScannerSpec {
|
|
|
48
78
|
override init() {
|
|
49
79
|
super.init()
|
|
50
80
|
previewView.previewLayer.session = session
|
|
81
|
+
let tap = UITapGestureRecognizer(target: gestureHandler, action: #selector(GestureHandler.tap(_:)))
|
|
82
|
+
let pinch = UIPinchGestureRecognizer(
|
|
83
|
+
target: gestureHandler, action: #selector(GestureHandler.pinch(_:)))
|
|
84
|
+
previewView.addGestureRecognizer(tap)
|
|
85
|
+
previewView.addGestureRecognizer(pinch)
|
|
51
86
|
}
|
|
52
87
|
|
|
53
88
|
func afterUpdate() {
|
|
@@ -71,7 +106,7 @@ class HybridQrScanner: HybridQrScannerSpec {
|
|
|
71
106
|
guard !running else { return }
|
|
72
107
|
let currentGeneration = nextGeneration()
|
|
73
108
|
// Check for hardware first: a device without a camera must not prompt.
|
|
74
|
-
guard
|
|
109
|
+
guard QrCameraControl.preferredBackCamera() != nil else {
|
|
75
110
|
emitError("no-camera", "No camera is available", currentGeneration)
|
|
76
111
|
return
|
|
77
112
|
}
|
|
@@ -125,7 +160,7 @@ class HybridQrScanner: HybridQrScannerSpec {
|
|
|
125
160
|
}
|
|
126
161
|
|
|
127
162
|
private func configure() -> (code: String, message: String)? {
|
|
128
|
-
guard let device =
|
|
163
|
+
guard let device = QrCameraControl.preferredBackCamera() else {
|
|
129
164
|
return ("no-camera", "No camera is available")
|
|
130
165
|
}
|
|
131
166
|
session.beginConfiguration()
|
|
@@ -156,25 +191,97 @@ class HybridQrScanner: HybridQrScannerSpec {
|
|
|
156
191
|
output.metadataObjectTypes = [.qr]
|
|
157
192
|
|
|
158
193
|
if (try? device.lockForConfiguration()) != nil {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
if device.isAutoFocusRangeRestrictionSupported {
|
|
163
|
-
// Codes are held close to the lens.
|
|
164
|
-
device.autoFocusRangeRestriction = .near
|
|
165
|
-
}
|
|
194
|
+
// Virtual devices start on their widest lens; begin at the familiar 1x.
|
|
195
|
+
device.videoZoomFactor = QrCameraControl.wideAngleZoomFactor(for: device)
|
|
196
|
+
Self.applyContinuousFocus(device)
|
|
166
197
|
device.unlockForConfiguration()
|
|
167
198
|
}
|
|
199
|
+
self.device = device
|
|
168
200
|
return nil
|
|
169
201
|
}
|
|
170
202
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
203
|
+
/// Continuous focus and exposure over the whole frame. Caller holds the lock.
|
|
204
|
+
private static func applyContinuousFocus(_ device: AVCaptureDevice) {
|
|
205
|
+
let center = CGPoint(x: 0.5, y: 0.5)
|
|
206
|
+
if device.isFocusPointOfInterestSupported { device.focusPointOfInterest = center }
|
|
207
|
+
if device.isFocusModeSupported(.continuousAutoFocus) {
|
|
208
|
+
device.focusMode = .continuousAutoFocus
|
|
209
|
+
}
|
|
210
|
+
if device.isAutoFocusRangeRestrictionSupported {
|
|
211
|
+
// Codes are held close to the lens.
|
|
212
|
+
device.autoFocusRangeRestriction = .near
|
|
213
|
+
}
|
|
214
|
+
if device.isExposurePointOfInterestSupported { device.exposurePointOfInterest = center }
|
|
215
|
+
if device.isExposureModeSupported(.continuousAutoExposure) {
|
|
216
|
+
device.exposureMode = .continuousAutoExposure
|
|
217
|
+
}
|
|
218
|
+
device.isSubjectAreaChangeMonitoringEnabled = false
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// MARK: - Gestures (main thread in, sessionQueue for the device)
|
|
222
|
+
|
|
223
|
+
fileprivate func handleTap(_ recognizer: UITapGestureRecognizer) {
|
|
224
|
+
let layerPoint = recognizer.location(in: previewView)
|
|
225
|
+
let devicePoint = previewView.previewLayer.captureDevicePointConverted(fromLayerPoint: layerPoint)
|
|
226
|
+
previewView.showFocusIndicator(at: layerPoint)
|
|
227
|
+
sessionQueue.async { [weak self] in
|
|
228
|
+
guard let self, self.running, let device = self.device,
|
|
229
|
+
(try? device.lockForConfiguration()) != nil
|
|
230
|
+
else { return }
|
|
231
|
+
defer { device.unlockForConfiguration() }
|
|
232
|
+
if device.isFocusPointOfInterestSupported, device.isFocusModeSupported(.autoFocus) {
|
|
233
|
+
device.focusPointOfInterest = devicePoint
|
|
234
|
+
device.focusMode = .autoFocus
|
|
235
|
+
}
|
|
236
|
+
if device.isExposurePointOfInterestSupported,
|
|
237
|
+
device.isExposureModeSupported(.continuousAutoExposure)
|
|
238
|
+
{
|
|
239
|
+
device.exposurePointOfInterest = devicePoint
|
|
240
|
+
device.exposureMode = .continuousAutoExposure
|
|
241
|
+
}
|
|
242
|
+
// Moving the camera afterwards hands focus back to continuous mode.
|
|
243
|
+
device.isSubjectAreaChangeMonitoringEnabled = true
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
fileprivate func handlePinch(_ recognizer: UIPinchGestureRecognizer) {
|
|
248
|
+
let state = recognizer.state
|
|
249
|
+
let scale = recognizer.scale
|
|
250
|
+
sessionQueue.async { [weak self] in
|
|
251
|
+
guard let self, let device = self.device else { return }
|
|
252
|
+
if state == .began {
|
|
253
|
+
self.pinchStartZoom = device.videoZoomFactor
|
|
254
|
+
return
|
|
255
|
+
}
|
|
256
|
+
guard state == .changed, (try? device.lockForConfiguration()) != nil else { return }
|
|
257
|
+
defer { device.unlockForConfiguration() }
|
|
258
|
+
device.videoZoomFactor = QrCameraControl.clampZoom(
|
|
259
|
+
self.pinchStartZoom * scale,
|
|
260
|
+
minFactor: device.minAvailableVideoZoomFactor,
|
|
261
|
+
maxFactor: device.maxAvailableVideoZoomFactor,
|
|
262
|
+
wideFactor: QrCameraControl.wideAngleZoomFactor(for: device))
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
private func resumeContinuousFocus() {
|
|
267
|
+
sessionQueue.async { [weak self] in
|
|
268
|
+
guard let self, let device = self.device, (try? device.lockForConfiguration()) != nil
|
|
269
|
+
else { return }
|
|
270
|
+
Self.applyContinuousFocus(device)
|
|
271
|
+
device.unlockForConfiguration()
|
|
272
|
+
}
|
|
174
273
|
}
|
|
175
274
|
|
|
176
275
|
private func addObservers() {
|
|
177
276
|
let center = NotificationCenter.default
|
|
277
|
+
if let device {
|
|
278
|
+
observers.append(
|
|
279
|
+
center.addObserver(
|
|
280
|
+
forName: AVCaptureDevice.subjectAreaDidChangeNotification, object: device, queue: nil
|
|
281
|
+
) { [weak self] _ in
|
|
282
|
+
self?.resumeContinuousFocus()
|
|
283
|
+
})
|
|
284
|
+
}
|
|
178
285
|
observers.append(
|
|
179
286
|
center.addObserver(
|
|
180
287
|
forName: AVCaptureSession.wasInterruptedNotification, object: session, queue: nil
|
|
@@ -227,6 +334,23 @@ class HybridQrScanner: HybridQrScannerSpec {
|
|
|
227
334
|
}
|
|
228
335
|
}
|
|
229
336
|
|
|
337
|
+
private final class GestureHandler: NSObject {
|
|
338
|
+
private weak var owner: HybridQrScanner?
|
|
339
|
+
|
|
340
|
+
init(owner: HybridQrScanner) {
|
|
341
|
+
self.owner = owner
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
@objc func tap(_ recognizer: UITapGestureRecognizer) {
|
|
345
|
+
guard recognizer.state == .ended else { return }
|
|
346
|
+
owner?.handleTap(recognizer)
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
@objc func pinch(_ recognizer: UIPinchGestureRecognizer) {
|
|
350
|
+
owner?.handlePinch(recognizer)
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
230
354
|
private final class MetadataDelegate: NSObject, AVCaptureMetadataOutputObjectsDelegate {
|
|
231
355
|
private weak var owner: HybridQrScanner?
|
|
232
356
|
|