[置顶] xamarin android使用zxing扫描二维码

好久没写了,这片文章篇幅不长,概述一下在xamarin android中用 ZXing.Net.Mobile库扫描二维码读取url的示例。扫码支付,扫码登录,App上各种各样的扫码,好像没个扫码的就有点low一样似的,主要就是利用这个原理扫描二维码读取其中的url,url由不同的参数组成,然后根据url发送请求做各种事情

zxing更多详细的内容见官网:http://zxingnet.codeplex.com/
ZXing.Net.Mobile github源码:https://github.com/Redth/ZXing.Net.Mobile

官方介绍:
ZXing.Net.Mobile is a C#/.NET library based on the open source Barcode Library: ZXing (Zebra Crossing), using the ZXing.Net Port. It works with Xamarin.iOS, Xamarin.Android, and Windows Phone. The goal of ZXing.Net.Mobile is to make scanning barcodes as effortless and painless as possible in your own applications. The new iOS7 AVCaptureSession barcode scanning is now also supported!
谷歌翻译:
ZXing.Net.Mobile是基于开源条码库的C#/ .NET库:ZXing(Zebra Crossing),使用ZXing.Net端口。它与Xamarin.iOS,Xamarin.Android和Windows Phone配合使用。ZXing.Net.Mobile的目标是使扫描条码在您自己的应用程序中尽可能轻松,无痛。现在也支持新的iOS7 AVCaptureSession条码扫描!

看看效果图吧:
xamarin android zxing扫码xamarin android zxing扫码

Nuget引入ZXing.Net.Mobile库

如图:这里写图片描述

最基本代码实现扫描二维码获取结果

首先要注意初始化扫描仪。这里在线生成二维码可以方便测试一下:
http://cli.im/

 public class MainActivity : Activity
    {
        int count = 1;
        private MobileBarcodeScanner scanner;
        private TextView tv_url;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);

            //初始化扫描仪,不然会报错空引用
            MobileBarcodeScanner.Initialize(Application);

            Button btn_scanner = FindViewById<Button>(Resource.Id.MyButton);
            tv_url = FindViewById<TextView>(Resource.Id.tv_url);
            btn_scanner.Click +=async delegate {
                //也可以在单击事件中初始化
                //MobileBarcodeScanner.Initialize(Application);

                var scanner = new              ZXing.Mobile.MobileBarcodeScanner();
                //异步获取二维码中的url
                var result = await scanner.Scan();
                if (!string.IsNullOrEmpty(result.Text))
                {
                    ScanResultHandle(result);
                }
            };
        }
        /// <summary>
        /// 获取扫描结果的处理
        /// </summary>
        private void ScanResultHandle(ZXing.Result result)
        {
            string url = result.Text;
            if (!string.IsNullOrEmpty(url))
            {
                tv_url.Text = "扫描结果" + result.Text;
            }
            else
            {
                tv_url.Text = "扫描取消";
            }
        }
    }

虽然这个扫描界面非常干净,但是对于一些扫码界面来说这显得十分低级,微信,qq,支付宝那些扫码界面做的就非常完美,但是可以通过自定义扫码界面来美化这个扫码的界面,下次有时间再写吧,今天太晚了。 

posted @   张林-布莱恩特  阅读(398)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
博客统计by flagcounter:
点击右上角即可分享
微信分享提示