@@ -35,7 +35,8 @@ final class ContinuousBarcodeScanner {
35
35
metadataOutput. setMetadataObjectsDelegate ( self , queue: metadataQueue)
36
36
}
37
37
38
- func startRunning( barcodeScannedClosure: @escaping BarcodeScannedClosure ) {
38
+ func startRunning( rectOfInterest: CGRect , _ barcodeScannedClosure: @escaping BarcodeScannedClosure ) {
39
+ metadataOutput. rectOfInterest = rectOfInterest
39
40
metadataOutput. metadataObjectTypes = metadataOutput. availableMetadataObjectTypes
40
41
self . barcodeScannedClosure = barcodeScannedClosure
41
42
}
@@ -99,7 +100,23 @@ final class ContinuousBarcodeScanner {
99
100
captureSession. addInput ( videoInput)
100
101
101
102
previewLayer = AVCaptureVideoPreviewLayer ( session: captureSession)
102
- previewLayer. frame = previewView. bounds
103
+
104
+ if previewView. bounds. height > previewView. bounds. width {
105
+ previewLayer. frame = . init(
106
+ x: 0 ,
107
+ y: 0 ,
108
+ width: previewView. bounds. height,
109
+ height: previewView. bounds. width
110
+ )
111
+ } else {
112
+ previewLayer. frame = . init(
113
+ x: 0 ,
114
+ y: 0 ,
115
+ width: previewView. bounds. width,
116
+ height: previewView. bounds. height
117
+ )
118
+ }
119
+
103
120
previewLayer. videoGravity = . resizeAspectFill
104
121
105
122
metadataCapture = MetadataContinousCapture ( )
@@ -161,7 +178,8 @@ extension ContinuousBarcodeScanner {
161
178
func startRunning( ) {
162
179
captureSession. startRunning ( )
163
180
164
- metadataCapture. startRunning { [ weak self] barcodeObject in
181
+ let rectOfInterest = previewLayer. metadataOutputRectConverted ( fromLayerRect: previewView. bounds)
182
+ metadataCapture. startRunning ( rectOfInterest: rectOfInterest) { [ weak self] barcodeObject in
165
183
self ? . delegate? . didScan ( barcode: barcodeObject. stringValue ?? " " )
166
184
self ? . addBarcodeOverlayViewFor ( barcodeObject: barcodeObject)
167
185
}
0 commit comments