zxingify-objc
ZXingObjC 是 ZXing ("Zebra Crossing") 的全面 Objective-C 编译版,一个 Java 条码图像处理库。它设计用于在 iOS 设备和 Mac 应用程序中使用。
以下条码目前支持编码和解码:
- UPC-A 和 UPC-E
- EAN-8 和 EAN-13
- Code 39
- Code 93
- Code 128
- ITF
- Codabar
- RSS-14 (所有变体)
- QR Code
- Data Matrix
- Maxicode
- Aztec ('beta' 质量)
- PDF 417 ('beta' 质量)
ZXingObjC 目前与 ZXing 版本 3.3.3 具有功能等效性。
要求
ZXingObjC 需要 Xcode 8.3.3 及以上版本,目标平台为 iOS 8.0 及以上,或 Mac OS X 10.8 Mountain Lion 及以上。
用法
编码
NSError *error = nil;
ZXMultiFormatWriter *writer = [ZXMultiFormatWriter writer];
ZXBitMatrix* result = [writer encode:@"A string to encode"
format:kBarcodeFormatQRCode
width:500
height:500
error:&error];
if (result) {
CGImageRef image = CGImageRetain([[ZXImage imageWithMatrix:result] cgimage]);
// This CGImageRef image can be placed in a UIImage, NSImage, or written to a file.
CGImageRelease(image);
} else {
NSString *errorMessage = [error localizedDescription];
}
解码
CGImageRef imageToDecode; // Given a CGImage in which we are looking for barcodes
ZXLuminanceSource *source = [[[ZXCGImageLuminanceSource alloc] initWithCGImage:imageToDecode] autorelease];
ZXBinaryBitmap *bitmap = [ZXBinaryBitmap binaryBitmapWithBinarizer:[ZXHybridBinarizer binarizerWithSource:source]];
NSError *error = nil;
// There are a number of hints we can give to the reader, including
// possible formats, allowed lengths, and the string encoding.
ZXDecodeHints *hints = [ZXDecodeHints hints];
ZXMultiFormatReader *reader = [ZXMultiFormatReader reader];
ZXResult *result = [reader decode:bitmap
hints:hints
error:&error];
if (result) {
// The coded result as a string. The raw data can be accessed with
// result.rawBytes and result.length.
NSString *contents = result.text;
// The barcode format, such as a QR code or UPC-A
ZXBarcodeFormat format = result.barcodeFormat;
} else {
// Use error to determine why we didn't get a result, such as a barcode
// not being found, an invalid checksum, or a format inconsistency.
}
安装
我们强烈推荐使用 Carthage 作为模块管理器。
Carthage
可以使用 Carthage 安装 ZXingObjC。安装 Carthage 后,只需将 ZXingObjC 添加到您的 Cartfile 中。
github "TheLevelUp/ZXingObjC" ~> 3.6
CocoaPods
CocoaPods 是 Swift 和 Objective-C Cocoa 项目的依赖管理器。安装 CocoaPods 后,将 ZXingObjC 添加到您的 Podfile 中。
platform :ios, '8.0'
pod 'ZXingObjC', '~> 3.6.4'
示例
ZXingObjC 包含几个在 "examples" 文件夹中找到的示例应用程序。
- BarcodeScanner - 一款 iOS 应用程序,可以从相机捕获视频,扫描条码并在屏幕上显示结果。
- BarcodeScannerSwift - 一款 iOS 应用程序,可以从相机捕获视频,扫描条码并在屏幕上显示结果,完全用 Swift 重新编写。
感谢
- cwalcott 是本项目的初始创建者。
- neacao 因他的无与伦比的支持
- claybridges 对项目迁移的所有帮助
许可证
ZXingObjC 遵循 Apache 2.0 许可。