Anyline图像识别简单说明

一. 基础说明

1. 集成方式

手动导入集成,cocoapods没有反应一直等待,不知道是cocoapods版本缘由仍是什么。官方集成文档html

2. 如何实现Anyline插件

Anyline 4开始,每一个用例须要三个组件才能成功扫描,就是说在你想要实现扫描功能的控制器种添加下面三个组件就能够了:ios

  • ScanPlugin

    处理图像识别和扫描功能 有关扫描的功能所有由该插件处理git

  • ScanViewPlugin

    处理UI相关,关于UI的配置json

  • ScanView

    scanview将处理相机、闪光灯并管理先前建立的scanviewplugin和scanplugin。 关于UI的配置也由ScanView来呈现bash

3. 添加插件到ViewController

添加组件到控制器app

// The Anyline plugins used to scan
@property (nonatomic, strong) ALMeterScanViewPlugin *meterScanViewPlugin;
@property (nonatomic, strong) ALMeterScanPlugin *meterScanPlugin;
@property (nullable, nonatomic, strong) ALScanView *scanView;
复制代码

4. 插件说明

ScanPlugin初始化

初始化的时候须要用到在网站上生成的密钥iview

//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只要保证是一个惟一性的字符串就能够ide

ScanViewPlugin初始化

初始化scanplugin以后,下一步是使用刚刚建立的scanplugin建立scanviewplugin。scanviewplugin将处理并显示用于扫描的UI。网站

//Add Meter Scan View Plugin (Scan UI)
  self.meterScanViewPlugin = [[ALMeterScanViewPlugin alloc] initWithScanPlugin:self.meterScanPlugin];
复制代码
设置ScanViewPluginConfig

视图扫描过程的外观。您能够按如下方式设置 先生成一个json文件,在文件里配置参数,具体参数意义参考官网配置说明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];
复制代码
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在离开活动时正确中止,至少保证在uiviewcontrollerviewwilldisplases: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"
},
}
复制代码

更多UI配置说明

设置扫描模式

在用来扫描水电表的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插件包含的扫描模式图,具体说明点这里

scanmode.jpg

扫描结果回调(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

复制代码
相关文章
相关标签/搜索