Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to use ZbarSDK in Swift

#How to use ZbarSDK in Swift(XCode 6.2, base SDK iOS 8.2)

##Steps

  1. Download ZbarSDK and drag ZbarSDK folder in Swift project, setup buiding settings according to the zBarSDK tutorial
  2. Add #import "ZBarSDK.h" in YourApp_Bridging_Header file
  3. In your ViewController add ZBarReaderDelegate, setup ZBarReader, receive barcode data

##Posible building issues:

  1. Missing required architecture arm64 in file ...
    Reason: the libzbar.a is not compiled to arm64 architecture
    Solution: download libzbar.a compiled for arm64, and replace the libzbar.a in your project download link: (https://www.dropbox.com/sh/xoo1ql3yxzmu6wz/lV8vJFqJCE)
import UIKit
//This extension is used for the "for symbol in results as ZBarSymbolSet" 
extension ZBarSymbolSet: SequenceType {
    public func generate() -> NSFastGenerator {
        return NSFastGenerator(self)
    }
}
class MyViewController: UIViewController, UITextFieldDelegate, ZBarReaderDelegate {
    var ZBarReader: ZBarReaderViewController?
    
    @IBOutlet var barCodeText: UITextField!

    @IBAction func scanAction(sender: AnyObject) {
        if (self.ZBarReader == nil) {
            self.ZBarReader = ZBarReaderViewController()
        }
        self.ZBarReader?.readerDelegate = self
        self.ZBarReader?.scanner.setSymbology(ZBAR_UPCA, config: ZBAR_CFG_ENABLE, to: 1)
        self.ZBarReader?.readerView.zoom = 1.0
        self.ZBarReader?.modalInPopover = false
        self.ZBarReader?.showsZBarControls = false
        navigationController?.pushViewController(self.ZBarReader!, animated:true)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.barCodeText.delegate = self
    }
    
    func imagePickerController(reader:UIImagePickerController,
        didFinishPickingMediaWithInfo info: NSDictionary) {
        // ADD: get the decode results
        var results: NSFastEnumeration = info.objectForKey(ZBarReaderControllerResults) as NSFastEnumeration
        
        var symbolFound : ZBarSymbol?
        
        for symbol in results as ZBarSymbolSet {
            symbolFound = symbol as? ZBarSymbol
            break
        }
        var resultString = NSString(string: symbolFound!.data)
        self.barCodeText.text = resultString    //set barCode 
        
        navigationController?.popViewControllerAnimated(true)
    }

}
@nikunjazilen

This comment has been minimized.

Copy link

commented Nov 11, 2016

Hi,
I am installed pod version 1.3 for ZbarSdk and integrate your code in app. But I am facing build error issue in below line of code.
extension ZBarSymbolSet: SequenceType { // Error Type ZBarSymbolSet does not conform to protocol 'Sequence'
public func generate() -> NSFastGenerator { // Use of undeclared type 'NSFastGenerator'

func imagePickerController(reader:UIImagePickerController,
didFinishPickingMediaWithInfo info: NSDictionary) // its give me warning

for symbol in results as ZBarSymbolSet // Cannot convert value of type 'NSFastEnumeration' to type 'ZBarSymbolSet' in coercion.

All error are raised while build the app in Xcode 8.1. So please suggest or any idea for this. Your help will be me appreciate me.

Thanks
Nikunj Jadav

@CanTheAlmighty

This comment has been minimized.

Copy link

commented Feb 22, 2017

Here's the Swift 3 version

extension ZBarSymbolSet: Sequence
{
    public typealias Element  = ZBarSymbol
    public typealias Iterator = NSFastEnumerationIterator
    
    public func makeIterator() -> NSFastEnumerationIterator
    {
        return NSFastEnumerationIterator(self)
    }
}

And then the delegate method is simply

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
    guard let symbols = info[ZBarReaderControllerResults] as? ZBarSymbolSet else { return }

    for symbol in symbols
    {
        if let symbol = symbol as? ZBarSymbol, let data = symbol.data
        {
            // Now you can use data (String)
        }
    }
}
@ethanwa

This comment has been minimized.

Copy link

commented Nov 7, 2017

In Swift 4 I am getting the following error:

'Sequence' requires the types 'ZBarSymbolSet.Element' (aka 'ZBarSymbol') and 'Any' be equivalent

Commenting out public typealias Element = ZBarSymbol seems to fix it.

@shahmaulik

This comment has been minimized.

Copy link

commented Aug 23, 2018

Use AVFoundation instead as ZBar for arm64 creates issues.

https://www.hackingwithswift.com/example-code/media/how-to-scan-a-barcode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.