我试图在扫描文档后关闭相机视图,但它不起作用。我尝试使用当前方法关闭,相机成功关闭,但扫描的图像未打印。如果我使用 removefromsuperview 然后相机视图得到结构或崩溃。
这里相机扫描框架内使用的文档代码,我在示例项目中使用了框架。
这是扫描仪的框架代码:
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
这是代码示例项目:
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
如何关闭相机视图并将扫描的图像发送到示例项目?
任何帮助非常感谢请...