Xamarin工程中调用Android AAR

最近想尝试下用Xamarin来写Android app,通过调用Android Barcode SDK(AAR文件)创建一个简单的条形码读取工具。

绑定Android AAR文件

基本步骤

参考Binding an AAR,基本步骤如下:

  1. 创建一个Java Bindings Library工程。
  2. 添加Android AAR文件。
  3. 设置正确的Build Action。
  4. 选择目标框架。
  5. 编译工程。

DynamsoftBarcodeReader.aar转换成DBRAndroid.dll

运行Visual Studio 2015,创建工程Bindings Library (Android).

xamarin android project

下载SDK package for Android。把DynamsoftBarcodeReader.aar拖入工程中。Build Action设置成LibraryProjectZip:

xamarin android aar

编译工程可以生成DBRAndroid\bin\Debug\DBRAndroid.dll

创建简单的Android Barcode Reader

新建一个Android工程,把刚才生成的DBRAndroid.dll添加到Reference中:

xamarin android reference

双击DBRAndroid可以看到具体的类,成员以及方法:

xamarin android reference class

我这里比较偷懒,只是为了测试接口,所以就放了一张QR的图到drawable中。

如何用C#把Drawable转换成Bitmap?

使用Java的时候,代码是这样的:

Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_resource);

用C#有一点不同:

Bitmap image = BitmapFactory.DecodeResource(Resources, Resource.Drawable.qr);

如何读取Barcode?

引入命名空间:

using Com.Dynamsoft.Barcode;

调用接口:

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
            Button button = FindViewById<Button>(Resource.Id.MyButton);
 
            button.Click += delegate {
                Bitmap image = BitmapFactory.DecodeResource(Resources, Resource.Drawable.qr);
                BarcodeReader barcodeReader = new BarcodeReader("license");
                ReadResult result = barcodeReader.ReadSingle(image, Barcode.QrCode);
 
                button.Text = string.Format("{0} clicks! barcode result: {1}", count++, result.Barcodes[0].DisplayValue);
            };
        }

 

源码

https://github.com/yushulx/xamarin-aar

 

转载于:https://my.oschina.net/yushulx/blog/812776

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值