最近想尝试下用Xamarin来写Android app,通过调用Android Barcode SDK(AAR文件)创建一个简单的条形码读取工具。
绑定Android AAR文件
基本步骤
参考Binding an AAR,基本步骤如下:
- 创建一个Java Bindings Library工程。
- 添加Android AAR文件。
- 设置正确的Build Action。
- 选择目标框架。
- 编译工程。
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
来源:oschina
链接:https://my.oschina.net/u/1187419/blog/812776