淘先锋技术网

首页 1 2 3 4 5 6 7

在项目开发中需要使用到条码扫描,因为以前就测试过ZXing,感觉识别速度和功能都不错,所以直接引用。不过在实际开发的过程中,却遇到连续扫描的问题,每次扫描识别完成之后,扫描窗体自动关闭了。

在Xamarin论坛中查找解决方案,只是找到的iOS版本的解决方案。参考iOS的解决方案,其实就是在扫描完成之后重新打开扫描。按照这个思路,想到使用Intent for result的方式来进行实现。实现方法如下代码:

 

主窗体:

 1 using System;
 2 using Android.App;
 3 using Android.Content;
 4 using Android.Runtime;
 5 using Android.Views;
 6 using Android.Widget;
 7 using Android.OS;
 8 using ZXing.Mobile;
 9 
10 namespace imStudio.QRCodeLife
11 {
12     [Activity (Label = "imStudio.QRCodeLife", MainLauncher = true)]
13     public class MainActivity : Activity
14     {
15         int count = 1;
16 
17         protected override void OnCreate (Bundle bundle)
18         {
19             base.OnCreate (bundle);
20 
21             // Set our view from the "main" layout resource
22             SetContentView (Resource.Layout.Main);
23 
24             // Get our button from the layout resource,
25             // and attach an event to it
26             var button = FindViewById<Button>(Resource.Id.myButton);
27             var tv = FindViewById<TextView>(Resource.Id.textView1);
28             
29             button.Click += async delegate
30             {
31                 StartActivityForResult(typeof(CodeActivity),1);
32             };
33         }
34 
35         protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
36         {
37             if (requestCode == 1)
38             {
39                 if (resultCode == Result.Ok)
40                 {
41                     FindViewById<TextView>(Resource.Id.textView1).Text += data.GetStringExtra("Code") + System.Environment.NewLine;
42                     StartActivityForResult(typeof(CodeActivity), 1);
43                 }
44             }
45         }
46     }
47 }

子窗体,增加一个“完成”或“取消”按钮,用于关闭扫码窗体,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace imStudio.QRCodeLife
{
    [Activity(Label = "CodeActivity")]
    public class CodeActivity : Activity
    {
        protected override async void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Create your application here
            var scanner = new ZXing.Mobile.MobileBarcodeScanner(this);
            scanner.UseCustomOverlay = true;
            var zxingOverlay = LayoutInflater.FromContext(this).Inflate(Resource.Layout.Code, null);
            var doneButton = zxingOverlay.FindViewById<Button>(Resource.Id.buttonZxingDone);
            doneButton.Click += (sender, e) =>
            {
                scanner.Cancel();
                SetResult(Result.Canceled);
                Finish();
            };
            scanner.CustomOverlay = zxingOverlay;
            var result = await scanner.Scan();

            HandleScanResult(result);
        }

        private void HandleScanResult(ZXing.Result result)
        {
            if (result != null)
            {
                Intent intent = new Intent();
                intent.PutExtra("Code", result.Text);
                SetResult(Result.Ok,intent);
                Finish();
            }
        }
    }
}

实现代码虽然有些粗糙,不过功能OK,先用着,回头再想有没有好的办法。

转载于:https://www.cnblogs.com/songhaipeng/p/4316614.html