Xamarin.android掃描二維碼,條形碼

前些日子,閒來無聊就玩了下Xamarin.android掃描二維碼,條形碼功能,因此製做了一個demoandroid

效果很好,速度快,準確性高。async

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

直接看圖先。this

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

主要代碼:htm

[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());
        }
    }get


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

相關文章
相關標籤/搜索