VS2017 Xamarin扫描二维码并跳转网页

article/2025/10/23 22:03:59

【本文章并非完全原创,是结合网上查询的各种资料来进行整合改造,成为可以使用的代码】
本文要实现的功能为,扫描一个二维码,读出扫描结果,如果结果中含有http://则会自动打开浏览器跳转页面。
内容为百度地址
(该二维码扫描内容为:http://www.baidu.com)
1、打开VS2017创建一个Android程序(这里就不赘述了)。
新建安卓应用
2、配置所需权限CAMERA
配置所需权限
3、更改Main.axml文件,大概效果图如下:
页面预览图

axml代码如下:
【注意,如果完全直接复制我的代码可能无法运行,因为里面有图片,有自定义字段,需要自行修改】

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#FFFFFF"><LinearLayout
        android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextView
            android:id="@+id/tv1"android:text="养殖二维码产品追溯"android:textSize="30px"android:textColor="@color/black"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20px"android:layout_marginBottom="8px"android:layout_gravity="center_horizontal" /></LinearLayout><View
        android:layout_height="1px"android:layout_width="match_parent"android:background="#66CCFF" /><LinearLayout
        android:orientation="horizontal"android:layout_height="wrap_content"android:layout_width="match_parent"android:layout_weight="1"android:background="#00000000"><LinearLayout
            android:orientation="vertical"android:layout_height="match_parent"android:layout_width="match_parent"android:layout_weight="1"android:gravity="center"><ImageButton
                android:id="@+id/register"android:src="@drawable/gxscxmcode"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:background="#00000000" /><TextView
                android:id="@+id/tv1"android:text="追溯码查询"android:textColor="@color/black"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="2px"android:layout_marginBottom="2px"android:layout_gravity="center_horizontal" /></LinearLayout><LinearLayout
            android:orientation="vertical"android:layout_height="match_parent"android:layout_width="match_parent"android:layout_weight="1"android:gravity="center"><ImageButton
                android:id="@+id/register"android:src="@drawable/search128"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:background="#00000000" /><TextView
                android:id="@+id/tv1"android:text="基础信息查询"android:textColor="@color/black"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="2px"android:layout_marginBottom="2px"android:layout_gravity="center_horizontal" /></LinearLayout></LinearLayout><LinearLayout
        android:orientation="horizontal"android:layout_height="wrap_content"android:layout_width="match_parent"android:layout_weight="1"android:gravity="center"android:background="#00000000"><LinearLayout
            android:orientation="vertical"android:layout_height="match_parent"android:layout_width="match_parent"android:layout_weight="1"><ImageButton
                android:id="@+id/register"android:src="@drawable/camera_128"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:background="#00000000" /><TextView
                android:id="@+id/tv1"android:text="拍照取证"android:textColor="@color/black"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="2px"android:layout_marginBottom="2px"android:layout_gravity="center_horizontal" /></LinearLayout><LinearLayout
            android:orientation="vertical"android:layout_height="match_parent"android:layout_width="match_parent"android:layout_weight="1"><ImageButton
                android:id="@+id/register"android:src="@drawable/information_128"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:background="#00000000" /><TextView
                android:id="@+id/tv1"android:text="关于"android:textColor="@color/black"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="2px"android:layout_marginBottom="2px"android:layout_gravity="center_horizontal" /></LinearLayout></LinearLayout><View
        android:layout_height="1px"android:layout_width="match_parent"android:background="#66CCFF" /><LinearLayout
        android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:background="#00000000"><TextView
            android:id="@+id/tv1"android:text="V:1.0"android:textColor="@color/black"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="12px"android:layout_marginBottom="2px"android:layout_gravity="center_horizontal" /><TextView
            android:id="@+id/tv1"android:text="云之彼端cbl"android:textSize="30px"android:textColor="@color/black"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="2px"android:layout_marginBottom="12px"android:layout_gravity="center_horizontal" /></LinearLayout>
</LinearLayout>

4、创建扫描界面
扫描界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><Button
        android:id="@+id/button_scan"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="@string/button_scan" /><TextView
        android:id="@+id/barcode_format"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center" /><TextView
        android:id="@+id/barcode_data"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center" />
</LinearLayout>

5、管理NuGet程序包
需要引用Zxing.Net.Mobile,原本我查询网上,按照他们的方式,发现调用后总是有问题。才发现这个版本需要一些依赖项的,除此之外,还必须先引用Xamarin.Android.Support.v4。
【注意:原本我的程序是参考了这个网址(但是没有实现我的效果):http://designbased.net/2016/12/xamarin-android-qr-code-二维码扫描示例/】
需要安装的NuGet包

6、MainActivity.cs代码
主要功能为点击按钮,跳到扫描界面。

using Android.App;
using Android.Widget;
using Android.OS;namespace Gxscxm
{[Activity(Label = "产品追溯", MainLauncher = true, Icon = "@drawable/GxscxmCode")]public class MainActivity : Activity{protected override void OnCreate(Bundle bundle){base.OnCreate(bundle);//不显示标题this.RequestWindowFeature(Android.Views.WindowFeatures.NoTitle);SetContentView(Resource.Layout.Main);ImageButton btn = FindViewById<ImageButton>(Resource.Id.register);btn.Click += Btn_Click;}private void Btn_Click(object sender, System.EventArgs e){StartActivity(typeof(ScanActivity));}}
}

7、创建ScanActivity.cs并写上代码

        private TextView _barcodeFormat, _barcodeData;protected override void OnCreate(Bundle savedInstanceState){base.OnCreate(savedInstanceState);// Create your application hereSetContentView(Resource.Layout.scan);//初始化扫描仪,不然会报错空引用MobileBarcodeScanner.Initialize(Application);_barcodeFormat = FindViewById<TextView>(Resource.Id.barcode_format);_barcodeData = FindViewById<TextView>(Resource.Id.barcode_data);var button = FindViewById<Button>(Resource.Id.button_scan);button.Click += Button_Click;}

异步扫描

        private async void Button_Click(object sender, EventArgs e){try{var opts = new MobileBarcodeScanningOptions{PossibleFormats = new List<BarcodeFormat>{BarcodeFormat.CODE_128,BarcodeFormat.EAN_13,BarcodeFormat.EAN_8,BarcodeFormat.QR_CODE}};opts.CharacterSet = "";var scanner = new MobileBarcodeScanner();//不使用自定义界面scanner.UseCustomOverlay = false;scanner.FlashButtonText = "识别";scanner.CancelButtonText = "取消";//设置上下提示文字scanner.TopText = "请将条形码对准方框内";scanner.BottomText = "确认后按下右下角识别按钮";var result = await scanner.Scan(opts);if (!string.IsNullOrEmpty(result.Text)){ScanResultHandle(result);//若扫描结果包含https:// 或者 http:// 则跳转网页if (result.Text.Contains("https://") || result.Text.Contains("http://")){Android.Net.Uri uri = Android.Net.Uri.Parse(result.Text);Intent intent = new Intent(Intent.ActionView, uri);StartActivity(intent);Finish();}}_barcodeFormat.Text = result?.BarcodeFormat.ToString() ?? string.Empty;_barcodeData.Text = result?.Text ?? string.Empty;}catch (Exception ex){throw ex;}}
        /// <summary>/// 获取扫描结果的处理/// </summary>private void ScanResultHandle(ZXing.Result result){string url = result.Text;if (!string.IsNullOrEmpty(url)){_barcodeFormat.Text = "扫描结果" + result.Text;}else{_barcodeFormat.Text = "扫描取消";}}

至此,整个程序方可运行。
我这里选择的是真机调试,测试型号为:HTC D816w (Android 5.0 - API 21).以下为源码下载地址:

http://download.csdn.net/download/qq331059279/10032263


http://chatgpt.dhexx.cn/article/rToEgigi.shtml

相关文章

Fiori 实现在网页端调用摄像头扫描二维码进行识别

我们在UI5官方文档上进行搜索Scan,是只能找到一个BarcodeScanner的&#xff0c;这个API是无法实现我们这个需求的&#xff0c;所以如果有朋友收到这种需求&#xff0c;不想做的情况下&#xff0c;是可以推脱一下&#xff0c;把问题抛给SAP的&#xff08;笑&#xff09;。既然写…

扫描二维码登录原理

手机扫码二维码实现登录某个网站的操作过程为&#xff0c;手机登录某个APP&#xff0c;利用“扫一扫”功能扫描网页上的二维码&#xff0c;扫描成功后&#xff0c;提示“登录网页版XX”&#xff0c;同时网页上显示“成功扫描 请在手机点击确认以登录”&#xff0c;手机端点击“…

【H5扫描二维码】

H5调用摄像头识别二维码-3种方法 一.使用html5-qrcode实现二维码扫描1).下载html5-qrcode2).使用 二.使用zxing/library实现二维码扫描1).下载zxing/library2).使用 三. 使用jsQR实现二维码扫描1).使用父组件直接引用mumu-getQrcode组件 vue2中使用jsQR、zxing/library、html5-…

二维码软件如何扫描二维码打开网页

在平常生活中,我们扫描二维码付款或者扫描二维码查看某品牌的网站这些都是我们经过扫描二维码跳转到了对方的网站网页页面内容,在使用中琅二维码软件制作时,我们可以先将需要跳转的网页保存在一个文档中,然后作为二维码内容添加进二维码即可。 一、制作单个跳转网页的二维…

扫描二维码后可以自动跳转到网页

现在我们的生活中随处可见二维码的身影&#xff0c;扫码付款&#xff0c;扫描进入小程序&#xff0c;扫码关注等等。二维码可以存储各种信息&#xff0c;主要包括网址、名片、文本信息、特定代码等。今天跟大家分享使用条码软件生成二维码&#xff0c;扫描后可以跳转到网址链接…

厉害了网页扫码,所有方法都给你总结到这了,赶紧收藏

最近做一个项目&#xff0c;要通过扫一扫查询对应的信息&#xff0c;由于现在已经有一部分二维码已被生成&#xff0c;为了兼顾已生成的二维码&#xff0c;所以需要使用网页的扫一扫功能去完成项目。 项目使用技术栈&#xff1a;vue2 方案一、js 原生 热心的同事帮我已经找好…

怎样测试手机的流量

在找过GT后 我们发现用路由器测试比较方便&#xff0c;可以把所有的终端都连到路由器上&#xff0c;查看各端的流量

流量过小如何做A/B测试

AB测试对于产品和运营优化的重要性有目共睹。为了能更快的得到试验结果&#xff0c;试验流量越大越好。但是当流量不够的时候怎么办呢&#xff1f;小流量AB测试能不能做&#xff1f;能&#xff01;下面有多个节约流量的方法。 一、消除异常数据的影响 例如&#xff1a;当点击…

无线专项测试--流量测试(下)

这篇文章主要是想介绍下流量专项测试的另外一种方法tcpdumpWireshark抓包测试法。 在后台系统的开发和测试中&#xff0c;借助工具抓取网络包来进行网络层的分析是一种非常常用的技术手段&#xff0c;常用的抓包工具有Windows下的Wireshark工具和Linux下的tcpdump。由于android…

android性能测试 app 实时流量获取

下面介绍几种获取app流量的统计规则&#xff1a; 分析方法D ①如何获取uid? 1.先获取进程pid (adb shell ps |findstr 包名) 2.进入到proc/pid/status 文件中 C:\Users\chenhui>adb shell PD1816:/ $ cd proc/ PD1816:/proc $ cd 20814 PD1816:/proc/20814 $ cd status /s…

安卓app测试之流量监控

一、查看PID 通过ps命令查看&#xff1a;ps | grep packageName 案例&#xff1a;adb shell "ps | grep tv.danmaku.bili" adb shell "ps | grep tv.danmaku.bili$" 二、查看UID 1、通过查看packages.list adb shell cat /data/system/packages.list |…

Android的app性能测试--流量

测试场景&#xff1a;在手机上面点击想要测试的界面或者一组场景&#xff0c;然后查看用了多少流量。 方法一&#xff1a;使用流量测试的工具&#xff1a;1.tcpdump抓包 2 wireshark进行分析 tcpdump介绍&#xff1a;tcpdump是一款用于截取网络分组&#xff0c;并输出分组内容的…

APP性能测试:启动时间、CPU、耗电量、流量、内存

app的启动与停止 启动app的命令&#xff1a;adb shell am start -n packageName/activity 查看被测应用app的包名和活动名activity的命令&#xff1a; adb shell "dumpsys window | grep mCurrentFocus" 这里以调出系统通讯录为例。 查看app启动时间命令&#x…

Charles 流量配置(弱网测试)、断点调试

一、流量配置 流量配置主要是用来检测软件&#xff08;APP&#xff09;在不同的网络环境下的一个表现&#xff0c;例如出现丢包闪退等情况. 流量配置主要有以下四步&#xff1a; 在 Charles 窗口中点击菜单 “Proxy” &#xff0c;选择 "Throttle Setting" 进行网络…

APP测试内容 -- 流量测试常见测试方法

APP测试内容 -- 流量测试常见测试方法 大部分摘自&#xff1a;https://blog.csdn.net/baidu_21833433/article/details/63255182#commentBox 一、Android自带统计功能&#xff08;统计流量总数值&#xff09; proc/uid_stat/{UID}/tcp_snd和tcp_rcv UID是每个app安装时候分配的…

app性能测试:(三)流量监控

app性能测试&#xff1a;&#xff08;三&#xff09;流量监控 下面对流量监控进行分析&#xff1a; 获取进程ID指令 adb shell “ps | grep 包名”获取进程ID流量adb shell cat /proc/pid/net/dev pid换成第一步获取到的进程ID receive是指当前进程接收的数据&#xff0c;tra…

APP的流量测试(android和iOS的流量测试)

1、准备 性能测试工具—fiddler、电脑、Google浏览器、手机、无线&#xff08;手机需要和电脑在一个网段里&#xff09; 2、知识点 1&#xff09;掌握fiddler工具安装&#xff08;这个相对比较简单&#xff0c;大家百度进行安装即可&#xff09; 也可以访问这个网页进行下载…

自动化测试之流量回放技术

流量回放近几年一直是大家热衷讨论的话题&#xff0c;具体的效果智者见智。文章作者也是在技术工程领域有丰富的实践经验&#xff0c;推荐一看。 本篇背景是另外一同事朋友&#xff0c;最近在利用流量回放技术应用在服务端接口自动化测试方面&#xff0c;还在各部门全力推进阶段…

数据流测试

一、基本概念 定义节点 -- DEF(v,n) 使用节点 -- USE(v,n) 谓词使用 -- P-use 计算使用 -- C-use 定义-使用路径 -- du-path 定义-清除路径 -- dc-path 全定义覆盖准则&#xff1a;测试路径需要覆盖所有定义点和任意一个使用点&#xff0c;用dc-path扩展…

app性能测试--流量

测试场景&#xff1a;在手机上面点击想要测试的界面或者一组场景&#xff0c;然后查看用了多少流量。 方法一&#xff1a;使用流量测试的工具&#xff1a;1.tcpdump抓包 2 wireshark进行分析 tcpdump介绍&#xff1a;tcpdump是一款用于截取网络分组&#xff0c;并输出分组内容的…