RSBarcodes,现在用 Swift 实现。
RSBarcodes 允许您使用 iOS 7 中引入的元数据扫描功能来读取 1D 和 2D 条形码,并为显示和共享生成相同的条形码图像。现在已用 Swift 实现。
- Objective-C 版本: RSBarcodes
待办事项
生成器
- Code39
- Code39Mod43
- ExtendedCode39
- Code93
- Code128
- UPCE
- EAN FAMILIY (EAN8 EAN13 ISBN13 ISSN13)
- ITF14
- Interleaved2of5
- DataMatrix
- PDF417
- QR
- Aztec
- 视图
阅读器
- 视图
- ReaderController
安装
Swift Package Manager
要在您的 Xcode 项目中添加包依赖项,请选择“文件 > Swift 包 > 添加包依赖”,然后在文本框中输入 https://github.com/yeahdongcn/RSBarcodes_Swift。
CocoaPods
只需在您的 Podfile
中添加以下几行即可。
# required by Cocoapods 0.36.0.rc.1 for Swift Pods
use_frameworks!
pod 'RSBarcodes_Swift', '~> 5.1.1'
使用向导创建文件后,您需要手动在 ViewController 文件中导入 RSBarcodes_Swift。
(需要 CocoaPods v0.36 或更高版本。有关详细信息,请参阅 这篇博客文章。)
Carthage
只需在您的 Cartfile
中添加以下一行即可。
github "yeahdongcn/RSBarcodes_Swift" >= 5.1.1
使用向导创建文件后,您需要手动在 ViewController 文件中导入 RSBarcodes_Swift。
Swift Package Manager (需要 Xcode 11)
- 选择“文件 > Swift 包 > 添加包依赖”。在“选择包仓库”对话框中输入
https://github.com/yeahdongcn/RSBarcodes_Swift
。 - 在下一页,将版本解析规则指定为“到下一个主版本”,并选择最新版本。
- 在 Xcode 检查出来源并解析版本后,您可以选择“RSBarcodes_Swift”库并将其添加到您的应用程序目标中。
手动
- 通过打开终端,进入顶层项目目录,并输入命令
git submodule add https://github.com/yeahdongcn/RSBarcodes_Swift.git
,将 RSBarcodes_Swift 作为 子模块 添加。 - 打开
RSBarcodes_Swift
文件夹,并将RSBarcodes.xcodeproj
拖到您的应用程序项目文件导航器中。 - 在Xcode中,通过点击蓝色项目图标导航到目标配置窗口,然后在侧边栏的“目标”标题下选择应用程序目标。
- 确保RSBarcodes.framework的部署目标与应用程序目标的部署目标匹配。
- 在窗口顶部的标签栏中,打开“构建阶段”面板。
- 展开“目标依赖”组,并添加
RSBarcodes.framework
。 - 点击面板左上角的
+
按钮,选择“新复制文件阶段”。将此新阶段重命名为“复制框架”,将“目标”设置为“框架”,并添加RSBarcodes.framework
。 - 在创建文件使用向导后,需要在ViewController文件中手动导入RSBarcodes。
用法
生成器
首先,导入以下框架
import RSBarcodes_Swift
import AVFoundation
然后,使用生成器生成条码。例如
RSUnifiedCodeGenerator.shared.generateCode("2166529V", machineReadableCodeObjectType: AVMetadataObjectTypeCode39Code)
如果2166529V
是一个有效的code39字符串,它将生成一个UIImage
实例。对于AVMetadataObjectTypeCode128Code
,您可以将useBuiltInCode128Generator
更改为false
以使用我的实现(Autotable for code128)。
备注:有四个表格用于将字符串编码为code128,即TableA
、TableB
、TableC
和TableAuto
;其中TableAuto
始终是最好的选择,但如果有具体需求,试试这个
RSCode128Generator(codeTable: .A).generateCode("123456", machineReadableCodeObjectType: AVMetadataObjectTypeCode128Code)
这些简单调用的示例可以在测试项目中找到。
读取器
以下是启用条码读取器的步骤
文件
->新建
->文件
- 在iOS下点击源,确保已选中
Cocoa Touch Class
并按下一步
。 - 可以给类命名你想的名字,但我将称它为
ScanViewController
。 - 将其设置为
RSCodeReaderViewController
的子类,并确保语言为Swift
,然后点击下一步
和创建
- 打开您的故事板,将其中的
UIViewController
拖拽上去。 - 在自定义类下选择
ScanViewController
- 焦点标记层和角落层已经存在并为您工作。有两个处理器:一个用于屏幕上的单次点击和焦点标记,另一个用于检测对象的处理器,所有检测到的对象都将反馈给您。现在在
ScanViewController.swift
文件中,将以下代码添加到viewDidLoad()
或者您更合适的任何位置
override func viewDidLoad() {
super.viewDidLoad()
self.focusMarkLayer.strokeColor = UIColor.red.cgColor
self.cornersLayer.strokeColor = UIColor.yellow.cgColor
self.tapHandler = { point in
print(point)
}
self.barcodesHandler = { barcodes in
for barcode in barcodes {
print("Barcode found: type=" + barcode.type + " value=" + barcode.stringValue)
}
}
}
如果您想忽略某些代码类型(例如,AVMetadataObjectTypeQRCode
),请添加以下几行
let types = NSMutableArray(array: self.output.availableMetadataObjectTypes)
types.remove(AVMetadataObjectTypeQRCode)
self.output.metadataObjectTypes = NSArray(array: types)
校验器
用于校验条码
let isValid = RSUnifiedCodeValidator.shared.isValid(code, machineReadableCodeObjectType: AVMetadataObjectTypeEAN13Code)
图像辅助器
使用 RSAbstractCodeGenerator.resizeImage(source: UIImage, scale: CGFloat)
缩放生成的图像。
使用 RSAbstractCodeGenerator.resizeImage(source: UIImage, targetSize: CGSize, contentMode: UIViewContentMode)
将图像的边界填充/适配到最佳能力,而不必知道填充/适配的最佳比例,或者如果 UIImageView
本身是否可以灵活伸缩。
杂项
许可证
The MIT License (MIT)
Copyright (c) 2012-2014 P.D.Q.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.