日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Xamarin开发Android笔记:使用ZXing进行连续扫描

發布時間:2025/6/15 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Xamarin开发Android笔记:使用ZXing进行连续扫描 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在項目開發中需要使用到條碼掃描,因為以前就測試過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 herevar 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

總結

以上是生活随笔為你收集整理的Xamarin开发Android笔记:使用ZXing进行连续扫描的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。