Skip to content

Instantly share code, notes, and snippets.

@wszdwp
Last active April 22, 2019 13:48
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wszdwp/205dcccbf096c21f6288 to your computer and use it in GitHub Desktop.
Save wszdwp/205dcccbf096c21f6288 to your computer and use it in GitHub Desktop.
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)
    }

}
@shahmaulik
Copy link

shahmaulik 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