11

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

username tbd
  • 9,152
  • 1
  • 20
  • 35
David Gölzhäuser
  • 3,525
  • 8
  • 50
  • 98
  • This is quite different what You have already done. Yes - You have camera preview, but You still need necessary algorithm to decode any qr code visible in image. As there exists existing possibilities (Zbar, Libzbar, zXingObjc, etc) - then better use them. – Guntis Treulands Mar 10 '13 at 09:52
  • zxing is compatible with iOS 6. – smparkes Mar 10 '13 at 15:20

6 Answers6

9

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

Guntis Treulands
  • 4,764
  • 2
  • 50
  • 72
  • Thank you but I need the ZBarCaptureReader and there is no documentary about this in the documentation. I already have the live camera feed in a UIImageView but I don't know how to read the feed. And I don't have a solid understanding of the AVCapture infrastructure. Can you please provide me some code? I'll update my question with my used code. – David Gölzhäuser Mar 10 '13 at 09:37
  • Thank you but how can I set the UIImageView that should be scanned? And should I set an Timer, so it scan In an Time interval or is this controlled by the SDK? – David Gölzhäuser Mar 10 '13 at 10:01
  • Is there a Way to use the front camera? – David Gölzhäuser Mar 10 '13 at 11:07
  • But I need the front cam, because the iPad is mounted in a Wall. Cant I just set a UIImageView to be Scanned by the ZBar SDK? – David Gölzhäuser Mar 10 '13 at 11:20
  • I dont get it how it work, can you please make it for me? Please iMessage me me if you like: david.goelzhaeuser@me.com – David Gölzhäuser Mar 10 '13 at 13:16
  • OK, your example will work but I cant use this Picker view thing, It must scan automatically, so without pushing a button. the Zbar code must scann a UIImageView for codes and display the string in a textview and must send the string to a database. I cant see how this will work with the SDK or with your example – David Gölzhäuser Mar 10 '13 at 13:42
  • Too bad for You, You cannot see :) This is just an example to start scanner on button touch and then remove scanner and show scanned image/code. This is easily tweak-able to show scanner all-time and update visible label with code that is scanned, without stopping scanner :) SDK is there for you to play with it! :) – Guntis Treulands Mar 10 '13 at 13:48
  • I am not a developer, I am just normal guy trying to make an app for his business, cant you provide me any code to solve this problem? Please! – David Gölzhäuser Mar 10 '13 at 13:56
  • Oh, I understand Your problem then. But I don't think I should do it all in Your place. Because - as far as I found out about this project - example I provided already is 90% done. It just needs different layout and a bit tweaked functionality. – Guntis Treulands Mar 10 '13 at 14:08
  • It would be nice If you would only do the QR Code Scanning thing. I will do the Database thing and the rest. Could you do this for me? – David Gölzhäuser Mar 10 '13 at 14:15
  • Maybe. Explain more what changes You want to scanner? – Guntis Treulands Mar 10 '13 at 14:16
  • Can you mail or iMessage me, so i can send you my existing project? david.goelzhaeuser@me.com – David Gölzhäuser Mar 10 '13 at 14:31
  • ZBar is the best! It took just a few hours to make the reading qrcodes feature work in my project! – Renato Lochetti Aug 09 '13 at 20:15
2

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.

Community
  • 1
  • 1
Simon
  • 25,468
  • 44
  • 152
  • 266
  • 2
    from: http://zbar.sourceforge.net/iphone/sdkdoc/licensing.html "referring to the GPL, which is significantly different from the LGPL referring to a different version of the LGPL; we intentionally use version 2.1, which has specific static linking exceptions. not a lawyer either and too lazy to read the whole license Basically, if you leverage the appropriate sections of the license, it should be fully compatible with the App Store restrictions and requirements." - so it would be ok to use it in appstore. – Guntis Treulands Mar 10 '13 at 09:39
  • I wont use it in the AppStore, it is for private use only, so I can use it. – David Gölzhäuser Mar 10 '13 at 09:40
  • 1
    @Guntis: I'm aware of what ZBar say on the matter; I think they're wrong. So do the FSF: http://www.fsf.org/blogs/licensing/more-about-the-app-store-gpl-enforcement – Simon Mar 10 '13 at 10:14
1

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

Klax
  • 43
  • 3
1

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

Jason
  • 2,223
  • 2
  • 20
  • 28
0

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

Zeezer
  • 1,503
  • 2
  • 18
  • 33
0

Check this out with apple natively implemented Qr code

iPC
  • 5,916
  • 3
  • 26
  • 38