How to create a QR Code reader for iOS

David Gölzhäuser picture David Gölzhäuser · Mar 10, 2013 · Viewed 19.6k times · Source

I am developing an app for our local business. I already have the live camera in a UIImageView, now I need to know how to read QR codes from the UIImageView and display the content (0000-KKP0-2013) in a label.

So basically I need a QR code scanner which is reading a QR code and save the content in a String. I already used ZXing ("Zebra Crossing") but it is not compatible with iOS 6 and it won't work. Is there an easy code for getting the QR Code content in a String?

Thank you!

This is the code I am using in my .m file:

#import "ZBarSDK.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize vImagePreview;             

- (void)viewDidUnload
{
    [super viewDidUnload];

    vImagePreview = nil;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];


    //----- SHOW LIVE CAMERA PREVIEW -----
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPreset352x288;

    /*CALayer *viewLayer = self.vImagePreview.layer;
    NSLog(@"viewLayer = %@", viewLayer);*/

    AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

    captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
    [self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];

    AVCaptureDevice *device = [self frontCamera];

    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (!input) {
        // Handle the error appropriately.
        NSLog(@"ERROR: trying to open camera: %@", error);

        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"QReader"
                              message:[NSString stringWithFormat:@"ERROR: Versuch die Kamera zu öffnen ist fehlgeschlagen [%@]",error]
                              delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

        alert.tag = 1;

        [alert show];
    }
    [session addInput:input];

    [session startRunning];

    }

- (AVCaptureDevice *)frontCamera {
    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for (AVCaptureDevice *device in devices) {
        if ([device position] == AVCaptureDevicePositionFront) {
            return device;
        }
    }
    return nil;
}

Now I need to know how to read the QR code from the vImagePreview with the ZBarSDK. And I cant use a UIPickerView

Answer

Guntis Treulands picture Guntis Treulands · Mar 10, 2013

Try ZBar: http://zbar.sourceforge.net/iphone/sdkdoc/install.html

We are using it successfully in our application which supports iOS 4 up to iOS 6.1

In my case I use ZBarReaderView - to see a camera preview, which automatically detects and returns scanned code.

You'll need:

#import "ZBarSDK.h"  


ZBarReaderView *readerView;

add this : <ZBarReaderViewDelegate>

and then:

[readerView.scanner setSymbology:ZBAR_QRCODE config:ZBAR_CFG_ENABLE to:0]; 
readerView.readerDelegate = self;

[readerView start];

- (void)readerView:(ZBarReaderView *)view didReadSymbols: (ZBarSymbolSet *)syms fromImage:(UIImage *)img
{
    for(ZBarSymbol *sym in syms) 
    {  
        NSLog(@"Did read symbols: %@", sym.data);

    }
}

Anyways, just follow these instructions:

http://zbar.sourceforge.net/iphone/sdkdoc/tutorial.html

and then try it out - see if it works for You.

EDIT

Here I uploaded example project I took from here: https://github.com/arciem/ZBarSDK

It has enabled front facing camera. Tested - successfully reads qr code using front facing camera:

http://www.speedyshare.com/fkvqt/download/readertest.zip

or

Once application starts - front camera is shown - scanner is 200x200 large and as a subview. http://www.speedyshare.com/QZZU5/download/ReaderSample-v3.zip