Shahin
Shahin

Reputation: 885

Creating QR barcodes with ZXingObjC on Mac

I'm trying to use http://github.com/TheLevelUp/ZXingObjC to create QR codes on my Mac app.

It works for every barcode types, but returns nil on QRcode! both 'result' and 'error' is empty. here's my code:

NSError* error = nil;
ZXMultiFormatWriter* writer = [[ZXMultiFormatWriter alloc] init];
ZXBitMatrix* result = [writer encode:@"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678"
                              format:kBarcodeFormatQRCode
                               width:1750
                              height:1750 hints:[[ZXEncodeHints alloc] init] error:&error];
if (result) {
    CGImageRef image = [[ZXImage imageWithMatrix:result] cgimage];
    self.image.image = [[NSImage alloc] initWithCGImage:image size:NSMakeSize(1750, 1750)];
} else {

    NSLog(@"error: %@", error);
}

What's wrong on it?

Upvotes: 4

Views: 1244

Answers (2)

Andrey Gordeev
Andrey Gordeev

Reputation: 32449

I had the same issue. Here is workaround for this.

  1. Open file ZXingObjC\qrcode\encoder\ZXEncoder.m

  2. Find this row: int minPenalty = NSIntegerMax;. There must be a warning on it: Implicit conversion from 'long' to 'int' changes 9223372036854775807 to -1. That's the reason of the problem. NSIntegerMax returns 9223372036854775807 on my 64-bit Mac and minPenalty gets -1 value (since int type cannot store such a big number).

  3. Replace the NSIntegerMax by INT_MAX. It should return the correct value: 2147483647. That's the number NSIntegerMax returns on 32-bit machines according to the answer to this question.

  4. Run the app and you'll get your QR code!

Upvotes: 4

Daniel Arantes Loverde
Daniel Arantes Loverde

Reputation: 2314

Try to use another method, not this with HINTS, use just:
[writer encode:@"yourmeganumber" format:kBarcodeFormatQRCode width:xxxx height:xxxx error:&error];

This works for me
Try and let me know

Upvotes: 0

Related Questions