2

My problem is the following: I want to generate a data matrix with ZXingObjC and show it.

Currently i'm writing an application with XCode 7.3 and Swift 2.have to generate a data matrix with a String value. As the ZXingObjC written in Objective C, i tried to convert it myself according to this example:


    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 = [[ZXImage imageWithMatrix:result] cgimage];

    } else {
      NSString *errorMessage = [error localizedDescription];
    }

The problem occurs when i try to encode my String with writer.encode(...) and i think it's good to precise that i tried with longer and smaller string but the result still the same.


    do {
        let writer = ZXMultiFormatWriter()
        let hints = ZXEncodeHints() as ZXEncodeHints
        let result = try writer.encode("foo", format: kBarcodeFormatDataMatrix, width: 300, height: 300, hints: hints)

        let imageRef = ZXImage.init(matrix: result)
        picture.image = UIImage(CGImage: imageRef.cgimage)
    }
    catch {
        print(error)
    }

And finally the app crash with this exception :


    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't find a symbol arrangement that matches the message. Data codewords: 17'
    *** First throw call stack:
    (
        0   CoreFoundation                      0x00000001081b7d85 __exceptionPreprocess + 165
        1   libobjc.A.dylib                     0x000000010854cdeb objc_exception_throw + 48
        2   CoreFoundation                      0x00000001081b7cbd +[NSException raise:format:] + 205
        3   ZXingObjC                           0x0000000107eb320a +[ZXDataMatrixSymbolInfo lookup:shape:minSize:maxSize:fail:] + 1178
        4   ZXingObjC                           0x0000000107ead4bb -[ZXDataMatrixEncoderContext updateSymbolInfoWithLength:] + 379
        5   ZXingObjC                           0x0000000107ea2866 -[ZXDataMatrixC40Encoder encode:] + 342
        6   ZXingObjC                           0x0000000107eaf032 +[ZXDataMatrixHighLevelEncoder encodeHighLevel:shape:minSize:maxSize:] + 1330
        7   ZXingObjC                           0x0000000107eb75b0 -[ZXDataMatrixWriter encode:format:width:height:hints:error:] + 672
        8   ZXingObjC                           0x0000000107ee246c -[ZXMultiFormatWriter encode:format:width:height:hints:error:] + 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  QuartzCore                          0x000000010a2b408e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
        20  QuartzCore                          0x000000010a2b3f0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
        21  QuartzCore                          0x000000010a2a83c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
        22  QuartzCore                          0x000000010a2d6086 _ZN2CA11Transaction6commitEv + 486
        23  UIKit                               0x00000001068f572e _UIApplicationHandleEventQueue + 7135
        24  CoreFoundation                      0x00000001080dd301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
        25  CoreFoundation                      0x00000001080d322c __CFRunLoopDoSources0 + 556
        26  CoreFoundation                      0x00000001080d26e3 __CFRunLoopRun + 867
        27  CoreFoundation                      0x00000001080d20f8 CFRunLoopRunSpecific + 488
        28  GraphicsServices                    0x000000010a8a1ad2 GSEventRunModal + 161
        29  UIKit                               0x00000001068faf09 UIApplicationMain + 171
        30  DemoAppZXing                        0x0000000103f60ec2 main + 114
        31  libdyld.dylib                       0x000000010905492d start + 1
    )
    libc++abi

.dylib: terminating with uncaught exception of type NSException

Edit :

To solve this problem, i just need to set the minSize and the maxSize.


    let hints = ZXEncodeHints() as ZXEncodeHints
    hints.maxSize = ZXDimension.init(width: 1000, height: 1000)
    hints.minSize = ZXDimension.init(width: 1, height: 1)

    let result = try writer.encode(generatedStr[0], format: kBarcodeFormatDataMatrix, width: 500, height: 500, hints: hints)

Mathieu D.
  • 31
  • 5
  • Since the error is in `+[ZXDataMatrixSymbolInfo lookup:shape:minSize:maxSize:fail:] + 1178`, I would suspect a missing value in an earlier configuration or more likely values in your `hints` dictionary. – Price Ringo Apr 22 '16 at 13:31
  • There is no earlier configuration in this case. But even with that i don't see what i can do if i missed something. – Mathieu D. Apr 22 '16 at 14:03

2 Answers2

1

The method for returning the UIImage in the final form you need:

func generateDataMatrixQRCode(from string: String) -> UIImage? {
    do {
        let writer = ZXMultiFormatWriter()
        let hints = ZXEncodeHints() as ZXEncodeHints
        let result = try writer.encode(string, format: kBarcodeFormatDataMatrix, width: 1000, height: 1000, hints: hints)

        if let imageRef = ZXImage.init(matrix: result) {
            if let image = imageRef.cgimage {
                return UIImage.init(cgImage: image)
            }
        }
    }
    catch {
        print(error)
    }
    return nil
}
asheyla
  • 3,175
  • 5
  • 18
  • 34
0

I think thats a "generic error" not tied to Swift but rather Zebra Crossing/ZXing. Try making the rect bigger, might help.

let result = try writer.encode("foo", format: kBarcodeFormatDataMatrix, width: 1000, height: 1000, hints: hints)
Mikael Hellman
  • 2,664
  • 14
  • 22