一. 基础说明
1. 集成方式
手动导入集成,cocoapods没有反应一直等待,不知道是cocoapods版本原因还是什么。官方集成文档
2. 如何实现Anyline插件
从Anyline 4
开始,每个用例需要三个组件才能成功扫描,就是说在你想要实现扫描功能的控制器种添加下面三个组件就可以了:
- ScanPlugin
处理图像识别和扫描功能
有关扫描的功能全部由该插件处理 - ScanViewPlugin
处理UI相关,关于UI的配置
- ScanView
scanview将处理相机、闪光灯并管理先前创建的scanviewplugin和scanplugin。
关于UI的配置也由ScanView来呈现
3. 添加插件到ViewController
添加组件到控制器
// The Anyline plugins used to scan
@property (nonatomic, strong) ALMeterScanViewPlugin *meterScanViewPlugin;
@property (nonatomic, strong) ALMeterScanPlugin *meterScanPlugin;
@property (nullable, nonatomic, strong) ALScanView *scanView;
4. 插件说明
ScanPlugin初始化
初始化的时候需要用到在网站上生成的密钥
//ALMeterScanPlugin是针对水表的类型,根据业务选择合适的plugin(下文出现的同理)
NSError *error = nil;
self.meterScanPlugin = [[ALMeterScanPlugin alloc] initWithPluginID:@"ENERGY" licenseKey:kDemoAppLicenseKey delegate:self error:&error];
NSAssert(self.meterScanPlugin, @"Setup Error: %@", error.debugDescription);
上面代码里密钥需要生成,ID只要保证是一个唯一性的字符串就可以
ScanViewPlugin初始化
初始化scanplugin之后,下一步是使用刚刚创建的scanplugin创建scanviewplugin。scanviewplugin将处理并显示用于扫描的UI。
//Add Meter Scan View Plugin (Scan UI)
self.meterScanViewPlugin = [[ALMeterScanViewPlugin alloc] initWithScanPlugin:self.meterScanPlugin];
设置ScanViewPluginConfig
视图扫描过程的外观。您可以按以下方式设置
先生成一个json文件,在文件里配置参数,具体参数意义参考官网配置说明
NSString *confPath = [[NSBundle mainBundle] pathForResource:@"meter_capture_config" ofType:@"json"];
ALScanViewPluginConfig *scanViewPluginConfig = [ALScanViewPluginConfig configurationFromJsonFilePath:confPath];
self.meterScanViewPlugin = [[ALMeterScanViewPlugin alloc] initWithScanPlugin:self.meterScanPlugin
scanViewPluginConfig:scanViewPluginConfig];
ScanView
最后需要创建的anyline对象是所谓的scanview,它将处理相机、闪光灯并管理先前创建的scanviewplugin和scanplugin。您需要用先前创建的scanviewplugin实例化scanview。通常,这就是我们看到的相机界面。
//Add ScanView (Camera and Flashbutton)
self.scanView = [[ALScanView alloc] initWithFrame:frame scanViewPlugin:self.meterScanViewPlugin];
[self.view addSubview:self.scanView];
[self.scanView startCamera];
开始扫描
注意:
在启动任何行为之前,确保在viewDidLoad
中使用了[scanView startCamera]
。
/*
This method will be called once the view controller and its subviews have appeared on screen
*/
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
/*
This is the place where we tell Anyline to start receiving and displaying images from the camera.
Success/error tells us if everything went fine.
*/
NSError *error = nil;
BOOL success = [self.meterScanViewPlugin startAndReturnError:&error];
if( !success ) {
// Something went wrong. The error object contains the error description
[[[UIAlertView alloc] initWithTitle:@"Start Scanning Error"
message:error.debugDescription
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
}
停止扫描
要停止扫描过程,请在插件上调用
stopandreturnerror:
要确保sdk在离开活动时正确停止,至少保证在uiviewcontroller
的viewwilldisplases:lifecycle
方法中使用stopandreturnerror
:
/*
Cancel scanning to allow the module to clean up
*/
- (void)viewWillDisappear:(BOOL)animated {
[self.meterScanViewPlugin stopAndReturnError:nil];
}
检查密钥的到期时间
密钥过期会引起错误,提供了一个静态方法来检查许可证密钥字符串的到期日期。将返回包含日期的nsstring。如果无法分析许可证,将返回一个错误
NSError *error = nil;
NSString *dateString = [ALCoreController licenseExpirationDateForLicense:YOUR_LICENSE_KEY_STRING error:&error];
后续说明具体开发的一些细节和问题....
二. 插件细节
Anyline提供了6种插件,根据业务来选择合适的插件,下面简单介绍下其中的一种MeterPlugin
MeterPlugin
Anyline能源插件能够扫描模拟电表、煤气表和水表的读数。也可以扫描条形码和二维码,这对识别仪表和序列号很有用。普通的数字表和热量表也可以扫描。
如果要实现该插件,首先上文提到的基础是都要实现的,文末会给出一个完整的代码,先来看一些细节。
扫描界面UI配置相关
NSString *confPath = [[NSBundle mainBundle] pathForResource:@"meter_capture_config" ofType:@"json"];
ALScanViewPluginConfig *scanViewPluginConfig = [ALScanViewPluginConfig configurationFromJsonFilePath:confPath];
self.meterScanViewPlugin = [[ALMeterScanViewPlugin alloc] initWithScanPlugin:self.meterScanPlugin
scanViewPluginConfig:scanViewPluginConfig];
上面代码就是设置扫描界面UI代码,其中的重点是我们要在项目中生成一个json文件来配置,以上面代码为例就是要新建一个meter_capture_config .json
文件,例如设置相机和闪光灯按钮:
{
"camera": {
"captureResolution": "1080",
"pictureResolution": "1080",
"zoomGesture" : true,
"zoomRatio" : 2,
"maxZoomRatio" : 5
},
"flash": {
"mode": "manual",
"alignment": "bottom_right"
},
}
设置扫描模式
在用来扫描水电表的Meter插件中又细化为很多用例,通过将扫描模式设置为相应的用例,可以从meter插件启动与之相关的用例。
//Set ScanMode to ALAutoAnalogDigitalMeter
//这里的 ALAutoAnalogDigitalMeter 是一种类型,该类型描述为:扫描所有类型的模拟仪表(如煤气表、电表、水表),自动检测小数点前后的位数,以及至少3位的7段数字仪表。
BOOL success = [self.meterScanPlugin setScanMode:ALAutoAnalogDigitalMeter error:&error];
if( !success ) {
// Something went wrong. The error object contains the error description
[[[UIAlertView alloc] initWithTitle:@"设置扫描类型失败"
message:error.debugDescription
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
下面是一张Meter插件包含的扫描模式图,具体说明点这里:

扫描结果回调(TheMeterDelegate)
在控制器中遵循协议
<ALMeterScanPluginDelegate>
,在代理方法anylineMeterScanPlugin:didFindScanResult:
中得到扫描结果
得到的扫描结果为 ALMeterResult
,该结果类包含了实际的扫描结果、扫描图像和最后处理的全帧图像
- ALMeterResult
Field | Type | Nullable | Description |
---|---|---|---|
result | NSString | ✗ | 扫描过程的实际结果 |
image | UIImage | ✓ | 扫描中裁剪的图像 |
fullImage | UIImage | ✓ | 完整的图像 |
confidence | NSInteger | ✓ | 扫描结果的准确度 |
#pragma mark - ALMeterScanPluginDelegate methods
/*
The main delegate method Anyline uses to report its scanned codes
*/
- (void)anylineMeterScanPlugin:(ALMeterScanPlugin *)anylineMeterScanPlugin
didFindResult:(ALMeterResult *)scanResult {
[self anylineDidFindResult:scanResult.result barcodeResult:self.barcodeResult image:(UIImage*)scanResult.image scanPlugin:anylineMeterScanPlugin viewPlugin:self.meterScanViewPlugin completion:^{
//Display the result
}];
}
一个完整的代码:
//
// SHMeterScanViewController.m
// TimeHomeApp
//
// Created by ning on 2019/9/26.
// Copyright © 2019 SafeHome. All rights reserved.
//
#import "SHMeterScanViewController.h"
#import <Anyline/Anyline.h>
#define kDemoAppLicenseKey @"你申请的key"
@interface SHMeterScanViewController ()<ALMeterScanPluginDelegate>
@property (nonatomic, strong) ALMeterScanViewPlugin *meterScanViewPlugin;
@property (nonatomic, strong) ALMeterScanPlugin *meterScanPlugin;
@property (nullable, nonatomic, strong) ALScanView *scanView;
@end
@implementation SHMeterScanViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *confPath = [[NSBundle mainBundle] pathForResource:@"vin_capture_config" ofType:@"json"];
//Initiate the ALScanViewPluginConfig with the JSON file
ALScanViewPluginConfig *scanViewPluginConfig = [ALScanViewPluginConfig configurationFromJsonFilePath:confPath];
NSError *error = nil;
self.meterScanPlugin = [[ALMeterScanPlugin alloc] initWithPluginID:@"ENERGY" licenseKey:kDemoAppLicenseKey delegate:self error:&error];
self.meterScanViewPlugin = [[ALMeterScanViewPlugin alloc]initWithScanPlugin:self.meterScanPlugin scanViewPluginConfig:scanViewPluginConfig];
//Set ScanMode to ALAutoAnalogDigitalMeter
BOOL success = [self.meterScanPlugin setScanMode:ALAutoAnalogDigitalMeter error:&error];
if( !success ) {
// Something went wrong. The error object contains the error description
[[[UIAlertView alloc] initWithTitle:@"Set ScanMode Error"
message:error.debugDescription
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
//Add ScanView (Camera and Flashbutton)
CGRect frame = [[UIScreen mainScreen] applicationFrame];
frame = CGRectMake(frame.origin.x, frame.origin.y + self.navigationController.navigationBar.frame.size.height, frame.size.width, frame.size.height - self.navigationController.navigationBar.frame.size.height);
self.scanView = [[ALScanView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) scanViewPlugin:self.meterScanViewPlugin];
//Enable Zoom Gesture
[self.scanView enableZoomPinchGesture:YES];
//Adding the scanView
[self.view addSubview:self.scanView];
[self.scanView startCamera];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
/*
This is the place where we tell Anyline to start receiving and displaying images from the camera.
Success/error tells us if everything went fine.
*/
NSError *error = nil;
BOOL success = [self.meterScanViewPlugin startAndReturnError:&error];
if( !success ) {
// Something went wrong. The error object contains the error description
[QMUITips showError:@"Start Scanning Error"];
}
}
/*
Cancel scanning to allow the module to clean up
*/
- (void)viewWillDisappear:(BOOL)animated {
[self.meterScanViewPlugin stopAndReturnError:nil];
}
#pragma mark - ALMeterScanPluginDelegate methods
/*
The main delegate method Anyline uses to report its scanned codes
*/
- (void)anylineMeterScanPlugin:(ALMeterScanPlugin *)anylineMeterScanPlugin
didFindResult:(ALMeterResult *)scanResult {
NSLog(@"%@",scanResult);
QMUIAlertAction *action1 = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleCancel handler:NULL];
QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"结果" message:scanResult.result preferredStyle:QMUIAlertControllerStyleAlert];
[alertController addAction:action1];
[alertController showWithAnimated:YES];
}
@end
网友评论