问题
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?
回答1:
I had the same issue. Here is workaround for this.
Open file
ZXingObjC\qrcode\encoder\ZXEncoder.m
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
returns9223372036854775807
on my 64-bit Mac andminPenalty
gets-1
value (sinceint
type cannot store such a big number).Replace the
NSIntegerMax
byINT_MAX
. It should return the correct value:2147483647
. That's the numberNSIntegerMax
returns on 32-bit machines according to the answer to this question.Run the app and you'll get your QR code!
回答2:
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
来源:https://stackoverflow.com/questions/16024161/creating-qr-barcodes-with-zxingobjc-on-mac