iOS 条形码解码用户指南
要求
- 支持的操作系统:iOS 11或更高版本(发起使用iOS 13及更高版本)。
- 支持的 ABI:arm64和x86_64。
- 开发环境:Xcode 13 及以上版本(发起 Xcode 14.1+)。
有三种方法可以将 SDK 添加到您的项目中 -手动、通过CocoaPods或通过Swift 包管理器。
选项 1:通过 Swift 包管理器添加 xcframeworks
- 在您的 Xcode 项目中,转到文件 –> AddPackages。
- 在窗口的右上角,搜刮“https://github.com/Dynamsoft/barcode-reader-spm”
- 选择barcode-reader-spm、选择Exact version、输入10.4.2000,然后单击添加包。
- 查抄所有xcframeworks并添加。
选项 2:通过 CocoaPods 添加框架
- 在您的Podfile中添加框架,TargetName并用您的真实目标名称替换。
- target 'HelloWorld' do
- use_frameworks!
- pod 'DynamsoftBarcodeReaderBundle','10.4.2000'
- end
复制代码 了解有关DynamsoftBarcodeReaderBundle模块的更多信息
- 实行 pod 命令安装框架并天生工作区([TargetName].xcworkspace):
选项 3:添加本地 xcframeworks 文件
- 从Dynamsoft 官网下载 SDK 包,解压后在Dynamsoft\Frameworks目次下可以找到以下xcframeworks:
- 将xcframeworks拖放到您的 Xcode 项目中。确保查抄Copy items if needed并将Create groups框架复制到您的项目文件夹中。
- 点击项目设置,然后转到常规 –> 框架、库和嵌入内容。将上述所有xcframeworks的嵌入字段设置为嵌入并署名。
构建您的第一个应用步伐
在本节中,让我们创建一个HelloWorld应用步伐,用于从摄像机视频输入读取条形码。
条记:
- 本指南使用 XCode 14.2。
- 您可以从HelloWorld/DecodeWithCameraEnhancerObjc Sample下载完整的 Objective-C 源代码
- 您可以从HelloWorld/DecodeWithCameraEnhancer Sample下载完整的 Swift 源代码
- 以下指南中使用 DCE 举行相机捕获。如果您使用 iOS AVFoundation 框架举行相机捕获,请检察DecodeWithAVCaptureSession 示例,了解怎样将条形码扫描添加到您的应用。
创建新项目
- 打开 Xcode 并选择创建一个新项目。
- 选择iOS > App作为您的应用步伐。
- 输入您的产物名称(DBRHelloworld)、界面(StoryBoard)并选择语言(Objective-C/Swift)。
- 单击下一步按钮并选择保存项目的位置。
- 单击创建按钮完成。
包罗框架
将 SDK 添加到您的新项目中。有几种方法可以做到这一点,本节将具体介绍所有这些方法。
初始化许可证
Dynamsoft Barcode Reader 必要有效的许可证才气运行。
- LicenseVerificationListener通过ViewController类实现协议:
-
- import DynamsoftLicense
- class ViewController: UIViewController, LicenseVerificationListener
复制代码
- 在方法中添加以下代码来初始化许可证application:didFinishLaunchingWithOptions:并接收回调消息:
-
- func setLicense() {
- LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", verificationDelegate: self)
- }
- func onLicenseVerified(_ isSuccess: Bool, error: Error?) {
- if !isSuccess {
- if let error = error {
- print("\(error.localizedDescription)")
- }
- }
- }
复制代码 条记:
- 许可证要发挥作用,必要网络连接。
- 此处的许可证字符串将授予您限时试用许可证。
- 您可以通过哀求试用许可证链接申请 30 天的试用许可证。
- 如果您下载安装包,它默认附带 30 天的试用许可证。
设置相机增强器
- 导入文件中必要的所有标题ViewController。
-
- import DynamsoftCameraEnhancer
- import DynamsoftCaptureVisionRouter
- import DynamsoftBarcodeReader
- import DynamsoftCore
复制代码
- 初始化CameraEnhancer并CameraView添加的设置CameraEnhancer。
-
- var cameraView:CameraView!
- let dce = CameraEnhancer()
- ...
- func setUpCamera() {
- cameraView = .init(frame: view.bounds)
- cameraView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
- view.insertSubview(cameraView, at: 0)
- dce.cameraView = cameraView
- }
复制代码
通过 CaptureVisionRouter 设置条码解码使命
- 初始化CaptureVisionRouter并与CameraEnhancer实例绑定。
-
- let cvr = CaptureVisionRouter()
- ...
- func setUpDCV() {
- try! cvr.setInput(dce)
- }
复制代码
- 实现onDecodedBarcodesReceived接收条形码解码结果,并将此结果接收器添加到当前 CVR 对象。
-
- /**Add CapturedResultReceiver to your ViewController.*/
- class ViewController: UIViewController, CapturedResultReceiver, LicenseVerificationListener {
- ...
- func setUpDCV() {
- ...
- /**Add your CaptureResultReceiver to the CaptureVisionRouter.*/
- cvr.addResultReceiver(self)
- }
- /**Implement onDecodedBarcodesReceived method of CaptureResultReceiver to receive the barcode decoding results.*/
- func onDecodedBarcodesReceived(_ result: DecodedBarcodesResult) {
- if let items = result.items, items.count > 0 {
- DispatchQueue.main.async {
- self.cvr.stopCapturing()
- }
- var message = ""
- for item in items {
- message = String(format:"\nFormat: %@\nText: %@\n", item.formatString, item.text)
- }
- showResult("Results", message) {
- self.cvr.startCapturing(PresetTemplate.readBarcodes.rawValue)
- }
- }
- }
- private func showResult(_ title: String, _ message: String, completion: @escaping () -> Void) {
- DispatchQueue.main.async {
- let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in completion() }))
- self.present(alert, animated: true, completion: nil)
- }
- }
- }
复制代码
设置 viewDidLoad、viewWillAppear、viewWillDisappear
viewDidLoad现在已经定义和设置了所有紧张功能,让我们通过完成、viewWillAppear和功能来完成代码方面的工作viewWillDisappear。
-
- override func viewDidLoad() {
- super.viewDidLoad()
- /**Do any additional setup after loading the view.*/
- setLicense()
- setUpCamera()
- setUpDCV()
- }
- override func viewWillAppear(_ animated: Bool) {
- dce.open()
- // Start capturing when the view will appear. If success, you will receive results in the CapturedResultReceiver. Otherwise you will receive the error message in a dialog.
- cvr.startCapturing(PresetTemplate.readBarcodes.rawValue) { isSuccess, error in
- if (!isSuccess) {
- if let error = error {
- self.showResult("Error", error.localizedDescription)
- }
- }
- }
- super.viewWillAppear(animated)
- }
- override func viewWillDisappear(_ animated: Bool) {
- dce.close()
- cvr.stopCapturing()
- super.viewWillDisappear(animated)
- }
- ...
复制代码 设置相机权限
将隐私 - 相机使用阐明添加到info.plist您的项目以哀求相机权限。一种简朴的方法是访问您的项目设置,转到信息,然后将此隐私属性添加到 iOS 目标属性列表。
iOS 12 或更低版本的其他步骤
如果你的iOS版本低于13,请添加以下额外步骤:
- 从文件中删除方法application:didDiscardSceneSessions:和。application:configurationForConnectingSceneSession
ptions:AppDelegate
- 删除SceneDelegate.Swift文件(SceneDelegate.h&SceneDelegate.m表示 Objective-C)。
- 从您的 info.plist 文件中删除Application Scene Manifest。
- 在您的文件中声明窗口AppDelegate.Swift(AppDelegate.h对于 Objective-C)。
-
- import UIKit
- @main
- class AppDelegate: UIResponder, UIApplicationDelegate {
- var window: UIWindow?
- }
复制代码
运行项目
- 选择要在其上运行应用的设备。请留意,您必要一个物理设备来运行该应用步伐。如果您在模拟器上运行该应用步伐,您将在运行时遇到一些错误,由于必要物理相机组件。如果您有 M1 Macbook(或更高型号),您可以直接在 Macbook 上运行该应用步伐,由于它具有与您的 iPhone/iPad 类似的架构。
- 运行该项目,然后您的应用步伐将安装在您的设备上。
您可以在此处下载完整的源代码:
- Objective-C 源代码
- Dynamsoft Barcode Reader
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |