0

我已经在框架内实现了视觉文档扫描仪。当调用相机视图控制器并捕获文档时。当点击保存按钮时,它应该关闭并返回到 viewController。

这是框架内的代码:

      public func showScanner(){
       self.createTaskController()
//        let scannerViewController = VNDocumentCameraViewController()
//        scannerViewController.delegate = self
//        present(scannerViewController, animated: true)
       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)
     scannerViewController.dismiss(animated: true)
 }

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("new image::\(newImage.size)")
        print("new imagei::\(newImage)")
        controller.dismiss(animated: true)
    }

    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
    }

这是我在示例项目中调用框架的代码:

 @IBAction func btnAction(_ sender: Any) {
    A8Scan(self).showScanner()
    p()
}

我的问题是当点击保存按钮时,它应该关闭相机控制器(VNDocumentCameraViewController)并返回到示例应用程序。但是,就我而言,它没有返回。

任何帮助非常感谢请...

4

1 回答 1

1

你在这里添加它作为一个孩子

let scannerViewController = VNDocumentCameraViewController()
private func createTaskController(){
    scannerViewController.delegate = self
    self.clientView?.addChild(scannerViewController)
    self.clientView?.view.addSubview(scannerViewController.view)
    scannerViewController.didMove(toParent: clientView)
 ///    scannerViewController.dismiss(animated: true) remove this line
}

然后删除做

scannerViewController.removeFromParent()
scannerViewController.view.removeFromSuperView()

或者

private func createTaskController(){ 
    let scannerViewController = VNDocumentCameraViewController()
    scannerViewController.delegate = self  
    self.clientView?.present(scannerViewController,animated:true,completion:nil)
}

解雇

controller.dismiss(animated: true)

要发送图像,请在 clientView 中创建一个函数并调用它

let newImage = compressedImage(originalImage)
self.clientView?.sendImage(newImage)
于 2019-12-09T10:39:21.583 回答