-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose AVDocumentScanner to be public (#34)
* Make stuff public * Make more stuff public * SwiftLint * Add custom UIViewController class Showing how to use the scanner directly * Add some documentation
- Loading branch information
1 parent
1a6f22d
commit 76ba026
Showing
8 changed files
with
117 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// CustomUIViewController.swift | ||
// YesWeScanExampleApp | ||
// | ||
// Created by Xaver Lohmüller on 02.05.19. | ||
// Copyright © 2019 adorsys GmbH & Co KG. All rights reserved. | ||
// | ||
|
||
import YesWeScan | ||
|
||
final class CustomUIViewController: UIViewController { | ||
private lazy var scanner: DocumentScanner = AVDocumentScanner(delegate: self) | ||
|
||
private var detectionLayer: CAShapeLayer = { | ||
let layer = CAShapeLayer() | ||
layer.fillColor = UIColor.red.withAlphaComponent(0.3).cgColor | ||
layer.strokeColor = UIColor.red.withAlphaComponent(0.9).cgColor | ||
layer.lineWidth = 2 | ||
layer.contentsGravity = .resizeAspectFill | ||
return layer | ||
}() | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
scanner.previewLayer.frame = UIScreen.main.bounds | ||
view.layer.addSublayer(scanner.previewLayer) | ||
scanner.previewLayer.addSublayer(detectionLayer) | ||
} | ||
} | ||
|
||
|
||
extension CustomUIViewController: DocumentScannerDelegate { | ||
func didCapture(image: UIImage) { | ||
let imageView = UIImageView(image: image) | ||
imageView.showAnimated(on: view) | ||
|
||
scanner.stop() | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 3) { | ||
imageView.hideAnimated() | ||
self.scanner.start() | ||
} | ||
} | ||
|
||
func didRecognize(feature: RectangleFeature?, in image: CIImage) { | ||
detectionLayer.path = feature?.bezierPath.cgPath | ||
} | ||
} | ||
|
||
private extension UIView { | ||
func showAnimated(on view: UIView, duration: TimeInterval = 0.5) { | ||
frame = view.frame | ||
alpha = 0 | ||
view.addSubview(self) | ||
UIView.animate(withDuration: duration) { | ||
self.alpha = 1 | ||
} | ||
} | ||
|
||
func hideAnimated(duration: TimeInterval = 0.5) { | ||
UIView.animate(withDuration: duration, | ||
animations: { self.alpha = 0 }, | ||
completion: { _ in self.removeFromSuperview() }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
import UIKit | ||
import CoreImage | ||
|
||
protocol DocumentScannerDelegate: AnyObject { | ||
// Always called on main queue | ||
// Methods are always called on main queue | ||
public protocol DocumentScannerDelegate: AnyObject { | ||
|
||
/// Called when the scanner successfully found an image | ||
/// | ||
/// - Parameter image: The image that was found | ||
func didCapture(image: UIImage) | ||
|
||
/// Use this method to display a preview border of the image that is about | ||
/// to be recognized | ||
/// | ||
/// - Parameters: | ||
/// - feature: The extent of the image that is being recognized | ||
/// - image: The image that contains the image to be recognized | ||
func didRecognize(feature: RectangleFeature?, in image: CIImage) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters