一. 基礎(chǔ)說(shuō)明
1. 集成方式
手動(dòng)導(dǎo)入集成,cocoapods沒(méi)有反應(yīng)一直等待凭迹,不知道是cocoapods版本原因還是什么罚屋。官方集成文檔
2. 如何實(shí)現(xiàn)Anyline插件
從Anyline 4
開(kāi)始,每個(gè)用例需要三個(gè)組件才能成功掃描嗅绸,就是說(shuō)在你想要實(shí)現(xiàn)掃描功能的控制器種添加下面三個(gè)組件就可以了:
- ScanPlugin
處理圖像識(shí)別和掃描功能
有關(guān)掃描的功能全部由該插件處理 - ScanViewPlugin
處理UI相關(guān),關(guān)于UI的配置
- ScanView
scanview將處理相機(jī)脾猛、閃光燈并管理先前創(chuàng)建的scanviewplugin和scanplugin。
關(guān)于UI的配置也由ScanView來(lái)呈現(xiàn)
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. 插件說(shuō)明
ScanPlugin初始化
初始化的時(shí)候需要用到在網(wǎng)站上生成的密鑰
//ALMeterScanPlugin是針對(duì)水表的類型鱼鸠,根據(jù)業(yè)務(wù)選擇合適的plugin(下文出現(xiàn)的同理)
NSError *error = nil;
self.meterScanPlugin = [[ALMeterScanPlugin alloc] initWithPluginID:@"ENERGY" licenseKey:kDemoAppLicenseKey delegate:self error:&error];
NSAssert(self.meterScanPlugin, @"Setup Error: %@", error.debugDescription);
上面代碼里密鑰需要生成猛拴,ID只要保證是一個(gè)唯一性的字符串就可以
ScanViewPlugin初始化
初始化scanplugin之后,下一步是使用剛剛創(chuàng)建的scanplugin創(chuàng)建scanviewplugin蚀狰。scanviewplugin將處理并顯示用于掃描的UI愉昆。
//Add Meter Scan View Plugin (Scan UI)
self.meterScanViewPlugin = [[ALMeterScanViewPlugin alloc] initWithScanPlugin:self.meterScanPlugin];
設(shè)置ScanViewPluginConfig
視圖掃描過(guò)程的外觀。您可以按以下方式設(shè)置
先生成一個(gè)json文件麻蹋,在文件里配置參數(shù)跛溉,具體參數(shù)意義參考官網(wǎng)配置說(shuō)明
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
最后需要?jiǎng)?chuàng)建的anyline對(duì)象是所謂的scanview,它將處理相機(jī)、閃光燈并管理先前創(chuàng)建的scanviewplugin和scanplugin倒谷。您需要用先前創(chuàng)建的scanviewplugin實(shí)例化scanview蛛蒙。通常糙箍,這就是我們看到的相機(jī)界面渤愁。
//Add ScanView (Camera and Flashbutton)
self.scanView = [[ALScanView alloc] initWithFrame:frame scanViewPlugin:self.meterScanViewPlugin];
[self.view addSubview:self.scanView];
[self.scanView startCamera];
開(kāi)始掃描
注意:
在啟動(dòng)任何行為之前,確保在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];
}
}
停止掃描
要停止掃描過(guò)程抖格,請(qǐng)?jiān)诓寮险{(diào)用
stopandreturnerror:
要確保sdk在離開(kāi)活動(dòng)時(shí)正確停止,至少保證在uiviewcontroller
的viewwilldisplases:lifecycle
方法中使用stopandreturnerror
:
/*
Cancel scanning to allow the module to clean up
*/
- (void)viewWillDisappear:(BOOL)animated {
[self.meterScanViewPlugin stopAndReturnError:nil];
}
檢查密鑰的到期時(shí)間
密鑰過(guò)期會(huì)引起錯(cuò)誤,提供了一個(gè)靜態(tài)方法來(lái)檢查許可證密鑰字符串的到期日期咕晋。將返回包含日期的nsstring雹拄。如果無(wú)法分析許可證,將返回一個(gè)錯(cuò)誤
NSError *error = nil;
NSString *dateString = [ALCoreController licenseExpirationDateForLicense:YOUR_LICENSE_KEY_STRING error:&error];
后續(xù)說(shuō)明具體開(kāi)發(fā)的一些細(xì)節(jié)和問(wèn)題....
二. 插件細(xì)節(jié)
Anyline提供了6種插件掌呜,根據(jù)業(yè)務(wù)來(lái)選擇合適的插件滓玖,下面簡(jiǎn)單介紹下其中的一種MeterPlugin
MeterPlugin
Anyline能源插件能夠掃描模擬電表、煤氣表和水表的讀數(shù)质蕉。也可以掃描條形碼和二維碼势篡,這對(duì)識(shí)別儀表和序列號(hào)很有用。普通的數(shù)字表和熱量表也可以掃描模暗。
如果要實(shí)現(xiàn)該插件禁悠,首先上文提到的基礎(chǔ)是都要實(shí)現(xiàn)的,文末會(huì)給出一個(gè)完整的代碼兑宇,先來(lái)看一些細(xì)節(jié)碍侦。
掃描界面UI配置相關(guān)
NSString *confPath = [[NSBundle mainBundle] pathForResource:@"meter_capture_config" ofType:@"json"];
ALScanViewPluginConfig *scanViewPluginConfig = [ALScanViewPluginConfig configurationFromJsonFilePath:confPath];
self.meterScanViewPlugin = [[ALMeterScanViewPlugin alloc] initWithScanPlugin:self.meterScanPlugin
scanViewPluginConfig:scanViewPluginConfig];
上面代碼就是設(shè)置掃描界面UI代碼,其中的重點(diǎn)是我們要在項(xiàng)目中生成一個(gè)json文件來(lái)配置隶糕,以上面代碼為例就是要新建一個(gè)meter_capture_config .json
文件瓷产,例如設(shè)置相機(jī)和閃光燈按鈕:
{
"camera": {
"captureResolution": "1080",
"pictureResolution": "1080",
"zoomGesture" : true,
"zoomRatio" : 2,
"maxZoomRatio" : 5
},
"flash": {
"mode": "manual",
"alignment": "bottom_right"
},
}
設(shè)置掃描模式
在用來(lái)掃描水電表的Meter插件中又細(xì)化為很多用例,通過(guò)將掃描模式設(shè)置為相應(yīng)的用例枚驻,可以從meter插件啟動(dòng)與之相關(guān)的用例濒旦。
//Set ScanMode to ALAutoAnalogDigitalMeter
//這里的 ALAutoAnalogDigitalMeter 是一種類型,該類型描述為:掃描所有類型的模擬儀表(如煤氣表测秸、電表疤估、水表),自動(dòng)檢測(cè)小數(shù)點(diǎn)前后的位數(shù)霎冯,以及至少3位的7段數(shù)字儀表芯杀。
BOOL success = [self.meterScanPlugin setScanMode:ALAutoAnalogDigitalMeter error:&error];
if( !success ) {
// Something went wrong. The error object contains the error description
[[[UIAlertView alloc] initWithTitle:@"設(shè)置掃描類型失敗"
message:error.debugDescription
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
下面是一張Meter插件包含的掃描模式圖,具體說(shuō)明點(diǎn)這里:
掃描結(jié)果回調(diào)(TheMeterDelegate)
在控制器中遵循協(xié)議
<ALMeterScanPluginDelegate>
,在代理方法anylineMeterScanPlugin:didFindScanResult:
中得到掃描結(jié)果
得到的掃描結(jié)果為 ALMeterResult
,該結(jié)果類包含了實(shí)際的掃描結(jié)果铸豁、掃描圖像和最后處理的全幀圖像
- ALMeterResult
Field | Type | Nullable | Description |
---|---|---|---|
result | NSString | ? | 掃描過(guò)程的實(shí)際結(jié)果 |
image | UIImage | ? | 掃描中裁剪的圖像 |
fullImage | UIImage | ? | 完整的圖像 |
confidence | NSInteger | ? | 掃描結(jié)果的準(zhǔn)確度 |
#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
}];
}
一個(gè)完整的代碼:
//
// 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 @"你申請(qǐng)的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:@"結(jié)果" message:scanResult.result preferredStyle:QMUIAlertControllerStyleAlert];
[alertController addAction:action1];
[alertController showWithAnimated:YES];
}
@end