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) {
                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.


Reading MetaData With The iPhone Camera

I haven’t posted for a while. So I thought I would just give a brief report of something I’m working on.

There are many cases where you might wish to read bar codes, QR codes, or what have you for your iPhone app. It turns out to be pretty easy to setup. I’ve got code that reads metadata and sends it to the debugger as plain text. My next step is to display the data in a new view and, hopefully, also draw the code that produced the data.

I don’t yet know if the generation of codes is built into iOS. If it isn’t, I won’t bother for now. Right now I’m interested in being able to read the data. Still, it is possible for you to create a custom QR code right now. Just go to this site:


They have a lot of interesting stuff there. You can generate a free QR code without having to sign in or anything. It’s pretty cool.

So what metadata can the iPhone read? Well, as of iOS 8, it can read the following list of metadata as printed out by println() into the debugger:

  • face
  • org.iso.Aztec
  • org.iso.Code128
  • org.iso.Code39
  • org.iso.Code39Mod43
  • com.intermec.Code93
  • org.iso.DataMatrix
  • org.gs1.EAN-13
  • org.gs1.EAN-8
  • org.ansi.Interleaved2of5
  • org.gs1.ITF14
  • org.iso.PDF417
  • org.iso.QRCode
  • org.gs1.UPC-E


It’s probably quite obvious from the list where the face recognition comes from. The rest of the list is a surprising variety of codes that can, among other things, read bar codes on food products, ISBN codes on books, shipping labels, and so on.

Not a bad deal.

As for the QR code I generated? Well, it contains the URL to my website, naturally!

QR Code
QR Code


Child of Twenty Fifteen

I’ve gained a new respect for the people who work on WordPress, WordPress Themes, and general web monkey work. I’ll tell you why.

I’m not a web monkey. That is, I do not make my living by writing HTML, CSS, or JavaScript to run in browsers. Ten years ago, I rebooted my blog. I’ve actually had a personal website since 1997. It has undergone several incarnations, including being 404 for a while when I had no hosting arrangement. The 2005 reboot was hand written using XHTML 1.0 and CSS. There was no JavaScript. Well, perhaps a little. But it was code inserted from a third party.

Back then, there was no iPhone. There were no tablets. There wasn’t even Chrome. Safari was pretty primitive back then. When I endeavored to build my site, I stuck with the W3C standards of the time. I had no patience for working around browser bugs or other glitches. There was a purity to my site. I was actually quite happy with it.

Over time, I lost interest in it. I wasn’t making new entries. I just let it sit. Time passed. While other things did happen, I’m going to fast forward to 2015. Things have changed. They’ve changed a lot. I wanted to reboot my site again. This time, I didn’t want to write HTML or CSS. I certainly didn’t want to write JavaScript. I also wanted to have a proper Content Management System (CMS) like the big guys use. I ended up choosing WordPress.

This lead me to a bit of a quandary. Choices. WordPress is customizable with Themes. And there are a heck of a lot of them. Some are free. Some cost money. I didn’t want to pay more money. I had just spent money on a five year renewal of my domain and web hosting. I won’t go into the wonderful options that are available for someone who wants to create their own website in this post. Suffice it to say things have come a long way. I even have a free TLS certificate so that I can run HTTPS instead of HTTP. That doesn’t work for hot linking from an HTTP only site (the padlock disappears due to mixed content).

I settled on the Twenty Fifteen theme packaged with WordPress. It’s a nice theme that reformats the content when smaller screens are used. It does clever things that I couldn’t imagine doing ten years ago. I can add widgets. Search is one of them. As I add content to this site, search will probably become rather more useful.

The theme you are looking at is not the Twenty Fifteen theme. It is a Child Theme derived from Twenty Fifteen. Twenty Fifteen is a good theme. Unfortunately it is very liberal in use of white space. I did not want to let this precious screen real estate go to waste. I had several options. One was to simply choose another theme. Did I mention there are lots of themes out there? I didn’t want to spend more money either.

At first, I tried hacking away at the style.css file for Twenty Fifteen. That was a disaster. I ended up reverting all my changes. I needed help. Fortunately, WordPress has support forums. So I asked for help. In a very short time, I had the exact help I needed. The solution was to create a Child Theme. No matter how much I screwed that up, I could always switch back to the default Twenty Fifteen theme with no harm no foul. Beautiful.

I set about aggressively reducing white space consumption. So much for not messing with CSS. Even worse, I also had to do a tiny bit of PHP. Fortunately not much. Although even the little bit I did managed to screw things up. The support forums came to my rescue again. A stephencottontail gave me the help I needed.

I still have a little bit more work to do. The left side menu, (About, Privacy, Blog Roll), uses too much vertical space. Obviously I need to add some more CSS to fix that. I will be creating my own custom footer when I figure out what I need to put there. I’m sure I also missed some bits too.

The important thing is that the site is now working properly. Or mostly working properly. Whether I have good taste or not is another matter. I’ve placed my Child Theme under source control on GitHub. It is free to use. Hopefully I can get a bit more feedback on improvements.


It would almost certainly have been easier to simply locate another theme. On the plus side, this has proved to be an educational exercise that leads to me tweaking my site to look exactly how I want it too.