Forum Archive

Barcode / QR Code Camera Scanner Demo (objc_util)

omz

Someone asked me on Twitter if Pythonista had support for scanning barcodes, using the camera. While there's nothing like that built-in, I looked around a bit, and it turns out that there are APIs for this in Apple's AVFoundation framework, so I thought it might be interesting to build a wrapper around that.

This demo shows a live camera preview, and it plays a sound when a barcode or QR code is found in the viewfinder. The scanned code (a number for EAN codes, URLs or text for QR codes...) is shown in an overlay label. When the view is closed, a list of all (unique) codes that were scanned is printed to the console.

Here's the code: Barcode Scanner.py (Gist)

And a screenshot:

dgelessus

Is it also possible to read a barcode from an image file instead of the camera? (For when the QR code is so small that getting the camera to focus is almost impossible.) There are lots of apps that can scan barcodes normally, but I don't know of any that can scan existing image files.

omz

@dgelessus That should be possible. You'd probably use CoreImage instead of AVFoundation though – the approach would be very similar to the "Camera Scanner" demo in the bundled ObjC examples, which is using CIDetector with CIDetectorTypeRectangle. There's also CIDetectorTypeQRCode, which sounds like exactly what you're looking for.

vcr80

How can I push the scanner as a subview to a navigation view, get the first result it scanned, close the scanner an process the number, the scanner returned?

I already managed to load the scanner as a sub view of a navigation view but now I'm stuck.

jbap

Don't we have a qrcode module?

omz

@jbap That's for generating QR codes (from a URL etc.), not for reading them.

jbap

Just wondering if the qrcode module could be expanded to produce and read.

jbap

I tried this, and I'm just curious how I could use the photos module to scan. When my iPad is in any other orientation than the preset in this program, the camera does not shift. My iPad will turn, but the camera won't.

kareliap

Hi! I have been working with this script and I have been trying to get the picture from which the QR code was finally scanned? Do you know if this is possible with AVFoundation?

robertiii

This works great except when my ipad is turned sideways the camera acts weird and is like the camera is in mirror mode or something.

roseybrown

nice information. You have shared all the ideas very nicely. You may get more ideas from Canon Printer Paper Jam page.