内容
要求
- iOS 9.0+
- Xcode 10.0+
- Swift 4.0+
安装
CocoaPods
CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它:
$ gem install cocoapods
要使用 CocoaPods 将 DBrainDocumentFlow 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
target '<Your Target Name>' do
pod 'DBrainDocumentFlow'
end
然后,运行以下命令:
$ pod install
用法
流程
DocumentFlow
- 基础流程。拍照时可以选择文件类型,发送识别前;PassportFlow
- 从 DocumentFlow 继承的流程。默认识别 passport_main 类型;DriverLicenceFlow
- 从 DocumentFlow 继承的流程。默认识别 driver_license_2011_front 类型。
类型
DocumentType
- 创建 DocumentFlow
时指定可能的文件类型;
.empty
- 发送识别时不指定 doc_type;custom (type: String)
- 提交时指定自定义的 doc_type;driverLicence
- 发送识别时不指定 doc_type;passport
- 发送识别时指定 passport_main 类型的 doc_type;selectable
- 拍照成功后,如果分类成功,将提供选择;
方法
- 处理结束流程,用于识别成功后关闭窗口并获取数据(当上传照片成功时调用),默认
nil
Swift
let onEndFlow: ([RecognitionItem]) -> Void = { [weak self] data in
print(data)
self?.dismiss(animated: true)
}
flow.with(onEndFlow: onEndFlow)
RecognitionItem
- 识别结果中获取字段的对象
Swift
struct RecognitionItem: Decodable {
var docType: String // Document type
var fields: [String: RecognitionField] // Document fields
}
struct RecognitionField: Decodable {
var text: String // Field value
var confidence: Double // Accuracy
}
- 照片最大尺寸(千字节),默认
400
Swift
flow.with(expectedSizeKb: 400)
- 默认情况下,跟踪区域按示例中的方法进行计算
Swift
let width = UIScreen.main.bounds.width - 50.0 * 2.0
let height = width * 1.3
let size = CGSize(width: width, height: height)
let origin = CGPoint(x: 50.0, y: 88.0)
let zoneRect = CGRect(origin: origin, size: size)
flow.with(trackingRect: zoneRect)
- 最大爆闪等级,默认为
1
(不考虑爆闪),详细信息请见爆闪
Swift
let lumaDiffCoefficient: CGFloat = 0.35
flow.with(lumaDiffCoefficient: lumaDiffCoefficient)
- 显示直方图和最大亮度级别,默认为
disabled
Swift
flow.withDebugViews()
- 在成功识别后显示结果,默认为
disabled
Swift
flow.withResult()
- 分析结果的标题,使其能够返回用于显示的字符串,默认
nil
(此外,如果返回nil
,则信息将不显示)
Swift
let keyTitles: [String: String] = [
"date_of_birth": "Date of birth: ",
"date_of_issue": "Date of issue: ",
"first_name": "First name: ",
"issuing_authority": "Issuing authority: ",
"other_names": "Other names: ",
"place_of_birth": "Place of birth: ",
"series_and_number": "Series and number: ",
"sex": "Sex: ",
"subdivision_code": "Subdivision code: ",
"surname": "Surname: "
]
let onReciveResult: ((_ key: String) -> String?) = { key in
return keyTitles[key]
}
documentFlow.with(onReciveResult: onReciveResult)
- 分类结果的标题,允许返回字符串并使识别可用/不可用,默认
nil
Swift
let onReciveDocumentType: ((_ type: String) -> (title: String, isEnabled: Bool)) = { type in
return (type.replacingOccurrences(of: "_", with: " ").capitalized, type != "not_document")
}
documentFlow.with(onReciveDocumentType: onReciveDocumentType)
- 模块创建和模块显示。在创建之前,需要调用
build()
方法,然后,使用start()
方法,可以获取配置好的UIViewController
Swift
// Some configration
flow = flow.build()
let viewController = flow.start()
present(viewController, animated: true)
示例
- 创建DocumentFlow
Swift
let onEndFlow: () -> Void = { [weak self] in
self?.dismiss(animated: true)
}
let keyTitles: [String: String] = [
"some_key": "Some key"
]
let onReciveResult: ((_ key: String) -> String?) = { key in
return keyTitles[key] ?? key
}
let onReciveDocumentType: ((_ type: String) -> (title: String, isEnabled: Bool)) = { type in
return (type.replacingOccurrences(of: "_", with: " ").capitalized, type != "not_document")
}
let side = UIScreen.main.bounds.width - 50.0 * 2.0
let size = CGSize(width: side, height: side)
let origin = CGPoint(x: 50.0, y: 88.0)
let flow = DocumentFlow.configure(authorizationToken: token)
.with(onEndFlow: onEndFlow)
.with(onReciveResult: onReciveResult)
.with(onReciveDocumentType: onReciveDocumentType)
.with(lumaDiffCoefficient: 0.70)
.with(expectedSizeKb: 1000)
.with(trackingRect: CGRect(origin: origin, size: size))
.withDebugViews()
.build()
let viewController = flow.start()
viewController.modalPresentationStyle = .fullScreen
present(viewController, animated: true)
- 创建PassportFlow
Swift
let flow = PassportFlow.configure(authorizationToken: token)
.with(onEndFlow: onEndFlow)
.build()
let viewController = flow.start()
viewController.modalPresentationStyle = .fullScreen
present(viewController, animated: true)
Flare
在创建DocumentFlow
、DriverLicenceFlow
或PassportFlow
时,可以指定lumaDiffCoefficient
的值为0到1,如果值大于1,则接受1
,即接受所有内容而忽略亮度。该因子设置直方图右侧边缘的最大高度Max = maxValue * lumaDiffCoefficient
致谢
- Alex Belkovskiy (@nearlydeadhipo)
许可协议
DBrainDocumentFlow 在 MIT 许可协议下发布。详细信息请参阅 LICENSE 文件。