我正在尝试使用ZXingScannerFragment类扫描Xamarin.Android中的QR码。摄像机运行正常,一切正常,ZXing甚至将“找到条形码”写入输出控制台。但是,永远不会调用处理结果的函数。我有一个预感,问题可能出在上下文和活动上,但是相机再次像我想要的那样加载得很好,但是我从来没有得到任何扫描结果。
这是我的片段代码:
public class HomeFragment : Fragment
{
private ZXingScannerFragment scanFragment;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override void OnResume()
{
base.OnResume();
MobileBarcodeScanner.Initialize(this.Activity.Application);
var needsPermissionRequest = ZXing.Net.Mobile.Android.PermissionsHandler.NeedsPermissionRequest(Model.Model.MainActivityContext);
if (needsPermissionRequest)
ZXing.Net.Mobile.Android.PermissionsHandler.RequestPermissionsAsync(this.Activity);
if (scanFragment == null)
{
scanFragment = new ZXingScannerFragment();
FragmentManager.BeginTransaction()
.Replace(Resource.Id.container, scanFragment)
.Commit();
}
if (!needsPermissionRequest)
Scan();
}
private void Scan()
{
var opts = new MobileBarcodeScanningOptions
{
PossibleFormats = new List<BarcodeFormat> {
ZXing.BarcodeFormat.QR_CODE
},
CameraResolutionSelector = availableResolutions => {
foreach (var ar in availableResolutions)
{
Console.WriteLine("Resolution: " + ar.Width + "x" + ar.Height);
}
return null;
}
};
scanFragment.StartScanning(result => {
// This function is never called
if (result == null || string.IsNullOrEmpty(result.Text))
{
Toast.MakeText(Model.Model.MainActivityContext, "Scanning Cancelled", ToastLength.Long).Show();
return;
}
Activity.RunOnUiThread(() => Toast.MakeText(Model.Model.MainActivityContext, "Scanned: " + result.Text, ToastLength.Short).Show());
}, opts);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.Inflate(Resource.Layout.home_fragment, container, false);
//return base.OnCreateView(inflater, container, savedInstanceState);
}
}
大多数内容来自ZXing在其GitHub页面上提供的示例。
谢谢!