首页 技术 正文
技术 2022年11月14日
0 收藏 691 点赞 4,678 浏览 2185 个字

在项目开发中需要使用到条码扫描,因为以前就测试过ZXing,感觉识别速度和功能都不错,所以直接引用。不过在实际开发的过程中,却遇到连续扫描的问题,每次扫描识别完成之后,扫描窗体自动关闭了。

在Xamarin论坛中查找解决方案,只是找到的iOS版本的解决方案。参考iOS的解决方案,其实就是在扫描完成之后重新打开扫描。按照这个思路,想到使用Intent for result的方式来进行实现。实现方法如下代码:

主窗体:

 using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using ZXing.Mobile; namespace imStudio.QRCodeLife
{
[Activity (Label = "imStudio.QRCodeLife", MainLauncher = true)]
public class MainActivity : Activity
{
int count = ; protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle); // Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main); // Get our button from the layout resource,
// and attach an event to it
var button = FindViewById<Button>(Resource.Id.myButton);
var tv = FindViewById<TextView>(Resource.Id.textView1); button.Click += async delegate
{
StartActivityForResult(typeof(CodeActivity),);
};
} protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if (requestCode == )
{
if (resultCode == Result.Ok)
{
FindViewById<TextView>(Resource.Id.textView1).Text += data.GetStringExtra("Code") + System.Environment.NewLine;
StartActivityForResult(typeof(CodeActivity), );
}
}
}
}
}

子窗体,增加一个“完成”或“取消”按钮,用于关闭扫码窗体,代码如下:

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 here
var 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,先用着,回头再想有没有好的办法。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,084
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,559
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,408
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,181
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,818
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,901