首页 > 解决方案 > Xamarin.Forms VNDocumentCameraViewController Scanner如何在Android中实现类似

问题描述

我在我的 Xamarin.Forms 应用程序中实现了 IOS 13+ Vision 支持的 Document Scanner。这是我的 IOS SharedFunction 代码

[assembly: Dependency(typeof(CameraScanner))]
    class CameraScanner : ICameraScanner
    {
        public void OpenScanCamera()
        {
            ScanViewController viewController = new ScanViewController();
            UIApplication.SharedApplication.KeyWindow.RootViewController.
            PresentViewController(viewController, false, null);
        }
        public static UIKit.UIViewController GetTopViewController()
        {
            var window = UIApplication.SharedApplication.KeyWindow;
            var vc = window.RootViewController;
            while (vc.PresentedViewController != null)
                vc = vc.PresentedViewController;

            if (vc is UINavigationController navController)
                vc = navController.ViewControllers.Last();
            return vc;
        }
    }

VNDocumentCameraViewController 类逻辑,用于制作 IOS Vision 支持的拍照和扫描视图

    [Register("UniversalView")]
    public class UniversalView : UIView
    {
        public UniversalView()
        {
            Initialize();
        }

        public UniversalView(RectangleF bounds) : base(bounds)
        {
            Initialize();
        }

        void Initialize()
        {
            BackgroundColor = UIColor.Blue;

        }
    }

    [Register("ScanViewController")]
    public class ScanViewController : UIViewController
    {
        public ScanViewController()
        {
        }

        public override void DidReceiveMemoryWarning()
        {
            // Releases the view if it doesn't have a superview.
            base.DidReceiveMemoryWarning();

            // Release any cached data, images, etc that aren't in use.
        }

        public override void ViewDidLoad()
        {
            View = new UniversalView();



            base.ViewDidLoad();

            // Perform any additional setup after loading the view
        }

        public override void ViewDidAppear(bool animated)
        {
            var documentCameraController = new VNDocumentCameraViewController();
            var documentscanDelegate = new DocumentScanDelegate();
            documentscanDelegate.OnScanTaken += (VNDocumentCameraScan scan) =>
            {
                documentCameraController.DismissViewController(true, null);
                Debug.WriteLine($"{scan.PageCount} Pages!");
            };

            documentscanDelegate.OnCanceled += () =>
            {
                documentCameraController.DismissViewController(true, null);
            };

            documentCameraController.Delegate = documentscanDelegate;
            documentCameraController.ModalPresentationStyle = UIModalPresentationStyle.OverFullScreen;
            PresentViewController(documentCameraController, true, null);
        }
    }

    class DocumentScanDelegate : VNDocumentCameraViewControllerDelegate
    {
        public delegate void ScanTakenEventHandler(VNDocumentCameraScan scan);
        public event ScanTakenEventHandler OnScanTaken;

        public delegate void ScanCanceledEventHandler();
        public event ScanCanceledEventHandler OnCanceled;

        public override void DidCancel(VNDocumentCameraViewController controller)
        {
            Debug.WriteLine("DocumentScanDelegate:DidCancel");
            OnCanceled();
        }

        public override void DidFinish(VNDocumentCameraViewController controller, VNDocumentCameraScan scan)
        {
            Debug.WriteLine("DocumentScanDelegate:DidFinish");
            OnScanTaken(scan);
        }


        public override void DidFail(VNDocumentCameraViewController controller, NSError error)
        {
            Debug.WriteLine("Failed scanning photo");
        }
    }
}

哪个是类似的实现Xamarin.Android?我知道谷歌也支持相机扫描,但我找不到关于它的好文档。有人进口过这个吗?

标签: c#xamarin.formsxamarin.androidxamarin.ios

解决方案


推荐阅读