Xamarin.android扫描二维码,条形码

前些日子,闲来无聊就玩了下Xamarin.android扫描二维码,条形码功能,所以制作了一个demo

效果很好,速度快,准确性高。

程序获取地址:http://item.taobao.com/item.htm?spm=a1z10.1-c.w4004-9287888495.12.fWWy9f&id=45185930621

直接看图先。

123645_PuV4_933229.png

123645_EEEb_933229.png

123645_BZIH_933229.png

http://item.taobao.com/item.htm?spm=a1z10.1-c.w4004-9287888495.12.fWWy9f&id=45185930621

主要代码:

[Activity (Label = "ZXing.Net.Mobile", MainLauncher = true, Theme="@android:style/Theme.Holo.Light", ConfigurationChanges=ConfigChanges.Orientation|ConfigChanges.KeyboardHidden)]
    public class Activity1 : Activity
    {
        Button buttonScanCustomView;
        Button buttonScanDefaultView;
        Button buttonFragmentScanner;

        MobileBarcodeScanner scanner;

        TextView txtViewDefualt;
        TextView txtViewCustom;
        TextView txtViewFragment;
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

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

            txtViewDefualt = FindViewById < TextView >( Resource.Id.txtViewDefualt);
            txtViewCustom = FindViewById<TextView>(Resource.Id.txtViewCustom);
            txtViewFragment = FindViewById<TextView>(Resource.Id.txtViewFragment);

            //Create a new instance of our Scanner
            scanner = new MobileBarcodeScanner(this);

            buttonScanDefaultView = this.FindViewById<Button>(Resource.Id.buttonScanDefaultView);
            buttonScanDefaultView.Click += async delegate {
                
                //Tell our scanner to use the default overlay
                scanner.UseCustomOverlay = false;

                //We can customize the top and bottom text of the default overlay
                scanner.TopText = "Hold the camera up to the barcode\nAbout 6 inches away";
                scanner.BottomText = "Wait for the barcode to automatically scan!";

                //Start scanning
                var result = await scanner.Scan();
                txtViewDefualt.Text = result.Text;
                HandleScanResult(result);
            };

            Button flashButton;
            View zxingOverlay;

            buttonScanCustomView = this.FindViewById<Button>(Resource.Id.buttonScanCustomView);
            buttonScanCustomView.Click += async delegate {

                //Tell our scanner we want to use a custom overlay instead of the default
                scanner.UseCustomOverlay = true;

                //Inflate our custom overlay from a resource layout
                zxingOverlay = LayoutInflater.FromContext(this).Inflate(Resource.Layout.ZxingOverlay, null);

                //Find the button from our resource layout and wire up the click event
                flashButton = zxingOverlay.FindViewById<Button>(Resource.Id.buttonZxingFlash);
                flashButton.Click += (sender, e) => scanner.ToggleTorch();

                //Set our custom overlay
                scanner.CustomOverlay = zxingOverlay;

                //Start scanning!
                var result = await scanner.Scan();
                txtViewCustom.Text = result.Text;
                HandleScanResult(result);
            };

            buttonFragmentScanner = FindViewById<Button> (Resource.Id.buttonFragment);
            buttonFragmentScanner.Click += delegate {
                StartActivity (typeof (FragmentActivity));    
            };
        }

        void HandleScanResult (ZXing.Result result)
        {
            string msg = "";

            if (result != null && !string.IsNullOrEmpty(result.Text))
                msg = "Found Barcode: " + result.Text;
            else
                msg = "Scanning Canceled!";

            this.RunOnUiThread(() => Toast.MakeText(this, msg, ToastLength.Short).Show());
        }
    }


程序获取地址:http://item.taobao.com/item.htm?spm=a1z10.1-c.w4004-9287888495.12.fWWy9f&id=45185930621

转载于:https://my.oschina.net/u/933229/blog/412706

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值