Code Monkey home page Code Monkey logo

barcodescanner-1's Introduction

BarcodeScanner

CI Status Version Swift Carthage Compatible License Platform

Description

BarcodeScanner is a simple and beautiful wrapper around the camera with barcode capturing functionality and a great user experience.

  • Barcode scanning.
  • State modes: scanning, processing, unauthorized, not found.
  • Handling of camera authorization status.
  • Animated focus view and custom loading indicator.
  • Torch mode switch.
  • Customizable colors, informational and error messages.
  • No external dependencies.
  • Demo project.

Table of Contents

BarcodeScanner Icon

Usage

Controller

To start capturing just instantiate BarcodeScannerController, set needed delegates and present it:

let controller = BarcodeScannerController()
controller.codeDelegate = self
controller.errorDelegate = self
controller.dismissalDelegate = self

present(controller, animated: true, completion: nil)
BarcodeScanner scanning

You can also push BarcodeScannerController to your navigation stack:

let controller = BarcodeScannerController()
controller.codeDelegate = self

navigationController?.pushViewController(controller, animated: true)

Delegates

Code delegate

Use BarcodeScannerCodeDelegate when you want to get the captured code back.

extension ViewController: BarcodeScannerCodeDelegate {

  func barcodeScanner(_ controller: BarcodeScannerController, didCaptureCode code: String, type: String) {
    print(code)
    controller.reset()
  }
}

Error delegate

Use BarcodeScannerErrorDelegate when you want to handle session errors.

extension ViewController: BarcodeScannerErrorDelegate {

  func barcodeScanner(_ controller: BarcodeScannerController, didReceiveError error: Error) {
    print(error)
  }
}

Dismissal delegate

Use BarcodeScannerDismissalDelegate to handle "Close button" tap. Please note that BarcodeScannerController doesn't dismiss itself if it was presented initially.

extension ViewController: BarcodeScannerDismissalDelegate {

  func barcodeScannerDidDismiss(_ controller: BarcodeScannerController) {
    controller.dismiss(animated: true, completion: nil)
  }
}

Actions

When the code is captured BarcodeScannerController switches to the processing mode:

BarcodeScanner loading

While the user see a nice loading animation you can perform some background task, for example make a network request to fetch product info based on the code. When the task is done you have 3 options to proceed:

  1. Dismiss BarcodeScannerController and show your results.
func barcodeScanner(_ controller: BarcodeScannerController, didCaptureCode code: String, type: String) {
  // Code processing
  controller.dismiss(animated: true, completion: nil)
}
  1. Show an error message and switch back to the scanning mode (for example, when there is no product found with a given barcode in your database):
BarcodeScanner error

func barcodeScanner(_ controller: BarcodeScannerController, didCaptureCode code: String, type: String) {
  // Code processing
  controller.resetWithError(message: "Error message")
  // If message is not provided the default message from the config will be used instead.
}
  1. Reset the controller to the scanning mode (with or without animation):
func barcodeScanner(_ controller: BarcodeScannerController, didCaptureCode code: String, type: String) {
  // Code processing
  controller.reset(animated: true)
}

If you want to do continuous barcode scanning just set the isOneTimeSearch property on your BarcodeScannerController instance to false.

Customization

We styled BarcodeScanner to make it look nice, but feel free to customize its appearance by changing global configuration variables:

// Strings
BarcodeScanner.Title.text = NSLocalizedString("Scan barcode", comment: "")
BarcodeScanner.CloseButton.text = NSLocalizedString("Close", comment: "")
BarcodeScanner.SettingsButton.text = NSLocalizedString("Settings", comment: "")
BarcodeScanner.Info.text = NSLocalizedString(
  "Place the barcode within the window to scan. The search will start automatically.", comment: "")
BarcodeScanner.Info.loadingText = NSLocalizedString("Looking for your product...", comment: "")
BarcodeScanner.Info.notFoundText = NSLocalizedString("No product found.", comment: "")
BarcodeScanner.Info.settingsText = NSLocalizedString(
  "In order to scan barcodes you have to allow camera under your settings.", comment: "")

// Fonts
BarcodeScanner.Title.font = UIFont.boldSystemFont(ofSize: 17)
BarcodeScanner.CloseButton.font = UIFont.boldSystemFont(ofSize: 17)
BarcodeScanner.SettingsButton.font = UIFont.boldSystemFont(ofSize: 17)
BarcodeScanner.Info.font = UIFont.boldSystemFont(ofSize: 14)
BarcodeScanner.Info.loadingFont = UIFont.boldSystemFont(ofSize: 16)

// Colors
BarcodeScanner.Title.color = UIColor.black
BarcodeScanner.CloseButton.color = UIColor.black
BarcodeScanner.SettingsButton.color = UIColor.white
BarcodeScanner.Info.textColor = UIColor.black
BarcodeScanner.Info.tint = UIColor.black
BarcodeScanner.Info.loadingTint = UIColor.black
BarcodeScanner.Info.notFoundTint = UIColor.red

Installation

BarcodeScanner is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'BarcodeScanner'

In order to quickly try the demo project of a BarcodeScanner just run pod try BarcodeScanner in your terminal.

BarcodeScanner is also available through Carthage. To install just write into your Cartfile:

github "hyperoslo/BarcodeScanner"

To install BarcodeScanner manually just download and drop Sources and Images folders in your project.

Author

Hyper Interaktiv AS, [email protected]

Contributing

We would love you to contribute to BarcodeScanner, check the CONTRIBUTING file for more info.

License

BarcodeScanner is available under the MIT license. See the LICENSE file for more info.

barcodescanner-1's People

Contributors

algrid avatar carlbrusell avatar ecompton3 avatar frederic-adda avatar harmoniqpunk avatar ivanicspeter92 avatar jeryrazakarison avatar jkates1 avatar nashby avatar onmyway133 avatar ramongilabert avatar tapwork avatar vadymmarkov avatar zenangst avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.