Xamarin的独特之处在于,它提供了一种单一的语言—C#、类库和运行时,可以跨iOS、Android和Windows Phone这三种移动平台运行(Windows Phone的母语已经是C#),同时仍然编译本机(非解释型)应用程序,即使对于要求很高的游戏,这些应用程序的性能也足够高。
这些平台都有不同的特性集,并且在编写本机应用程序的能力上也各不相同——也就是说,将应用程序编译为本机代码并与底层Java子系统流畅地进行互操作。例如,有些平台只允许用HTML和JavaScript构建应用程序,而有些平台非常低级,只允许使用C/ c++代码。有些平台甚至不使用本机控件工具包。
Xamarin的独特之处在于,它结合了本地平台的所有功能,并添加了自己的一些强大功能,包括:
Xamarin提供两种商业产品:Xamarin。iOS和Xamarin.Android。它们都建在单声道,一个基于已发布的。net ECMA标准的。net框架的开源版本。Mono出现的时间几乎和。net框架本身一样长,并且可以在几乎所有可以想象的平台上运行,包括Linux、Unix、FreeBSD和Mac OS X。
在iOS, Xamarin的提前(AOT编译器编译Xamarin。iOSapplications directly to native ARM assembly code. On Android, Xamarin’s compiler compiles down to中间语言(白细胞介素),然后即时(准时制)在应用程序启动时编译为本机程序集。
在这两种情况下,Xamarin应用程序都利用了一个运行时来自动处理诸如内存分配、垃圾收集、底层平台互操作等事情。
Xamarin.表格is a framework that allows developers to rapidly create cross platform user interfaces. It provides it's own abstraction for the user interface that will be rendered using native controls on iOS, Android, Windows, or Windows Phone. This means that applications can share a large portion of their user interface code and still retain the native look and feel of the target platform.
Xamarin.表格allows for rapid prototyping of applications that can evolve over time to complex applications. Because Xamarin.Forms applications are native applications, they don't have the limitations of other toolkits such as browser sandboxing, limited APIs, or poor performance. Applications written using Xamarin.Forms are able to utilize any of the API’s or features of the underlying platform, such as (but not limited to) CoreMotion, PassKit, and StoreKit on iOS; NFC and Google Play Services on Android; and Tiles on Windows. In addition, it's possible to create applications that will have parts of their user interface created with Xamarin.Forms while other parts are created using the native UI toolkit.
表单应用程序的架构与传统的跨平台应用程序相同。最常见的方法是使用便携式图书馆或共享项目存放共享代码,并创建将使用共享代码的特定于平台的应用程序。
在Xamarin.Forms中创建用户界面有两种技术。第一种技术是完全使用C#源代码创建UI。第二种技术是使用可扩展应用程序标记语言(XAML),一种用于描述用户界面的声明性标记语言。有关XAML的更多信息,请参见XAML基础知识.
要首先开始开发Xamarin应用程序,您需要安装Visual Studio或Xamarin Studio,并确保包含所有必要的Xamarin组件。在我们的示例中,我们将使用Visual Studio向您展示如何开发Xamarin应用程序和使用我们的SDK。导航到此链接逐步阅读如何下载和安装VisualStudioforXamarin应用程序。
在下面的小节中,我们将一步步地解释我们的示例应用程序是如何开发的。
打开Visual Studio并遵循以下步骤:
1.去文件->新建->项目。
2.创造空白应用程序(Android)。
加载新项目时,添加对XamarinDataManLibrary.dll文件的引用。
创建一个空白应用程序后,创建所有你将使用的资源(图标,图像,样式,布局等)。必威随行版官网你可以从我们的样品中复制。
接下来右键单击你的项目文件,然后单击属性,然后去Android Manifest部分。
设置清单文件(应用程序名称、应用程序图标、最低和最高android版本),确保为此应用程序启用摄像头权限。
在我们设置了一些必要的属性之后,我们可以创建ScannerActivity哪一个是最重要的MainLauncher并继承一些接口。
[Activity(Label=“@string/app_name”,MainLauncher=true,Icon=“@drawable/ic_launcher”,ScreenOrientation=ScreenOrientation.肖像)]公共类扫描活动:Activity,IOnConnectionCompletedListener,IReaderDeviceListener,Android.Support.V4.app.ActivityCompat.IOnRequestPermissionsResultCallback{private static int REQUEST_PERMISSION_CODE=12322;private ListView ListView result;private TextView tvConnectionStatus;private Button btnScan;private RelativeLayout rlPreviewContainer;private ImageView ivPreview;private ListresultList;private JavaList>resultListData;private resultlistview适配器resultListAdapter;public static int-listViewResultSelectedItem=-1;private bool isScanning=false;private static ReaderDevice ReaderDevice;private enum DeviceType{MX,PHONE_-CAMERA}private static bool isDevicePicked=false;private static DeviceType param_DeviceType=DeviceType.PHONE\u CAMERA;private static bool DialogExisted=false;private static string selectedDevice=“”;private bool listeningForUSB=false;受保护覆盖void OnCreate(Bundle savedInstanceState){base.OnCreate(savedInstanceState);SetContentView(Resource.Layout.activity_scanner)。。。。。。
在这里,我们将定义一些UI元素和变量,稍后将在活动中使用:
tvConnectionStatusTextView是一个用于显示当前连接状态的UI元素;
listViewResult- ListView一个UI元素,用于读取结果;
btnScan按钮一个将触发的UI元素readerDevice。StartScanning或readerDevice。StopScanning;
rlPreviewContainerViewGroup (RelativeLayout)容器用于相机预览;
ivPreview- ImageView控件,匹配大小作为其父控件,显示预览或扫描会话的最后一帧;
读设备-显示MX设备或手机摄像头的cmbSDK对象取决于我们的配置;
每次活动开始时,我们都会呼叫initDevice我们在其中配置读卡器设备对象。
如果我们想使用MX设备进行扫描,我们可以使用:
readerDevice = GetMXDevice(这个);if (!listeningForUSB) {readerDevice.StartAvailabilityListening();listeningForUSB = true;}
MX设备的可用性可能会随着设备的转动而改变在…上或关,或者如果USB线连接或断开连接,由IReaderDeviceListener界面
如果我们想将读取设备配置为手机摄像头,我们可以使用:
readerDevice = GetPhoneCameraDevice(this, CameraMode。NoAimer PreviewOption。违约,rlPreviewContainer);
CameraMode参数为typeCameraMode(定义在CameraMode.java),它接受下列值之一:
被动瞄准器:初始化读卡器,使其使用被动瞄准器。被动瞄准器是附在移动设备或移动设备外壳上的附件,使用移动设备的内置LED闪光灯作为光源投射瞄准模式。在此模式下,设备屏幕上不会显示直播预览,因为会有一个瞄准模式被投射。
前摄像头初始化读取器以使用设备的移动前置摄像头,如果可用(不是所有移动设备都有前置摄像头)。这是一种不寻常但可能的配置。大多数前置摄像头没有自动对焦和照明,提供的图像分辨率明显较低。这个选项应该小心使用。在此模式下,照明不可用。
上述所有模式都为读卡器提供以下默认设置:
基于选定的模式,将设置以下附加选项和行为:
的PreviewOption的类型PreviewOption(定义在PreviewOption.java),并用于更改读卡器的默认值或覆盖从选定对象派生的默认值CameraMode.在传递参数时,可以通过OR-ing来指定多个选项。可供选择的选项有:
配置后读设备我们需要连接到设备。
在我们接通之前ReaderDeviceListener对象的设置以接收事件:
readerDevice.SetReaderDeviceListener(这个);
此外,您可以通过以下方式从阅读器发送最后触发的图像和SVG:
readerDevice.EnableImage(真);readerDevice.EnableImageGraphics(真);
然后我们可以连接到:
readerDevice.Connect(这个);
将被调用的事件有:
public void OnConnectionStateChanged(ReaderDevice reader)
如果在尝试连接时出现错误,则该错误将作为参数抛出OnConnection已完成方法,否则,如果没有发生错误,则错误参数为零.
如果连接成功,则语句读者。ConnectionState = = ConnectionState。连接会是真的。
有两种API方法可用于更改用于配置连接设备的某些公共属性,当ConnectionState是连接。
例如,如果移动相机被用作读设备有默认情况下没有启用符号。必须启用要与一起使用的符号SetSymbologyEnabledAPI方法:
readerDevice.SetSymbologyEnabled(象征。C128,真的,null);readerDevice.SetSymbologyEnabled(象征。Datamatrix,真的,null);readerDevice.SetSymbologyEnabled(象征。UpcEan,真的,null);readerDevice.SetSymbologyEnabled(象征。Qr,真的,null);
你可以通过以下方式直接向连接的设备发送命令:
readerDevice.DataManSystem。SendCommand(“设置标志。MICROPDF417”);
有了正确配置的阅读器,现在就可以扫描条形码了。可以通过调用startScanning方法从你读设备对象
接下来会发生什么取决于读设备以及它是如何配置的,但一般来说:
在下列情况之一时,扫描停止:
当条形码被成功解码时(第一种情况),您将收到一个ReadResults中的iterable结果集合对象读设备侦听器方法。
public void OnReadResultReceived(ReaderDevice reader, ReadResults results) {listViewResultSelectedItem = -1;resultList.Clear ();resultListData.Clear ();ivPreview.SetImageBitmap(空);如果结果。= null && results.SubResults. count > 0) {foreach (ReadResult subResult in results.SubResults) {if (subResult. isgoodread) {resultList.Add(subResult);JavaDictionary(); / /创建新的JavaDictionary项。Add(“resultText subResult.ReadString);symbol = subResult.Symbology; if (sym != null) item.Add("resultType", subResult.Symbology.Name); else item.Add("resultType", "UNKNOWN SYMBOLOGY"); resultListData.Add(item); listViewResultSelectedItem = resultListData.Count - 1; } else { resultList.Add(subResult); JavaDictionary item = new JavaDictionary(); item.Add("resultText", "NO READ"); item.Add("resultType", ""); resultListData.Add(item); listViewResultSelectedItem = resultListData.Count - 1; } if (subResult.Image != null) { ivPreview.SetImageBitmap(renderSvg(subResult.ImageGraphics, subResult.Image)); } else { if (subResult.ImageGraphics != null) { ivPreview.SetImageBitmap(renderSvg(subResult.ImageGraphics, ivPreview.Width, ivPreview.Height)); } else ivPreview.SetImageBitmap(null); } } } else if (results.Count > 0) { ReadResult result = results.GetResultAt(0); if (result.IsGoodRead) { resultList.Add(result); JavaDictionary item = new JavaDictionary(); item.Add("resultText", result.ReadString); Symbology sym = result.Symbology; if (sym != null) item.Add("resultType", result.Symbology.Name); else item.Add("resultType", "UNKNOWN SYMBOLOGY"); resultListData.Add(item); listViewResultSelectedItem = resultListData.Count - 1; } else { resultList.Add(result); JavaDictionary item = new JavaDictionary(); item.Add("resultText", "NO READ"); item.Add("resultType", ""); resultListData.Add(item); listViewResultSelectedItem = resultListData.Count - 1; } if (result.Image != null) { ivPreview.SetImageBitmap(renderSvg(result.ImageGraphics, result.Image)); } else { if (result.ImageGraphics != null) { ivPreview.SetImageBitmap(renderSvg(result.ImageGraphics, ivPreview.Width, ivPreview.Height)); } else ivPreview.SetImageBitmap(null); } } isScanning = false; btnScan.Text = "START SCANNING"; resultListAdapter.NotifyDataSetChanged(); }
结果。图像扫描过程中的最后一帧是否会显示在ivPreviewImageView,和结果。图像Graphics是用于在图像上定位条形码的SVG图像。
在ScannerActivity我们覆盖OnPause和原这样我们就可以断开连接和StopAvailabilityListening当我们从这个活动中导航或破坏这个活动时,释放所有的连接。
受保护的覆盖void OnPause(){base.OnPause();if(readerDevice!=null){readerDevice.Disconnect();}}}受保护的覆盖void OnStop(){if(readerDevice!=null)尝试{readerDevice.stopAvailabilityListing();}catch(System.Exception e){}listeningForUSB=false;base.OnStop();}
请记住,在某些情况下,设备可能会由于电池电量不足或手动断开电缆而断开。
如果你打算使用cmbSDK要使用智能手机或平板电脑(没有MX移动终端)进行移动扫描,SDK需要安装许可密钥。没有许可密钥,SDK仍然可以运行,但扫描结果会被模糊化(SDK会用星号字符随机替换扫描结果中的字符)。
联系您的Cognex销售代表,了解如何获得许可证密钥,包括可用于评估SDK 30天的试用许可证。
获得许可证密钥后,有两种方法可以在应用程序中添加许可证密钥。
第一个是将它作为一个元标签添加到manifest文件中的application标签中:
< android应用程序:label = " XamarinDataManSample " >… . android:value="YOUR_MX_MOBILE_LICENSE"/>
第二种方法是直接从代码实现激活。当你创建readerDevice时,在构造函数中将许可密钥设置为输入参数:
...readerDevice = GetPhoneCameraDevice(this, CameraMode。NoAimer PreviewOption。违约,rlPreviewContainer,"YOUR_MX_MOBILE_LICENSE");
扫描成功后,请使用以下代码添加“哔”声:
它在MX设备上播放音频信号。
连接和断开MX设备的过程是通过Connect/Disconnect方法完成的
将尝试连接到设备。当Connect ()执行时,我们得到行动的状态OnConnection已完成侦听器
OnConnectionCompleted(ReaderDevice reader,Throwable error){if(error != null){//处理错误readerDisconnected();} }
将从MX设备断开连接。
例子:
readerDevice = ReaderDevice.GetMXDevice (mContext);readerDevice.StartAvailabilityListening ();readerDevice.SetReaderDeviceListener(这个);readerDevice.EnableImage(真正的);readerDevice.Connect(这个);...
AvailabilityChanged(ReaderDevice reader){if(reader.GetAvailability()==Availability.Availability){ReaderDevice.Connect(this);}。。。
成功扫描的结果可以返回一个图像。这是在一次成功的扫描中解析的最后一帧。要启用/禁用这个功能,我们可以使用API方法:
readerDevice=readerDevice.GetPhoneCameraDevice(mContext、CameraMode.NoAimer、PreviewOption.Defaults、rlMainContainer);readerDevice.EnableImage(true);readerDevice.Connect(此);
成功扫描的结果可以返回SVG图像图形。这是成功扫描中解析的最后一帧。要启用/禁用此功能,我们可以使用API方法:
readerDevice = readerDevice。GetPhoneCameraDevice (mContext CameraMode。NoAimer PreviewOption。违约,rlMainContainer); readerDevice.EnableImageGraphics(true); readerDevice.Connect(this);
在我们可以连接到MX设备之前,我们需要知道是否有一个可用的任务。
它回来了可利用性对象,该对象可以是可用,不可用或未知的
当有USB连接到我们的智能手机时,MX设备是可用的。
AvailabilityChanged(ReaderDevice reader){if(reader.GetAvailability()==Availability.Availability){ReaderDevice.Connect(this);}。。。
如果我们想检查MX设备的电池水平,我们可以使用
检索读卡器设备的当前电池百分比级别作为中的输入参数OnDeviceBatteryLevelReceived侦听器方法
public void OnConnectionStateChanged(ReaderDevice reader) {if (ReaderDevice reader);ConnectionState = = ConnectionState。连接){ reader.GetDeviceBatteryLevel(this); ......... } } public void OnDeviceBatteryLevelReceived(ReaderDevice p0, int p1, Throwable p2) { int batteryLevel = p1; }
如果我们要检查MX设备的所有灯是否都开着或关着
检索读数装置的灯是否作为输入参数打开或关闭OnLightsOnCompleted侦听器方法
要打开或关闭MX设备灯,我们使用
public void OnConnectionStateChanged(ReaderDevice reader) {if (ReaderDevice reader);ConnectionState = = ConnectionState。连接){ reader.IsLightsOn(this); ......... } } public void OnLightsOnCompleted(ReaderDevice p0, Java.Lang.Boolean p1, Throwable p2) { bool lightsON = p1.BooleanValue(); }
如果我们想要检查一些符号是否被启用或禁用,我们可以使用
检索符号是否作为中的输入参数启用或禁用OnSymbologyEnabled侦听器方法
为了启用特定的符号,我们使用
public void OnConnectionStateChanged(ReaderDevice reader) {if (ReaderDevice reader);ConnectionState = = ConnectionState。连接){ reader.IsSymbologyEnabled(Symbology.Azteccode, this); ......... } } public void OnSymbologyEnabled(ReaderDevice p0, Symbology p1, Java.Lang.Boolean p2, Throwable p3) }
将MX Device配置设置重置为默认使用
在里面OnResetConfigCompleted我们可以通过检查Throwable输入参数error是否为null来检查重置是否成功
public void OnResetConfigCompleted(ReaderDevice p0, Throwable p1) {if (p1 !WriteLine(“重启成功。Error: " + p1.Message);}
注册侦听器函数的步骤可用性改变,OnConnectionStateChanged和OnReadResultReceived在尝试连接到阅读器设备之前,我们需要调用这个方法
public void OnAvailabilityChanged(ReaderDevice reader) {if (reader. getavailability () == availabilityavailable) {ReaderDevice . connect (this);} }
连接状态已更改(ReaderDevice reader){if(reader.ConnectionState==ConnectionState.Connected){}否则if(reader.ConnectionState==ConnectionState.Disconnected){}
public void OnReadResultReceived(ReaderDevice reader, ReadResults results) {if (result . result, result . result);Count > 0) {ReadResult result = results.GetResultAt(0);if (result.IsGoodRead){…
例子
readerDevice=readerDevice.GetPhoneCameraDevice(mContext、CameraMode.NoAimer、PreviewOption.Defaults、rlMainContainer);SetReaderDeviceListener(此文件);readerDevice.Connect(此);
我们打电话之后readerDevice.SetReaderDeviceListener(IReaderDeviceListener listenerObject)方法和寄存器可用性改变监听器函数,我们可以启动/停止可用性监听
将开始侦听读卡器设备的可用性,并在每次可用性更改时触发侦听器功能
是否会停止收听阅读器设备的可用性
例子:
readerDevice=readerDevice.GetMXDevice(mContext);readerDevice.StartAvailabilityListening();SetReaderDeviceListener(此文件);readerDevice.Connect(此);
{if (readerDevice != null && readerDevice. value = null, readerDevice. value = null;ConnectionState = = ConnectionState。连接){ readerDevice.SetReaderDeviceListener(null); readerDevice.Disconnect(); } if (readerDevice != null) try { readerDevice.StopAvailabilityListening(); } catch (System.Exception e) { } base.Dispose(disposing); }
在我们连接和配置我们的阅读器和设置所有我们需要的设置之后,我们可以开始扫描过程。
开始扫描时,我们使用方法
当我们的阅读器成功扫描某些条形码时,扫描过程将停止,或者我们可以使用此方法手动停止扫描
停止扫描时,无论是使用成功扫描还是使用stopScanning()方法OnReadResultReceived在我们可以检查扫描结果的地方将调用Listener函数
public void OnReadResultReceived(ReaderDevice reader, ReadResults results) {if (result . result, result . result);Count > 0) {ReadResult result = results.GetResultAt(0);if (result.IsGoodRead) {symbol sym = result.Symbology;if (sym != null){电视符号。文本= sym.Name;} else{电视符号。Text = "未知符号";} tvCode。文本= result.ReadString;} else{电视符号。Text = "NO READ";tvCode。文本= " "; } ivPreview.SetImageBitmap(result.Image); } }
在下面的小节中,我们将一步步地解释我们的示例应用程序是如何开发的。
打开Visual Studio并执行以下步骤:
1.去文件->新建->项目。
2.创造空白应用程序(iPhone)。
加载新项目时,将引用添加到XamarinDataManLibrary.dll文件。
你的下一个开放Info.plist根据您的需要(应用程序名称、部署目标、主界面等)归档并设置一些项目属性。
这里重要的是为这个应用程序添加摄像头权限。在Visual Studio中没有从这里添加这个权限的选项。你得打开你的Info.plist文件,并添加以下行:
NSCameraUsageDescription 用于扫描的摄像机
同样,如果你使用MX Device作为读取器,请在Info.plist中添加以下协议:
UISupportDexternalAccessoryProtocols com.cognex.dmcc com.demo.data
的ViewController将是我们的第一个控制器主故事板.这里我们创建了一些UI元素和变量,稍后将在这个控制器中使用。
public partial class ViewController: UIViewController, ICMBReaderDeviceDelegate {protected ViewController(IntPtr句柄):base(句柄){//注意:这个.ctor不应该包含任何初始化逻辑。} CMBReaderDevice readerDevice;public bool isScanning = false;私人NSMutableArray tableData;私人MXResultsTableSource tableSource;public override void ViewDidLoad() {base.ViewDidLoad();...
lblConnection-为当前连接状态标记UI元素。
tableSource- UITableViewSource用于读取结果。
btnScan-按钮UI元素将触发StartScanning或StopScanning.
ivPreview-图片框元素,用于显示预览或扫描会话的最后一帧。
CMBReaderDevice-显示MX设备或手机摄像头的cmbSDK对象取决于我们的配置。
这里我们重写视图将出现方法来配置读取器设备对象。
如果我们想使用MX设备进行扫描,我们正在使用
readerDevice = CMBReaderDevice.ReaderOfMXDevice ();
MX设备的可用性可能会在设备打开或关闭时发生变化,或者USB电缆连接或断开时发生变化,并由ICMBReaderDeviceDelegate接口。我们将此接口设置为具有的读卡器设备的属性
readerDevice。WeakDelegate =;
让我们来听听这三件事:
public void DidReceiveReadResultFromReader(CMBReaderDevice reader, CMBReadResults readResults) public void AvailabilityDidChangeOfReader(CMBReaderDevice reader) public void ConnectionStateDidChangeOfReader(CMBReaderDevice reader)
如果我们想将读取器配置为移动相机。
readerDevice = CMBReaderDevice.ReaderOfDeviceCameraWithCameraMode (CDMCameraMode。NoAimer CDMPreviewOption。违约,ivPreview);
的CameraMode参数的类型CDMCameraMode,它接受下列值之一:
上述所有模式都为读卡器提供以下默认设置:
基于选定的模式,将设置以下附加选项和行为:
的previewOptions类型的参数(CDMPreviewOption)用于更改读取器的默认值或覆盖从选定对象派生的默认值CameraMode.在传递参数时,可以通过OR-ing来指定多个选项。可供选择的选项如下:
类型的最后一个参数UIView是可选的,用作摄影机预览的容器。如果参数保留为零,则将使用全屏预览。
配置后读设备我们需要连接到设备。
readerDevice.ConnectWithCompletion((error) => {if (error != null) {new UIAlertView("Failed to connect", error. ");Description, null, "OK", null).Show();}});
如果在尝试连接时出现错误,则会在回调函数中抛出错误作为参数。如果一切正常,错误参数将为空。
此函数将触发读卡器的连接状态方法。如果连接成功reader.ConnectionState==ConnectionState.Connected。
成功连接后,我们可以为ReaderDevice设置一些设置。ReaderDevice设置可以使用已包装的函数进行设置,也可以直接通过向配置的设备发送命令进行设置。
例如,如果移动相机被用作reader设备有默认情况下没有启用符号。必须启用要与一起使用的符号SetSymbology包装函数。
在这个例子中,我们启用了一些符号,并设置了设置来获取扫描进来的最后一帧预览图像视图.
readerDevice.SetSymbology (CMBSymbology。DataMatrix, true, (error) => {if (error != null) {System.Diagnostics.Debug. if (error !WriteLine("FALIED TO ENABLE [DataMatrix], ", error.LocalizedDescription);} });readerDevice.SetSymbology (CMBSymbology。Qr, true, (error) => {if (error != null) {System.Diagnostics.Debug. if (error !WriteLine("FALIED TO ENABLE [Qr], ", error.LocalizedDescription);} });readerDevice.SetSymbology (CMBSymbology。C128, true, (error) => {if (error != null) {System.Diagnostics.Debug. C128, true, (error) => {WriteLine("FALIED TO ENABLE [C128], ", error.LocalizedDescription);} });readerDevice.SetSymbology (CMBSymbology。UpcEan, true, (error) => { if (error != null) { System.Diagnostics.Debug.WriteLine("FALIED TO ENABLE [UpcEan], ", error.LocalizedDescription); } }); readerDevice.ImageResultEnabled = true; readerDevice.SVGResultEnabled = true; readerDevice.DataManSystem.SendCommand("SET IMAGE.SIZE 0");
有了正确配置的阅读器,现在就可以扫描条形码了。可以通过调用startScanning方法从你读设备对象
接下来发生的事情是基于阅读器设备的类型和它是如何被配置的,但一般来说:
在下列情况之一时,扫描停止:
当条形码被成功解码时(第一种情况),您将收到一个ReadResults中可迭代的结果集合对象读设备侦听器方法。
public void DidReceiveReadResultFromReader(CMBReaderDevice reader, CMBReadResults readResults) {btnScan。SetTitle(“开始扫描”,UIControlState.Normal);isScanning = false;tableData.RemoveAllObjects ();如果(readResults。= null && readResults.SubReadResults. length > 0) {tableData.AddObjects(readResults.SubReadResults);tvResults.ReloadData ();} else if (readResults.ReadResults.){tableData.Add(readResults.ReadResults[0]);} tableSource.SetItems (tableData);tableSource.displayResult (0); tvResults.ReloadData(); tvResults.SelectRow(NSIndexPath.FromRowSection(0,0), false, UITableViewScrollPosition.None); }
可能会出现设备因电池电量不足或手动断开电缆而断开的情况。这些病例可以被读卡器的连接状态回调的ICMBReaderDeviceDelegate.
注:的AvailabilityDidChangeOfReader方法在设备物理上不可用时也会被调用。这意味着(重新)连接是不可能的。的可用性属性读设备对象之前,尝试调用连接完成方法。
如果您计划使用cmbSDK来使用智能手机或平板电脑(没有MX移动终端)进行移动扫描,那么该SDK需要安装许可密钥。没有许可密钥,SDK仍然可以运行,尽管扫描结果会被混淆(SDK会用星号字符随机替换扫描结果中的字符)。
联系您的Cognex销售代表,了解如何获得许可证密钥,包括可用于评估SDK 30天的试用许可证。
在获得许可密钥后,有两种方式在应用中添加许可密钥。
第一个方法是将它作为一个键添加到特定于项目的值中Info.plist文件:
MX_MOBILE_LICENSE 您的许可密钥 . txt
第二种实现激活的方法是直接从代码中创建readerDevice:
....readerDevice = CMBReaderDevice.ReaderOfDeviceCameraWithCameraMode (CDMCameraMode。NoAimer CDMPreviewOption。违约,ivPreview,"YOUR_MX_MOBILE_LICE");
在portable项目中,我们创建一个页面(主页)在这里,我们为扫描页面和一个类(CameraPreview.cs)创建一个布局,该类将从视图控件继承,并具有一些属性和事件。
CameraPreview控件添加到主页:
...< x:网格名称= " gridCamera " > <当地:CameraPreview x: Name = " CameraPreview " SelectedDevice =“MobileCamera ResultReceived =“CameraPreview_ResultReceived”ConnectionStateChanged = " CameraPreview_ConnectionStateChanged " / > <标签x: Name = " lblStatus文本输入TextColor =“白色”=“断开”字形大小=“十一”VerticalOptions = "开始" HorizontalOptions =“结束”背景颜色="#ff4444" Margin="0,2,5,0" /> …
在里面安卓和iOS我们有具体的平台项目自定义渲染器(ScannerView.cs),我们可以在可移植项目中使用本地元素。
ViewRenderer在Android平台特定项目中:
受保护覆盖无效OnElementChanged(ElementChangedEventArgse){base.OnElementChanged(e);if(e.OldElement!=null | | | Element==null){return;}rlMainContainer=new RelativeLayout(上下文);rlMainContainer.SetMinimumHeight(50);rlMainContainer.SetMinimumWidth(100);rlMainContainer.LayoutParameters=新建RelativeLayout.LayoutParams.MatchParent,RelativeLayout.LayoutParams.MatchParent);ivPreview=新建图像视图(上下文);ivPreview.SetMinimumHeight(50);ivPreview.SetMinimumWidth(100);ivPreview.LayoutParameters=新建RelativeLayout.LayoutParameters(RelativeLayout.LayoutParams.MatchParent,RelativeLayout.LayoutParams.MatchParent);ivPreview.SetScaleType(ImageView.ScaleType.FitCenter);rlMainContainer.AddView(ivPreview);if(Control==null)SetNativeControl(rlMainContainer);MainActivity.instance.setActiveReader(控件,元素);}
在里面MainActivity在Android平台特定的项目中,我们处理的是reader设备对象(几乎与ScannerActivity为泽马林,安卓),从自定义渲染器,我们只需调用setActiveReader方法,并传递Control(原生RelativeLayout元素)和element(从MainPage初始化的CameraPreview对象)。
ViewRenderer在iOS平台具体项目:
OnElementChanged(ElementChangedEventArgs e) {base.OnElementChanged(e);if (e.o oldelement != null || Element == null) {return;} container = new UIView();ivPreview = new UIImageView();ivPreview。内容模式= UIViewContentMode.ScaleToFill;ivSVG = new UIImageView();ivSVG。内容模式= UIViewContentMode.ScaleToFill;container.AddSubview (ivPreview); container.AddSubview(ivSVG); ivPreview.Frame = new CoreGraphics.CGRect(0, 0, container.Frame.Size.Width, container.Frame.Size.Height); ivPreview.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth; ivSVG.Frame = new CoreGraphics.CGRect(0, 0, container.Frame.Size.Width, container.Frame.Size.Height); ivSVG.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth; if (Control == null) SetNativeControl(container); AppDelegate.instance.setActiveReader(Control, Element); }
在里面AppDelegate在iOS平台特定的项目中,我们处理的是读卡器设备对象(几乎与之类似)视图控制器为Xamarin.iOS)从自定义渲染器中,我们只需调用setActiveReader方法和传递控件(本机UIImageView元素)和元素(从主页初始化的CameraPreview对象)。
配置读取设备、连接设备、扫描条码和断开从设备是一样的,你可以在泽马林,安卓和Xamarin.iOS部分。