Xcode 6.2: Swift iOS App That Reads Bar Codes

In my last post, I spoke about reading barcodes in iOS. I’m sure others have also spoken on the topic. Certainly there are some apps out there. For example there is a nice QR code reader called QR Code Reader from Kaywa. It’s a nice little free utility for iPhone and iPad. I’m going to talk about a very simple program I wrote in Swift 1.1 using Xcode 6.2 that reads all the barcodes mentioned in my last post.

As simple as the app is, it uses several technologies that can start to get to get complicated. Fortunately that isn’t the case in this program. Specifically, AVFoundation, Grand Central Dispatch, and Segues are used. AVFoundation has the barcode reading functionality. GCD is used to manage threads. Segues are used programmatically to switch from the scanner view and the data display view.

HelloUPC barcode reader project. I think you’ll like the license.

The project starts out as a single view iOS application. There was no reason to make it iPhone only so it is a universal app. Now is the part of the blog where I wonder how much code I should bother to explain when you can just read it. Although I’m not big on code comments. So I should explain somethings perhaps. To make it easier on me, I’m going to assume you have some familiarity with the AVCam demo code (written in Objective-C) by Apple. The demo code is available from the Xcode documentation.

The important file in my project is ViewController.swift. What a surprise. That is a template file generated when creating the project. It’s the initial view controller that starts up when the app is launched. A view controller needs a view. That is defined in the CameraView.swift file. Things are kept extra simple by not having any controls in this view.

To get things rolling, it is necessary to setup the camera. When the view is loaded, the checkDeviceAuthorizationStatus() function is called. This packages up several procedures in one call:

  1. Request access to the rear camera.
  2. Configure the camera on a session thread that is defined in the AppDelegate.swift file.
  3. As part of the configuration, setup a metadata collection object that reads the barcodes.

 

All these things are done in Swift rather than Objective-C of course. And I also threw in my own ad hoc code style. Step 3 above is in this code snippet from the configure() function I use to setup the capture session.

            if captureSession.canAddOutput(metaDataOutput) {
                captureSession.addOutput(metaDataOutput)
                metaDataOutput.metadataObjectTypes =
                    metaDataOutput.availableMetadataObjectTypes.filter() {
                        $0 as NSString != "face"
                }
                metaDataOutput.setMetadataObjectsDelegate(self, queue: sessionQueue)
            }

 

The metaDataOutput object was defined at the top of the ViewController class. There is also a delegate set. The view controller is the delegate. It has to follow the AVCaptureMetadataOutputObjectsDelegate protocol. Only one function needs to be implemented for that protocol which is the captureOutput() function.

The work is done by the framework. It is still necessary to do something in the captureOutput() function. That’s called when the camera session “sees” one of the barcodes it recognizes. It gets passed the data which is then used to segue over to the display view where you can see the type of code plus the data from the code.

The code view only has labels and a button. The button segues you back to the scanner view to start over.

I didn’t write this code to be a stand alone app. I wrote it to learn how to read codes and do something useful with them. In this case, I simply display the values. This was also the first time I’ve used segues. It’s also the first time I’ve read barcodes.

The performance of the app is only so so. At least the debug version is. Some codes won’t read or take a while to get recognized on my iPhone 5s. Lighting seems to make a difference. Viewing angle doesn’t seem to be a huge issue. With the EAN-13 codes, it sometimes helps to get the code at a slight angle. I tried to read a printed stamp on an envelope. It took a while for the code to recognize the type of code. But no data was displayed. The code assumes the value will be a string.

There were also small barcodes of some type on some apples (the fruit). The iPhone 5s was not able to read them. There are some other codes that don’t seem to read well. There are also a surprising number of codes that do read well. The QR codes and DataMatrix codes have a lot of data. They can be fun.

I’m not sure what comes next.

I hope it’s good.

 

Published by

David S

David Steuber

I'm the administrator of this facility.