我的问题如下:我想用 ZXingObjC 生成一个数据矩阵并显示它。
目前我正在用 XCode 7.3 和 Swift 2 编写一个应用程序。必须生成一个带有字符串值的数据矩阵。作为用Objective C编写的ZXingObjC,我尝试按照这个例子自己转换:
NSError *error = nil; ZXMultiFormatWriter *writer = [ZXMultiFormatWriter writer]; ZXBitMatrix* result = [writer encode:@"A string to encode" 格式:kBarcodeFormatQRCode 宽度:500 高度:500 错误:&错误]; 如果(结果){ CGImageRef image = [[ZXImage imageWithMatrix:result] cgimage]; } 别的 { NSString *errorMessage = [错误本地化描述]; }
当我尝试使用 writer.encode(...) 对我的字符串进行编码时会出现问题,我认为我尝试使用更长和更小的字符串是很好的,但结果仍然相同。
做 { 让 writer = ZXMultiFormatWriter() 让提示 = ZXEncodeHints() 作为 ZXEncodeHints let result = try writer.encode("foo", format: kBarcodeFormatDataMatrix, width: 300, height: 300, hints: hints) 让 imageRef = ZXImage.init(矩阵:结果) 图片.image = UIImage(CGImage:imageRef.cgimage) } 抓住 { 打印(错误) }
最后,应用程序崩溃并出现此异常:
*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“找不到与消息匹配的符号排列。数据码字:17' *** 首先抛出调用堆栈: ( 0 核心基础 0x00000001081b7d85 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010854cdeb objc_exception_throw + 48 2 核心基础 0x00000001081b7cbd +[NSException raise:format:] + 205 3 ZXingObjC 0x0000000107eb320a +[ZXDataMatrixSymbolInfo 查找:形状:最小尺寸:最大尺寸:失败:] + 1178 4 ZXingObjC 0x0000000107ead4bb -[ZXDataMatrixEncoderContext updateSymbolInfoWithLength:] + 379 5 ZXingObjC 0x0000000107ea2866 -[ZXDataMatrixC40Encoder 编码:] + 342 6 ZXingObjC 0x0000000107eaf032 +[ZXDataMatrixHighLevelEncoder encodeHighLevel:shape:minSize:maxSize:] + 1330 7 ZXingObjC 0x0000000107eb75b0 -[ZXDataMatrixWriter 编码:格式:宽度:高度:提示:错误:] + 672 8 ZXingObjC 0x0000000107ee246c -[ZXMultiFormatWriter 编码:格式:宽度:高度:提示:错误:] + 1100 9 DemoAppZXing 0x0000000103f54d98 _TFC4DemoAppZXing27VSMatrixOrderViewController11viewDidLoadfT_T_ + 8472 10 DemoAppZXing 0x0000000103f58002 _TToFC4DemoAppZXing27VSMatrixOrderViewController11viewDidLoadfT_T_ + 34 11 UIKit 0x0000000106a95984-[UIViewController loadViewIfRequired] + 1198 12 UIKit 0x0000000106a9b93b -[UIViewController __viewWillAppear:] + 120 13 UIKit 0x0000000106acb750-[UINavigationController _startCustomTransition:] + 1203 14 UIKit 0x0000000106adbb9b-[UINavigationController _startDeferredTransitionIfNeeded:] + 712 15 UIKit 0x0000000106adcd0b -[UINavigationController __viewWillLayoutSubviews] + 57 16 UIKit 0x0000000106c8b503 -[UILayoutContainerView layoutSubviews] + 248 17 UIKit 0x00000001069b5980-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703 18 QuartzCore 0x000000010a2bfc00 -[CALayer layoutSublayers] + 146 19 石英核心 0x000000010a2b408e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 20 石英核心 0x000000010a2b3f0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 21 石英核心 0x000000010a2a83c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 22 石英核心 0x000000010a2d6086 _ZN2CA11Transaction6commitEv + 486 23 UIKit 0x00000001068f572e _UIApplicationHandleEventQueue + 7135 24 核心基础 0x00000001080dd301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 25 核心基础 0x00000001080d322c __CFRunLoopDoSources0 + 556 26 核心基础 0x00000001080d26e3 __CFRunLoopRun + 867 27 核心基础 0x00000001080d20f8 CFRunLoopRunSpecific + 488 28 图形服务 0x000000010a8a1ad2 GSEventRunModal + 161 29 UIKit 0x00000001068faf09 UIApplicationMain + 171 30 DemoAppZXing 0x0000000103f60ec2 主 + 114 31 libdyld.dylib 0x000000010905492d 开始 + 1 ) libc++abi .dylib:以 NSException 类型的未捕获异常终止
编辑 :
要解决这个问题,我只需要设置 minSize 和 maxSize。
让提示 = ZXEncodeHints() 作为 ZXEncodeHints hints.maxSize = ZXDimension.init(宽度:1000,高度:1000) hints.minSize = ZXDimension.init(width: 1, height: 1) 让结果 = 尝试 writer.encode(生成的Str [0],格式:kBarcodeFormatDataMatrix,宽度:500,高度:500,提示:提示)