Xamarin工程中调用Android AAR

怎甘沉沦 提交于 2019-11-30 22:32:29

最近想尝试下用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).

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

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

创建简单的Android Barcode Reader

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

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

我这里比较偷懒,只是为了测试接口,所以就放了一张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

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!