idoodler
idoodler

Reputation: 3545

How to create a QR Code reader for iOS

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

Upvotes: 11

Views: 20909

Answers (6)

iPC
iPC

Reputation: 6168

Check this out with apple natively implemented Qr code

Upvotes: 0

Jason
Jason

Reputation: 2353

OP was looking for something that supported iOS6 two years ago, but for anyone else coming along, this one that I went with wraps the built-in iOS7 functionality:

https://github.com/mikebuss/MTBBarcodeScanner

Upvotes: 1

Zeezer
Zeezer

Reputation: 1533

Anyone looking to implement this in Swift. Check this out: https://github.com/aeieli/swiftQRCode

Need to change a few syntax errors, otherwise fully working on iOS 8.1

Upvotes: 0

Klax
Klax

Reputation: 43

if you want to test the qr codes here are some apps for iphone that might come in handy. iphone qr scanner

Upvotes: 1

Guntis Treulands
Guntis Treulands

Reputation: 4762

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

Upvotes: 9

Simon
Simon

Reputation: 25993

We looked into this not long ago. ZBar looks good, but it's LGPL-licensed, which is not suitable for use on the App Store. In the end I went with ZXingObjC.

Upvotes: 2

Related Questions