当前位置: 移动技术网 > IT编程>开发语言>.net > C#-Xamarin利用ZXing.Net.Mobile进行扫码

C#-Xamarin利用ZXing.Net.Mobile进行扫码

2019年03月14日  | 移动技术网IT编程  | 我要评论

重庆花样年华聊天室,叶籽亿,千年染色表

前言

很多人觉得xamarin的开源少,没法用来开发项目。

但,实际上xamarin已经有很多开源代码了;只要不是特别特殊的项目,基本上是都可以满足开发。

下面我们来看一下xamarin中利用开源代码zxing.net.mobile进行扫码。

引用zxing.net.mobile

在xamarin中进行扫码,需要先引用开源代码zxing.net.mobile。

下面我们先打开nuget,搜索zxing.net.mobile,如下图:

然后我们点击右侧安装。

接着visualstudio可能会提示我们这样的错误。

错误 检测到 xamarin.android.support.compat 存在版本冲突。直接从项目引用包可解决此问题。 
salesapp -> xamarin.android.support.design 25.4.0.2 -> xamarin.android.support.compat (= 25.4.0.2) 
salesapp -> zxing.net.mobile 2.4.1 -> xamarin.android.support.v4 25.4.0.1 -> xamarin.android.support.compat (= 25.4.0.1). 

这是因为我们选择安装的zxing.net.mobile依赖的dll和我们项目的dll对应不上。

解决办法很简单,选择下右侧zxing.net.mobile的其他版本安装。

----------------------------------------------------------------------------------------------------

但在visualstudio2017中有个非常坑的地方,就是nuget的引用会将相关dll都组织成一个包,然后一起引用,如下图:

看上去引用更简洁,按理说应该是好事,但这里有个问题就是,你引用的dll并没有被下载到本地。

也就是说,如果你网不好,或者断网,就别想调试了。

所以最终我还是选择把相关dll都下载下来,然后直接引用,相关dll如下图:

 

使用mobilebarcodescanner扫码

本项目里,我们使用zxing.net.mobile下的mobilebarcodescanner类进行扫码。

下面简单介绍下mobilebarcodescanner的使用方法。

首先,mobilebarcodescanner类的实例需要调用scan方法才会进行扫码。

而scan方法被调用后,会自己打开了一个activity。

而这个被打开的activity的ui,就是,他对应的axml也是可以被自定义的。

自定义的方法就是scan方法被调用前,为mobilebarcodescanner类的customoverlay属性赋值。

有兴趣的同学可以查看下mobilebarcodescanner的【源代码】,更深入的了解一些。

mobilebarcodescanner的scan方法有两个参数。

第一个参数是当前调用扫码的activity,也可以传null,传null的话,mobilebarcodescanner类里自己找context为他赋值。

第二个参数是当前扫描的配置属性;为mobilebarcodescanningoptions类型。

下面我们看调用代码。

 view zxingoverlay;
 mobilebarcodescanner scanner;
 protected override void oncreate(bundle savedinstancestate)
 {
     base.oncreate(savedinstancestate);
     setcontentview(resource.layout.mainactivity);
    
     button btnscan = this.findcontrol<button>("btnscan");
     btnscan.click += (s, e) =>
     { 
         scanner = new mobilebarcodescanner(); 
         task t = new task(autoscan);
         t.start();
     };  
 }
 async void autoscan()
 { 
     scanner.usecustomoverlay = true;
     zxingoverlay = layoutinflater.fromcontext(this).inflate(resource.layout.zxingoverlay, null); 
     imageview ivscanning = zxingoverlay.findviewbyid<imageview>(resource.id.ivscanning); 
     button btncancelscan = zxingoverlay.findviewbyid<button>(resource.id.btncancelscan);
     btncancelscan.click += (s, e) =>
     {
         if (scanner != null)
         {
             scanner.cancel();
         }
     };

     zxingoverlay.measure(measurespecmode.unspecified.gethashcode(), measurespecmode.unspecified.gethashcode());
     int width = zxingoverlay.measuredwidth;
     int height = zxingoverlay.measuredheight; 

     // 从上到下的平移动画
     animation verticalanimation = new translateanimation(0, 0, 0, height);
     verticalanimation.duration = 3000; // 动画持续时间
     verticalanimation.repeatcount = animation.infinite; // 无限循环

     // 播放动画
     ivscanning.animation = verticalanimation;
     verticalanimation.startnow();
           
     scanner.customoverlay = zxingoverlay;
     var mbs = mobilebarcodescanningoptions.default;
     mbs.assumegs1 = true;
     mbs.autorotate = true;
     mbs.disableautofocus = false;
     mbs.purebarcode = false;
     mbs.tryinverted = true;
     mbs.tryharder = true;
     mbs.usecode39extendedmode = true;
     mbs.usefrontcameraifavailable = false;
     mbs.usenativescanning = true;

     var result = await scanner.scan(this, mbs);
     handlescanresult(result);

 }  
 void handlescanresult(zxing.result result)
 {
     if (result != null && !string.isnullorempty(result.text))
     {
         if (result.text != null && result.text.trim().length > 5)
         {
             this.runonui(() => { this.showtoast(result.text); });

         }
         else
         {
             this.runonui(() => { this.showtoast("扫描无数据"); });
         }
     }
     else
     {
         this.runonui(() => { this.showtoast("扫描取消"); });
     }
     scanner.cancel();  
 }

如上代码所示,我们把项目中写好的zxingoverlay.axml赋值给了mobilebarcodescanner的customoverlay属性。

并且在赋值前,我们为页面内的btncancelscan按钮定义了取消事件;同时还定义了一个扫描动画。

因为定义动画时,页面还没加载出来,所以要取高度进行动画移动的话,需要先进行下预测。

代码中调用了measure方法进行预测,然后再取出预测的高度和宽度measuredheight,measuredwidth进行动画操作。

----------------------------------------------------------------------------------------------------

最终扫描界面如下图所示:

----------------------------------------------------------------------------------------------------

到此扫码使用就介绍完了。

框架代码已经传到github上了,欢迎大家下载。

相关文章:

c#-xamarin的android项目开发(三)——发布、部署、打包

c#-xamarin的android项目开发(二)——控件应用

c#-xamarin的android项目开发(一)——创建项目

github地址:https://github.com/kiba518/kibaapp

----------------------------------------------------------------------------------------------------

注:此文章为原创,欢迎转载,请在文章页面明显位置给出此文链接!
若您觉得这篇文章还不错,请点击下右下角的推荐】,非常感谢!

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网