I trying to dismiss camera view after document scanned but it does not work. I tried to dismiss using present method, camera dismissed successfully but the image scanned is not printed. If i use removefromsuperview then camera view got struct or getting crash.
Here camera scan document code used inside framework and i have used framework in sample project.
Here is the framework code for scanner:
public class A8Scan: NSObject, VNDocumentCameraViewControllerDelegate {
public var imageCallBack: ((UIImage) -> Void)?
var textVal = [VNRecognizedTextObservation]()
var textRecognitionRequest = VNRecognizeTextRequest(completionHandler: nil)
private let textRecognitionWorkQueue = DispatchQueue(label: "MyVisionScannerQueue", qos: .userInitiated, attributes: [], autoreleaseFrequency: .workItem)
var imageNew: UIImage?
var statusImage: UIImageView?
private var clientView: UIViewController?
public init(_ viewController:UIViewController){
self.clientView = viewController
}
public func showScanner(){
self.createTaskController()
print("Called Build")
}
private func createTaskController(){
let scannerViewController = VNDocumentCameraViewController()
scannerViewController.delegate = self
self.clientView?.addChild(scannerViewController)
self.clientView?.view.addSubview(scannerViewController.view)
scannerViewController.didMove(toParent: clientView)
self.clientView?.present(scannerViewController,animated:true,completion: nil)
}
public func imageFromFile(result: @escaping (_ image: UIImage?) -> Void){
//the image
if imageNew != nil {
result(imageNew)
}
else{
//callback nil so the app does not pause infinitely if
//the error != nil
result(nil)
}
}
public func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
guard scan.pageCount >= 1 else {
controller.dismiss(animated: true)
return
}
let originalImage = scan.imageOfPage(at: 0)
let newImage = compressedImage(originalImage)
imageNew = newImage
print(";;;", newImage)
imageCallBack?(newImage)
print("new image::\(newImage.size)")
controller.dismiss(animated: true, completion: nil)
}
public func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFailWithError error: Error) {
print(error)
controller.dismiss(animated: true)
}
public func documentCameraViewControllerDidCancel(_ controller: VNDocumentCameraViewController) {
controller.dismiss(animated: true)
}
func compressedImage(_ originalImage: UIImage) -> UIImage {
guard let imageData = originalImage.jpegData(compressionQuality: 1),
let reloadedImage = UIImage(data: imageData) else {
return originalImage
}
return reloadedImage
}
}//class
extension UIViewController {
class func loadFromNib<T: UIViewController>() -> T {
return T(nibName: String(describing: self), bundle: nil)
}
}//extension
Here is the code sample project:
import A8Scanner
class ViewController: UIViewController {
@IBOutlet weak var img: UIImageView!
var im : UIImage?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func btnAction(_ sender: Any) {
// A8Scan(self).showScanner()
let vc = A8Scan(self)
vc.showScanner()
}
override func viewDidAppear(_ animated: Bool) {
let vc = A8Scan(self)
vc.imageCallBack = { image in
print("image", image)
}
}
}//class
How to dismiss camera view and send image scanned to sample project?
Any help much appreciated pls...