Reputation: 1337
Is there an objective-c library which will allow me to generate QRCodes offline? Thanks
Upvotes: 2
Views: 1968
Reputation: 878
Just a simple and native way of generating the QR code:
(CIImage *)createQRForString:(NSString *)qrString {
NSData *stringData = [qrString dataUsingEncoding: NSISOLatin1StringEncoding];
CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[qrFilter setValue:stringData forKey:@"inputMessage"];
return qrFilter.outputImage; }
Upvotes: 0
Reputation: 8482
In Mavericks and iOS7 QR code generation is part of Core Image. You just use the CIQRCodeGenerator filter. On Github you can find a class which implements this for iOS. I've adapted this code to get the OS X compatible code below:
NSString *website = @"http://stackoverflow.com/";
NSData *urlAsData = [website dataUsingEncoding:NSUTF8StringEncoding];
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setDefaults];
[filter setValue: urlAsData forKey:@"inputMessage"];
[filter setValue:@"M" forKey:@"inputCorrectionLevel"];
CIImage *outputImage = [filter valueForKey:kCIOutputImageKey];
If you want to draw the CIImage there are several possibilities. You can create an NSImage
like this:
CIContext *context = [[NSGraphicsContext currentContext] CIContext];
CGImageRef cgImage = [context createCGImage:outputImage
fromRect:[outputImage extent]];
NSImage *image = [[NSImage alloc] initWithCGImage:cgImage size:NSZeroSize];
But this image will typicall be much smaller than you want. I believe each black dot in the QR code just becomes one pixel. Not quite what you want. To scale up the image without making it blurry, do this:
NSSize largeSize = NSMakeSize(image.size.width * 10, image.size.height * 10);
[image setScalesWhenResized:YES];
NSImage *largeImage = [[NSImage alloc] initWithSize:largeSize];
[largeImage lockFocus];
[image setSize:largeSize];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationNone];
[image drawAtPoint:NSZeroPoint fromRect:CGRectMake(0, 0, largeSize.width, largeSize.height) operation:NSCompositeCopy fraction:1.0];
[largeImage unlockFocus];
largeImage
is then your result image which you can display.
If you want to decode a QR you use AVFoundation as explained in this blog. Unfortunately this seems to only be supported on iOS7 at the moment.
Upvotes: 3
Reputation: 111
See: https://github.com/jverkoey/ObjQREncoder#readme
To use
#import <QREncoder/QREncoder.h>
UIImage* image = [QREncoder encode:@"http://www.google.com/"];
Upvotes: 6