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中间语言(白细胞介素),然后即时(JIT)在应用程序启动时编译为本机程序集。
在这两种情况下,Xamarin应用程序都利用一个运行时来自动处理诸如内存分配、垃圾收集、底层平台互操作等事情。
Forms是一个允许开发人员快速创建跨平台用户界面的框架。它为将使用iOS、Android、Windows或Windows Phone上的本机控件呈现的用户界面提供了自己的抽象。这意味着应用程序可以共享大部分用户界面代码,并且仍然保留目标平台的本地外观。
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.表格applications are architected in the same way as traditional cross-platform applications. The most common approach is to use便携式图书馆或共享项目存放共享代码,并创建将使用共享代码的特定于平台的应用程序。
在Xamarin.Forms中创建用户界面有两种技术。第一种技术是完全使用C#源代码创建UI。第二种技术是使用可扩展应用程序标记语言(XAML),一种用于描述用户界面的声明性标记语言。有关XAML的更多信息,请参见XAML基础知识.
要首先开始开发Xamarin应用程序,您需要安装Visual Studio或Xamarin Studio,并确保包含所有必要的Xamarin组件。在我们的示例中,我们将使用Visual Studio向您展示如何开发Xamarin应用程序和使用我们的SDK。导航到此链接逐步阅读如何下载和安装用于Xamarin应用程序的Visual Studio。
在下面的小节中,我们将一步步地解释我们的示例应用程序是如何开发的。
打开Visual Studio并遵循以下步骤:
1.去文件->新建->项目。
2.创建空白应用程序(Android)。
加载新项目时,请添加对XamarinDataManLibrary.dll文件的引用。
创建空白应用程序后,创建所有要使用的资源(图标、图像、样式、布局等)。你可以从我们的样品中复制必威随行版官网它们。
接下来右键单击你的项目文件,然后单击属性,然后去Android Manifest部分。
设置您的Manifest文件(应用程序名称,应用程序图标,最小和最大android版本),确保启用此应用程序的摄像头权限。
在我们设置了一些必要的属性之后,我们可以创建ScannerActivity也就是主发射器,并继承一些接口。
[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。停止扫描;
rlPreviewContainer-用于相机预览的视图组(RelativeLayout)容器;
ivPreview- ImageView控件,匹配大小作为其父控件,显示预览或扫描会话的最后一帧;
ReaderDevicecmbSDK对象,它将呈现MX设备或手机摄像头取决于我们的配置;
每次活动开始时,我们都会呼叫初始化设备在这里,我们配置读取器设备对象。
如果我们要使用的MX设备扫描,我们可以使用:
readerDevice=GetMXDevice(此设备);如果(!listeningForUSB){readerDevice.StartAvailabilityListening();listeningForUSB=true;}
MX设备的可用性可以在设备转动时改变在或关,或者如果USB电缆已连接,或者断开连接,这是由IReaderDeviceListener接口。
如果我们想将读卡器设备配置为手机摄像头,我们可以使用:
readerDevice = GetPhoneCameraDevice(this, CameraMode。NoAimer PreviewOption。违约,rlPreviewContainer);
所述CameraMode参数的类型的CameraMode(定义在CameraMode.java)并且它接受以下值之一:
被动瞄准器:初始化读卡器,使其使用被动瞄准器。被动瞄准器是附在移动设备或移动设备外壳上的附件,使用移动设备的内置LED闪光灯作为光源投射瞄准模式。在此模式下,设备屏幕上不会显示直播预览,因为会有一个瞄准模式被投射。
FRONT_CAMERA初始化读取器以使用设备的移动前置摄像头,如果可用(不是所有移动设备都有前置摄像头)。这是一种不寻常但可能的配置。大多数前置摄像头没有自动对焦和照明,提供的图像分辨率明显较低。这个选项应该小心使用。在此模式下,照明不可用。
以上所有模式都为读者提供了以下默认设置:
基于选定的模式,将设置以下附加选项和行为:
这个PreviewOption参数是一种PreviewOption(定义在PreviewOption.java),并用于更改读取器的默认值或覆盖从选定对象派生的默认值CameraMode. 传递参数时,可以通过指定或取消指定多个选项。可供选择的方案有:
在配置ReaderDevice我们需要连接到设备。
在我们建立联系之前ReaderDeviceListener对象的设置以接收事件:
readerDevice.SetReaderDeviceListener(本);
此外,您可以通过以下方式从阅读器发送最后触发的图像和SVG:
readerDevice.EnableImage(真正的);readerDevice.EnableImageGraphics(真正的);
然后我们可以联系:
readerDevice.Connect(这个);
将调用的事件包括:
public void OnConnectionStateChanged(ReaderDevice reader)public void OnConnectionCompleted(ReaderDevice reader,可丢弃错误)
如果在尝试连接时出错,该错误将作为OnConnectionCompleted方法,否则,如果没有发生错误,则错误参数为空值.
如果连接成功,则语句读者。ConnectionState = = ConnectionState。连接会是真的。
有两个API方法用于更改一些公共属性以配置连接的设备,您应该在ConnectionState是连接。
例如,如果将移动摄像机用作ReaderDevice有默认情况下没有启用符号.属性中使用的符号必须启用SetSymbologyEnabledAPI方法:
readerDevice.SetSymbologyEnabled(象征。C128,真的,null);readerDevice.SetSymbologyEnabled(象征。Datamatrix,真的,null);readerDevice.SetSymbologyEnabled(象征。UpcEan,真的,null);readerDevice.SetSymbologyEnabled(象征。Qr,真的,null);
你可以通过以下方式直接向连接的设备发送命令:
readerDevice.DataManSystem.SendCommand(“将SYMBOL.MICROPDF417设置为ON”);
使用正确配置的读卡器,您现在可以扫描条形码了startScanning方法从你ReaderDevice对象。
接下来会发生什么取决于ReaderDevice以及它是如何配置的,但一般来说:
在下列情况之一时,扫描停止:
当条形码被成功解码时(第一种情况),您将收到一个ReadResults的可迭代结果集合对象ReaderDevice侦听器方法。
public void OnReadResultReceived(ReaderDevice reader, ReadResults results) {listViewResultSelectedItem = -1;resultList.Clear ();resultListData.Clear ();ivPreview.SetImageBitmap(空);如果结果。SubResults!= null && results.SubResults.Count > 0) { foreach (ReadResult subResult in results.SubResults) { if (subResult.IsGoodRead) { resultList.Add(subResult); JavaDictionary item = new JavaDictionary(); item.Add("resultText", subResult.ReadString); Symbology sym = 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(); }
result.Image是扫描过程中的最后一帧,它将显示在ivPreviewImageView,和result.ImageGraphics是在图像上定位条形码的SVG图像。
在ScannerActivity我们覆盖暂停和原事件,这样我们就可以断开连接和StopAvailabilityListening当我们从这个活动中导航或破坏这个活动时,释放所有的连接。
protected override void OnPause() {base.OnPause();if (readerDevice != null) {readerDevice. disconnect ();}} protected override void OnStop() {if (readerDevice != null) try {readerDevice. stopavailabilitylistening ();}(系统。异常e) {} listeningForUSB = false;base.OnStop ();}
请记住,在某些情况下,设备可能会由于电池电量不足或手动断开电缆而断开。
如果您计划使用cmbSDK要使用智能手机或平板电脑(没有MX移动终端)进行移动扫描,SDK需要安装许可密钥。没有许可密钥,SDK仍然可以运行,但扫描结果会被模糊化(SDK会用星号字符随机替换扫描结果中的字符)。
请与您的康耐视销售代表联系,了解如何获取许可密钥,包括可用于评估SDK 30天的试用许可。
获得许可密钥后,有两种方式在应用程序中添加许可密钥。
第一个是将它作为一个元标签添加到manifest文件中的application标签中:
…
第二种方法是直接从代码实现激活。当你创建readerDevice时,在构造函数中将许可密钥设置为输入参数:
...readerDevice = GetPhoneCameraDevice(this, CameraMode。NoAimer PreviewOption。违约,rlPreviewContainer,"YOUR_MX_MOBILE_LICENSE");
要在成功扫描后添加“嘟嘟”声,请使用以下代码:
它在MX设备上播放音频信号。
连接和断开MX设备的过程是通过Connect/Disconnect方法完成的
将尝试连接到设备。当Connect ()执行时,我们得到行动的状态OnConnectionCompleted侦听器
OnConnectionCompleted(ReaderDevice reader,Throwable error){if(error != null){//处理错误readerDisconnected();}}
将从MX设备断开。
例子:
readerDevice = ReaderDevice.GetMXDevice (mContext);readerDevice.StartAvailabilityListening ();readerDevice.SetReaderDeviceListener(这个);readerDevice.EnableImage(真正的);readerDevice.Connect(这个);......
public void OnAvailabilityChanged(ReaderDevice reader) {if (reader. getavailability () == availabilityavailable) {ReaderDevice . connect (this);}……
成功扫描的结果可以返回一个图像。这是在一次成功的扫描中解析的最后一帧。要启用/禁用这个功能,我们可以使用API方法:
readerDevice = readerDevice。GetPhoneCameraDevice (mContext CameraMode。NoAimer PreviewOption。违约,rlMainContainer); readerDevice.EnableImage(true); readerDevice.Connect(this);
成功扫描的结果可以返回SVG图像图形。这是成功扫描中解析的最后一帧。要启用/禁用此功能,我们可以使用API方法:
readerDevice=readerDevice.GetPhoneCameraDevice(mContext、CameraMode.NoAimer、PreviewOption.Defaults、rlMainContainer);readerDevice.EnableImageGraphics(true);readerDevice.Connect(this);
在我们可以连接到MX设备之前,我们需要知道是否有一个可用的任务。
它返回可用性对象可以是可用的,不可用的或不为人知
当有USB连接到我们的智能手机时,MX设备是可用的。
public void OnAvailabilityChanged(ReaderDevice reader) {if (reader. getavailability () == availabilityavailable) {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我们需要之前,我们尝试连接到读取设备调用此方法
AvailabilityChanged(ReaderDevice reader){if(reader.GetAvailability()==Availability.Availability){ReaderDevice.Connect(this);}}
public void OnConnectionStateChanged(ReaderDevice reader) {if (ReaderDevice reader);} else if (read_state == read_state . connected){/ /连接状态ConnectionState == ConnectionState. disconnected) {}}
公共无效OnReadResultReceived(ReaderDevice读卡器,ReadResults结果){如果(results.Count> 0){ReadResult结果= results.GetResultAt(0);如果(result.IsGoodRead){...
例子
readerDevice = readerDevice。GetPhoneCameraDevice (mContext CameraMode。NoAimer PreviewOption。违约,rlMainContainer); readerDevice.SetReaderDeviceListener(this); readerDevice.Connect(this);
我们称之为后readerDevice。SetReaderDeviceListener (IReaderDeviceListener listenerObject)方法和注册可用性改变侦听器功能我们可以启动/停止可用性侦听
将启动侦听阅读器设备可用性,并将触发侦听器功能时,每次可用性更改
将停止侦听读卡器设备的可用性
例子:
readerDevice = ReaderDevice.GetMXDevice (mContext);readerDevice.StartAvailabilityListening ();readerDevice.SetReaderDeviceListener(这个);readerDevice.Connect(这个);
受保护的重写void Dispose(bool disposing){if(readerDevice!=null&&readerDevice.ConnectionState==ConnectionState.Connected){readerDevice.SetReaderDeviceListener(null);readerDevice.Disconnect();}if(readerDevice!=null)请尝试{readerDevice.StopAvailabilityListening();}捕获(System.Exception e){}基本处理(disposing);}
在我们连接和配置我们的阅读器和设置所有我们需要的设置之后,我们可以开始扫描过程。
要开始扫描,我们使用方法
当我们的阅读器成功扫描某些条码时,扫描过程将停止,或者我们可以用这种方法手动停止扫描
当扫描停止时,无论是成功扫描还是使用stopscan()方法OnReadResultReceived将调用侦听器函数,我们可以在其中检查扫描结果
公共无效OnReadResultReceived(ReaderDevice读卡器,ReadResults结果){如果(results.Count> 0){ReadResult结果= 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中添加以下协议:
<键> UISupportedExternalAccessoryProtocols < /关键> <数组> <字符串> com.cognex。dmcc <字符串> / <字符串> com.demo.data字符串> < / > < /数组
这个ViewController将是我们的第一个控制器Main.storyboard.这里我们创建了一些UI元素和变量,稍后将在这个控制器中使用。
公共部分类的ViewController:UIViewController中,ICMBReaderDeviceDelegate {保护的ViewController(IntPtr的把手):基座(手柄){//注意:此.ctor不应该包含任何初始化逻辑。} CMBReaderDevice readerDevice;公共BOOL isScanning = FALSE;私人NSMutableArray的资料表;私人MXResultsTableSource tableSource;公共覆盖无效viewDidLoad中(){base.ViewDidLoad();......
lblConnection-为当前连接状态的UI元素添加标签。
tableSource- UITableViewSource用于读取结果。
btnScan-将触发的按钮UI元素StartScanning或停止扫描.
ivPreview-ImageView用于显示预览或扫描会话的最后一帧的UI元素。
CMBReaderDevicecmbSDK对象,它将呈现MX设备或手机摄像头取决于我们的配置。
这里我们重写那些有点方法在显示此视图时配置读卡器设备对象。
如果我们想使用MX设备进行扫描
readerDevice = CMBReaderDevice.ReaderOfMXDevice ();
MX Device的可用性可以在设备打开或关闭时改变,或者如果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参数是该类型Cdmameramode,它接受下列值之一:
以上所有模式都为读者提供了以下默认设置:
基于选定的模式,将设置以下附加选项和行为:
这个previewOptions参数(类型为CDMPreviewOption)用于更改读取器的默认值或覆盖从选定对象派生的默认值CameraMode. 传递参数时,可以通过指定或取消指定多个选项。可用选项如下:
类型的最后一个参数UIView是可选的,用作相机预览的容器。如果参数为空,则将使用全屏预览。
配置后ReaderDevice我们需要连接到设备。
readerDevice.ConnectWithCompletion((错误)=>{if(错误!=null){new UIAlertView(“连接失败”,错误。说明,null,“确定”,null)。Show();});
如果在尝试连接时出现错误,则会在回调函数中抛出错误作为参数。如果一切正常,错误参数将为空。
这个函数将触发ConnectionStateDidChangeOfReader方法。如果连接成功reader.ConnectionState==ConnectionState.Connected。
连接成功后,我们可以为ReaderDevice设置一些设置。ReaderDevice设置可以通过已经封装的函数来设置,也可以直接通过向配置的设备发送命令来设置。
例如,如果移动相机被用作reader设备有默认情况下没有启用符号.属性中使用的符号必须启用SetSymbology包装的功能。
在这个例子中,我们启用了一些符号,并设置了设置来获取扫描进来的最后一帧ivPreview ImageView.
readerDevice.SetSymbology(CMBSymbology.DataMatrix,真实,(误差)=> {如果(误差= NULL){System.Diagnostics.Debug.WriteLine( “FALIED TO ENABLE [DataMatrix的],”,error.LocalizedDescription);}});readerDevice.SetSymbology(CMBSymbology.Qr,真实,(误差)=> {如果(误差= NULL){System.Diagnostics.Debug.WriteLine( “FALIED TO ENABLE [QR]”,error.LocalizedDescription);}});readerDevice.SetSymbology(CMBSymbology.C128,真实,(误差)=> {如果(误差= NULL){System.Diagnostics.Debug.WriteLine( “FALIED TO ENABLE [C128]”,error.LocalizedDescription);}});readerDevice.SetSymbology(CMBSymbology.UpcEan,真实,(误差)=> {如果(误差= 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方法从你ReaderDevice对象。
接下来发生的事情是基于阅读器设备的类型和它是如何被配置的,但一般来说:
在下列情况之一时,扫描停止:
当条形码被成功解码时(第一种情况),您将收到一个ReadResults中可迭代的结果集合对象ReaderDevice侦听器方法。
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); }
可能存在由于电池电量不足或手动断开电缆而导致设备断开的情况。这些病例可以通过ConnectionStateDidChangeOfReader回调的ICMBReaderDeviceDelegate.
注意:这个读卡器的可用性当设备变得不可用物理方法也被称为。这意味着(重新)连接是不可能的。一定要检查的可用性特性ReaderDevice试图调用对象的前ConnectWithCompletion方法。
如果你打算使用cmbSDK做移动扫描与智能手机或平板电脑(没有MX移动终端),那么SDK需要许可证的安装。无许可证密钥,SDK将仍然工作,尽管扫描的结果将被模糊(SDK将随机替换字符中带星号的字符扫描结果)。
请与您的康耐视销售代表联系,了解如何获取许可密钥,包括可用于评估SDK 30天的试用许可。
在获得许可密钥后,有两种方式在应用中添加许可密钥。
第一种方法是将其作为键添加,并在项目特定的Info.plist文件:
MX_MOBILE_LICENSE 您的许可密钥 . txt
第二种实现激活的方法是在创建readerDevice时直接从代码中执行:
.... readerDevice = CMBReaderDevice.ReaderOfDeviceCameraWithCameraMode(CDMCameraMode.NoAimer,CDMPreviewOption.Defaults,ivPreview, “YOUR_MX_MOBILE_LICE”);
在可移植项目中,我们创建一个页面(主页)在这里,我们为扫描页面和一个类(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平台具体项目:
protected override void OnElementChanged(ElementChangedEventArgs e) {base.OnElementChanged(e);if (e.o oldelement != null || Element == null) {return;} rlMainContainer = new RelativeLayout(Context);rlMainContainer.SetMinimumHeight (50);rlMainContainer.SetMinimumWidth (100);rlMainContainer。LayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams. new RelativeLayout.LayoutParams)。MatchParent RelativeLayout.LayoutParams.MatchParent);ivPreview = new ImageView(Context);ivPreview.SetMinimumHeight (50);ivPreview.SetMinimumWidth (100); ivPreview.LayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.MatchParent); ivPreview.SetScaleType(ImageView.ScaleType.FitCenter); rlMainContainer.AddView(ivPreview); if (Control == null) SetNativeControl(rlMainContainer); MainActivity.instance.setActiveReader(Control, Element); }
在里面MainActivity在Android平台特定的项目中,我们处理的是reader设备对象(几乎是一样的ScannerActivity为Xamarin的。安卓)从自定义渲染器中,我们只需调用setActiveReader方法和传递控件(本机RelativeLayout元素)和元素(CameraPreview对象,该对象从主页初始化)。
ViewRenderer在iOS平台特定项目中:
受保护覆盖void-OnElementChanged(ElementChangedEventArgse){base.OnElementChanged(e);if(e.OldElement!=null | | Element==null){return;}container=new UIView();ivPreview=new UIImageView();ivPreview.ContentMode=UIViewContentMode.ScaleToFill;ivSVG=new UIImageView();ivSVG.ContentMode=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(控件,元素);}
在里面AppDelegate在iOS平台特定的项目中,我们处理的是读卡器设备对象(几乎与之类似)视图控制器为沙马林)从自定义渲染器中,我们只需调用setActiveReader方法和传递控件(本机UIImageView元素)和元素(从主页初始化的CameraPreview对象)。
配置读取设备、连接设备、扫描条码和从设备上断开是一样的,你可以在Xamarin的。安卓和沙马林部分。