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

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

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

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

直接看图先。

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

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

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

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

你可能感兴趣的:(android,条形码,Xamarin,扫描二维码)