Skip to content

Commit cbf756b

Browse files
committed
Added maximum number of scans property in DocScanner.swift
Signed-off-by: hjafri256 <[email protected]>
1 parent 74eef85 commit cbf756b

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

DocumentScanner/Source/DocScanner.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ import UIKit
3737

3838
/// If true can scan and export multiple documents at onse, defaults to false
3939
public var exportMultiple: Bool = false
40+
41+
///Maximum number of scans. -1 for unlimited
42+
public var maximumScans = -1
4043

4144
/// Construct scanner object
4245
public init(presenter: UIViewController) {
@@ -132,8 +135,16 @@ import UIKit
132135

133136
// perform perspective correction
134137
if let flattened = photo.flattened(rect: self.camera.observationRect) {
138+
135139
self.scannedImages.append(flattened.noiseReducted.rotated)
136-
140+
141+
if self.maximumScans != -1
142+
&& self.scannedImages.count >= self.maximumScans {
143+
self.exportMultiple ? self.exportImages?(self.scannedImages) : self.exportImage?(self.scannedImages[0])
144+
self.scannedImages.removeAll()
145+
self.stopSession()
146+
}
147+
137148
return
138149
}
139150

@@ -146,6 +157,7 @@ import UIKit
146157
// return from manual cropping
147158
cropView.onRetake = {
148159
self.continueSession()
160+
self.stopSession()
149161
}
150162

151163
cropView.onRegionSave = {
@@ -157,6 +169,13 @@ import UIKit
157169
if let flattened = photo.flattened(rect: region) {
158170
self.scannedImages.append(flattened.noiseReducted.rotated)
159171

172+
if self.maximumScans != -1
173+
&& self.scannedImages.count >= self.maximumScans {
174+
self.exportMultiple ? self.exportImages?(self.scannedImages) : self.exportImage?(self.scannedImages[0])
175+
self.scannedImages.removeAll()
176+
self.stopSession()
177+
}
178+
160179
return
161180
}
162181

@@ -186,6 +205,13 @@ import UIKit
186205

187206
let croppedImage = photo.crop(toPreviewLayer: camera.cameraLayer, withRect: regionRect)
188207
scannedImages.append(croppedImage.noiseReducted.rotated)
208+
209+
if self.maximumScans != -1
210+
&& self.scannedImages.count >= self.maximumScans {
211+
self.exportMultiple ? self.exportImages?(self.scannedImages) : self.exportImage?(self.scannedImages[0])
212+
self.scannedImages.removeAll()
213+
self.stopSession()
214+
}
189215
}
190216

191217
private func toggleIdle(disabled: Bool) {

Example/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ SPEC CHECKSUMS:
1313

1414
PODFILE CHECKSUM: 7cfc6a5ca9cae37b2543fa20083bbc420031f30d
1515

16-
COCOAPODS: 1.9.0
16+
COCOAPODS: 1.15.2

Sources/DocumentScanner/DocScanner.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ import UIKit
3737

3838
/// If true can scan and export multiple documents at onse, defaults to false
3939
public var exportMultiple: Bool = false
40+
41+
///Maximum number of scans. -1 for unlimited
42+
public var maximumScans = -1
4043

4144
/// Construct scanner object
4245
public init(presenter: UIViewController) {
@@ -132,8 +135,16 @@ import UIKit
132135

133136
// perform perspective correction
134137
if let flattened = photo.flattened(rect: self.camera.observationRect) {
138+
135139
self.scannedImages.append(flattened.noiseReducted.rotated)
136-
140+
141+
if self.maximumScans != -1
142+
&& self.scannedImages.count >= self.maximumScans {
143+
self.exportMultiple ? self.exportImages?(self.scannedImages) : self.exportImage?(self.scannedImages[0])
144+
self.scannedImages.removeAll()
145+
self.stopSession()
146+
}
147+
137148
return
138149
}
139150

@@ -146,6 +157,7 @@ import UIKit
146157
// return from manual cropping
147158
cropView.onRetake = {
148159
self.continueSession()
160+
self.stopSession()
149161
}
150162

151163
cropView.onRegionSave = {
@@ -157,6 +169,13 @@ import UIKit
157169
if let flattened = photo.flattened(rect: region) {
158170
self.scannedImages.append(flattened.noiseReducted.rotated)
159171

172+
if self.maximumScans != -1
173+
&& self.scannedImages.count >= self.maximumScans {
174+
self.exportMultiple ? self.exportImages?(self.scannedImages) : self.exportImage?(self.scannedImages[0])
175+
self.scannedImages.removeAll()
176+
self.stopSession()
177+
}
178+
160179
return
161180
}
162181

@@ -186,6 +205,13 @@ import UIKit
186205

187206
let croppedImage = photo.crop(toPreviewLayer: camera.cameraLayer, withRect: regionRect)
188207
scannedImages.append(croppedImage.noiseReducted.rotated)
208+
209+
if self.maximumScans != -1
210+
&& self.scannedImages.count >= self.maximumScans {
211+
self.exportMultiple ? self.exportImages?(self.scannedImages) : self.exportImage?(self.scannedImages[0])
212+
self.scannedImages.removeAll()
213+
self.stopSession()
214+
}
189215
}
190216

191217
private func toggleIdle(disabled: Bool) {

0 commit comments

Comments
 (0)