Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 507
David Gölzhäuser Avatar asked Mar 10 '13 09:03

David Gölzhäuser


People also ask

Does iOS have a built-in QR reader?

Learn how to use the built-in camera on your iPhone, iPad, or iPod touch to scan a Quick Response (QR) code for links to websites, apps, tickets, and more. QR codes give you quick access to websites without having to type or remember a web address. You can use the Camera app to scan a QR code.


2 Answers

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

like image 186
Guntis Treulands Avatar answered Oct 27 '22 08:10

Guntis Treulands


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.

like image 23
Simon Avatar answered Oct 27 '22 08:10

Simon