从Android Web视图启动QR扫描仪,并使用Xamarin返回扫描结果 [英] Launching a QR scanner from Android web view and returning the scanned result using Xamarin

查看:174
本文介绍了从Android Web视图启动QR扫描仪,并使用Xamarin返回扫描结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个Xamarin应用程序,当单击一个按钮时,它会启动QR扫描仪.此按钮单击是使用Javascript处理的.单击该按钮时,将调用下面的C#代码.这应该启动QR扫描器,并且一旦扫描了值,扫描的值将返回到Javascript函数.但是,一旦单击了扫描QR码的按钮,Web视图应用程序即会进入后台,但不会启动相机来扫描QR码.

I have this Xamarin application that launches a QR scanner when a button is clicked. This button click is handled in Javascript. When the button is clicked, below C# code gets called. This supposed to launch the QR scanner and once the value is scanned, the scanned value is returned to a Javascript function. But as soon as the button to scan QR code is clicked, web view app gets to the background but camera is not launched to scan the QR code.

 public class QRScannerJSInterface : Java.Lang.Object
    {
        QRScanner qrScanner;
        WebView webView;

        public QRScannerJSInterface(WebView webView)
        {
            this.webView = webView;
            qrScanner = new QRScanner();
        }

        [Export]
        [JavascriptInterface]
        public void ScanQR()
        {
            String result = qrScanner.ScanQR();
            var js = string.Format("getQRValue('{0}');", result);
            webView.LoadUrl("javascript:" + js);
            //call the Javascript method here with "result" as its parameter to get the scanned value
        }

下面是主要活动如何调用此类.

Below is how the main activity calls this class.

        webView = FindViewById<WebView>(Resource.Id.webView);
        webView.Settings.JavaScriptEnabled = true;
        webView.Settings.AllowFileAccessFromFileURLs = true;
        webView.Settings.AllowUniversalAccessFromFileURLs = true;
        webView.Settings.AllowFileAccess = true;
        webView.AddJavascriptInterface(new QRScannerJSInterface(webView),"CSharpQRInterface");

下面是QRScanner代码.

Below is the QRScanner code.

class QRScanner
    {
        MobileBarcodeScanner scanner;

        public QRScanner()
        {
            scanner = new MobileBarcodeScanner();
        }

        public String ScanQR()
        {
            scanner.UseCustomOverlay = false;
            scanner.TopText = "Scanning for barcode";
            Task<ZXing.Result> result = scanner.Scan();
            return result.ToString();
        }
    }

我在这里做错了什么?任何帮助将不胜感激.

What am I doing wrong here? Any help would be much appreciated.

推荐答案

下面是有效的解决方案.请注意扫描仪异步扫描更改.在将Task放入js文件之前,您需要等待结果.

Below is working solution. Please note scanner async Scan changes. before you would get Task into your js file, you need to await for result.

设置scannerPage.html

Asset scannerPage.html

<html>
<head>
    <title></title>

    <script type='text/javascript'>

        function getQRValue(result) {
    };

        function scan() {
            CSharpQRInterface.ScanQR();
        };


    </script>

</head>
<body style="background-color:powderblue;">
    <button type="button" onclick="scan()">Click Me!</button>
</body>
</html>

MainActivity

MainActivity

public class MainActivity : Activity
    {
        WebView webView;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            MobileBarcodeScanner.Initialize(Application);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            webView = FindViewById<WebView>(Resource.Id.webView);
            webView.Settings.JavaScriptEnabled = true;
            webView.Settings.AllowFileAccessFromFileURLs = true;
            webView.Settings.AllowUniversalAccessFromFileURLs = true;
            webView.Settings.AllowFileAccess = true;
            webView.AddJavascriptInterface(new QRScannerJSInterface(webView), "CSharpQRInterface");
            webView.LoadUrl("file:///android_asset/scannerPage.html");

        }

扫描仪界面

public class QRScannerJSInterface : Java.Lang.Object
    {
        QRScanner qrScanner;
        WebView webView;

        public QRScannerJSInterface(WebView webView)
        {
            this.webView = webView;
            qrScanner = new QRScanner();
        }

        [Export("ScanQR")]
        public void ScanQR()
        {
            qrScanner.ScanQR()
                .ContinueWith((t) =>
                {
                    //var js = string.Format("getQRValue('{0}');", t.Result);
                    //webView.LoadUrl("javascript:" + js);
                    //call the Javascript method here with "result" as its parameter to get the scanned value
                    if (t.Status == TaskStatus.RanToCompletion)
                        webView.LoadUrl(@"javascript:getQRValue('" + t.Result + "')");
                });
        }
    }

扫描仪类

class QRScanner
    {
        MobileBarcodeScanner scanner;

        public QRScanner()
        {
            scanner = new MobileBarcodeScanner();
        }

        public async Task<string> ScanQR()
        {
            scanner.UseCustomOverlay = false;
            scanner.TopText = "Scanning for barcode";
            var result = await scanner.Scan();
            return result.ToString();
        }
    }

这篇关于从Android Web视图启动QR扫描仪,并使用Xamarin返回扫描结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆