How to Scan Barcodes on Ios

How can I scan barcodes on iOS?

We produced the 'Barcodes' application for the iPhone. It can decode QR Codes. The source code is available from the zxing project; specifically, you want to take a look at the iPhone client and the partial C++ port of the core library. The port is a little old, from circa the 0.9 release of the Java code, but should still work reasonably well.

If you need to scan other formats, like 1D formats, you could continue the port of the Java code within this project to C++.

EDIT: Barcodes and the iphone code in the project were retired around the start of 2014.

Scanning Barcode or QR code in Swift 3.0 using AVFoundation

The first step needs to be declare access to any user private data types that is a new requirement in iOS 10. You can do it by adding a usage key to your app’s Info.plist together with a purpose string.

Because if you are using one of the following frameworks and fail to declare the usage your app will crash when it first makes the access:

Contacts, Calendar, Reminders, Photos, Bluetooth Sharing, Microphone, Camera, Location, Health, HomeKit, Media Library, Motion, CallKit, Speech Recognition, SiriKit, TV Provider.

To avoid the crash you need to add the suggested key to Info.plist:

Sample Image

And then the system shows the purpose string when asking the user to allow access:

Sample Image

For more information about it you can use this article:

  • Privacy Settings in iOS 10

I have done a little modifications to your BarcodeViewController to make it work properly as you can see below:

BarcodeViewController

import UIKit
import AVFoundation

protocol BarcodeDelegate {
func barcodeReaded(barcode: String)
}

class BarcodeViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

var delegate: BarcodeDelegate?

var videoCaptureDevice: AVCaptureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
var device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
var output = AVCaptureMetadataOutput()
var previewLayer: AVCaptureVideoPreviewLayer?

var captureSession = AVCaptureSession()
var code: String?

override func viewDidLoad() {
super.viewDidLoad()

self.view.backgroundColor = UIColor.clear
self.setupCamera()
}

private func setupCamera() {

let input = try? AVCaptureDeviceInput(device: videoCaptureDevice)

if self.captureSession.canAddInput(input) {
self.captureSession.addInput(input)
}

self.previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

if let videoPreviewLayer = self.previewLayer {
videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
videoPreviewLayer.frame = self.view.bounds
view.layer.addSublayer(videoPreviewLayer)
}

let metadataOutput = AVCaptureMetadataOutput()
if self.captureSession.canAddOutput(metadataOutput) {
self.captureSession.addOutput(metadataOutput)

metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code]
} else {
print("Could not add metadata output")
}
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

if (captureSession.isRunning == false) {
captureSession.startRunning();
}
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

if (captureSession.isRunning == true) {
captureSession.stopRunning();
}
}

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
// This is the delegate's method that is called when a code is read
for metadata in metadataObjects {
let readableObject = metadata as! AVMetadataMachineReadableCodeObject
let code = readableObject.stringValue


self.dismiss(animated: true, completion: nil)
self.delegate?.barcodeReaded(barcode: code!)
print(code!)
}
}
}

One of the important points was to declare the global variables and start and stop the captureSession inside the viewWillAppear(:) and viewWillDisappear(:) methods. In your previous code I think it was not called at all as it never enter inside the method to process the barcode.

I hope this help you.

Get Permission to Scan Barcodes on iOS with Flutter

You can use permission_handler. With this, you could ask for camera permission before build QRView. Of course, you should build QRView after camera permission is enabled.
I'm not sure it would be right solution for your issue, but I think that would be an awesome improvement.

Scanning barcode in iOS 8 and displaying barcode

There are two components to a barcode: 1) the image of the barcode itself and 2) the data that can be extracted from the barcode. iOS 7+ supports finding a barcode in an image and also extracting the data from that image without external libraries. There are many strong tutorials and answers related to barcode recognition but you are probably looking for this one:
http://www.infragistics.com/community/blogs/torrey-betts/archive/2013/10/10/scanning-barcodes-with-ios-7-objective-c.aspx

I can think of two ways to achieve what you wish, capturing a barcode then displaying it on-screen: 1) crop the larger image where the barcode was detected so that only the barcode is left, then display that, or 2) once a barcode is detected create a new barcode. The advantage of the first method is you can crop slightly larger so the barcode will clearly appear like the original but any imperfections (stains, shadows, etc...) will be left. The advantage of the second is you will have a crystal clear barcode but the user experience might appear a little disjointed (or it may look like magic: you'll have "cleaned up" a smudged barcode). Choosing one depends on the type of user experience you wish for.

If you choose to crop the image tell iOS to detect the barcode, as per the tutorial above, and when the barcode is detected use the the bounds of the barcode object to crop the underlying overall image, a UIImage object. If you choose to create a barcode using the data you extracted from the original barcode it looks like you will need a library. There is a question that addresses that here:
Barcode Generation from within iOS App



Related Topics



Leave a reply



Submit