xamarin.forms 使用ZXing扫描二维码

1. Android工程包含ZXing.Net.Mobile.Forms

 

2. 在C# 工程中, 包含ZXing.Net.Mobile

 

3. 在Android工程的MainActivity.cs中, 添加下面代码:

global::ZXing.Net.Mobile.Forms.Android.Platform.Init();

ZXing.Mobile.MobileBarcodeScanner.Initialize(this.Application);

using System;

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace TestScan.Droid
{
    [Activity(Label = "TestScan", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);
            global::ZXing.Net.Mobile.Forms.Android.Platform.Init();

            ZXing.Mobile.MobileBarcodeScanner.Initialize(this.Application);
            LoadApplication(new App());
        }
    }
}

 

4. 在C#页面工程中就可以这样调用扫码功能了:

private async void Button_Clicked(object sender, EventArgs e)
{    
    var scanner = new ZXing.Mobile.MobileBarcodeScanner();
    var result = await scanner.Scan();
    if (result != null)
        Console.WriteLine("Scanned Barcode: " + result.Text);
}

 

demo下载地址

 

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Xamarin.Forms 是一个跨平台的应用程序开发框架,可以用于开发 iOS、Android 和 Windows Phone 应用。在 Xamarin.Forms 中实现蓝牙扫描、配对、通讯,需要使用 Xamarin.Forms 的蓝牙插件,该插件可以让您在 Xamarin.Forms 中访问蓝牙功能。 以下是在 Xamarin.Forms 中实现蓝牙扫描、配对、通讯的步骤: 1. 安装蓝牙插件 使用 Xamarin.Forms 的蓝牙插件可以很容易地访问蓝牙功能。可以使用 NuGet 包管理器来安装蓝牙插件。 在 Visual Studio 中,选择项目,右键单击“管理 NuGet 包”,搜索“Xamarin.Forms.Bluetooth”,并安装。 2. 检查蓝牙权限 在使用蓝牙之前,需要检查应用程序是否具有蓝牙权限。可以使用 Xamarin.Essentials 库来检查权限。 在 Xamarin.Essentials 中,使用 Permissions 模块检查蓝牙权限。以下是代码示例: ``` var status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>(); if (status != PermissionStatus.Granted) { status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>(); if (status != PermissionStatus.Granted) { // Handle permission denied return; } } ``` 3. 扫描蓝牙设备 在 Xamarin.Forms 中,可以使用蓝牙插件的 CrossBluetoothLE.Current.Adapter 属性来扫描蓝牙设备。以下是代码示例: ``` var adapter = CrossBluetoothLE.Current.Adapter; adapter.ScanTimeout = TimeSpan.FromSeconds(10); adapter.DeviceDiscovered += (s, a) => { // Handle discovered devices }; await adapter.StartScanningForDevicesAsync(); ``` 4. 配对蓝牙设备 在 Xamarin.Forms 中,可以使用蓝牙插件的 CrossBluetoothLE.Current.Adapter 设备配对。以下是代码示例: ``` var device = // get the device to pair await device.ConnectAsync(); await device.PairAsync(); ``` 5. 连接蓝牙设备 在 Xamarin.Forms 中,可以使用蓝牙插件的 CrossBluetoothLE.Current.Adapter 设备连接。以下是代码示例: ``` var device = // get the device to connect await device.ConnectAsync(); ``` 6. 通讯 在连接了蓝牙设备后,可以使用蓝牙插件的 CrossDeviceBluetoothLE.Current.Device 属性来进行通讯。以下是代码示例: ``` var device = // get the connected device var service = await device.GetServiceAsync(serviceUuid); var characteristic = await service.GetCharacteristicAsync(characteristicUuid); await characteristic.WriteAsync(data); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值