To run the example project, clone the repo, and run pod install from the Example directory first.
- Barcode Scanner
- Barcode Scanner View
- Barcode Generator
- iOS 13.0+
- Xcode 11.0+
TCBBarcode is available through CocoaPods. To install TCBBarcode, simply add the following line to your Podfile:
pod'TCBBarcode'letgenerator=TCBBarcodeGenerator(transform:CGAffineTransform(scaleX:10, y:10))
// .QRCode(level: .levelH)
iflet codeObject = generator.generateCode(forType:.QRCode(), source: inputeTxtFld.text!.cleanString){
// apply tint | change barcode color
codeObject.applyTint(color:.green)
// Blends an image | gradient to the code mask
codeObject.applyBlend(withImage: image)
// Blends an image | gradient to the code mask -- set fill mode
codeObject.applyBlend(withImage: image, fillMode:.aspectFill)
// Apply logo at the center with 0.18% scale
// Only tested on QR type codes
codeObject.applyLogo(withImage: image)
imageView.image = codeObject.code
}varpreviewView:UIView!varscanner:TCBBarcodeScanner!func setupScanner(frame:CGRect){
scanner =TCBBarcodeScanner(supportedTypes:TCBBarcodeScanner.availableTypes, playSoundOnSuccess:true, delegate:self)letpreviewFrame=CGRect(x:0, y:0, width: frame.width, height: frame.height - TCBBarcodeScannerView.codeLblHeight)iflet previewLayer = scanner.previewLayer(withFrame: previewFrame){
previewView.layer.addSublayer(previewLayer)}else{leterror=TCBBarcodeError.createCustomError(errorMessage:"Scanner preview configuration failed")print(error)}}func scan(){
scanner.start()}func stop(){
scanner.stop()}
// MARK: - TCBBarcodeScannerDelegate
publicfunc scanner(scanner:TCBBarcodeScanner, setupDidFail error:Error){print(error)}publicfunc scanner(scanner:TCBBarcodeScanner, didOutputCodeObject codeObject:TCBBarcodeScanner.CodeObject){letcodeStr="\(codeObject.type): \(codeObject.code)"print(codeStr)switch detectType {case.box:showBox(frame: codeObject.bounds)case.line:showLine(corners: codeObject.corners)default:resetDetectView()}}
// MARK: - Controls
func resetDetectView(){
detectView.isHidden =trueforviewin detectView.subviews {
view.removeFromSuperview()}iflet sublayers = detectView.layer.sublayers {forlayerin sublayers {
layer.removeFromSuperlayer()}}}func showBox(frame:CGRect){resetDetectView()letcodeBox=UIView(frame: frame)
codeBox.backgroundColor =UIColor.lightGray.withAlphaComponent(0.5)
detectView.addSubview(codeBox)
detectView.isHidden =false}func showLine(corners:[CGPoint]){resetDetectView()letbezierPath=UIBezierPath(rect: previewView.frame)for(idx, point)in corners.enumerated(){if idx >0{
bezierPath.addLine(to: point)}else{
bezierPath.move(to: point)}}
bezierPath.close()letshapeLayer=CAShapeLayer()
shapeLayer.path = bezierPath.cgPath
shapeLayer.strokeColor =UIColor.orange.cgColor
shapeLayer.lineWidth =1
shapeLayer.fillRule =.evenOdd
shapeLayer.fillColor =UIColor.clear.cgColor
shapeLayer.borderColor =UIColor.clear.cgColor
shapeLayer.borderWidth =0
detectView.layer.addSublayer(shapeLayer)
detectView.isHidden =false}varscannerView:TCBBarcodeScannerView!func prepareScannerView(){letscreenFrame=UIScreen.main.bounds
letpreviewSize=CGSize(width: screenFrame.width -40, height:300)letscannerSize=TCBBarcodeScannerView.scannerFrameSizeFor(previewFrameSize: previewSize)letscannerFrame=CGRect(x:20, y:80, width: scannerSize.width, height: scannerSize.height)
scannerView =TCBBarcodeScannerView.instance(frame: scannerFrame, delegate:self)
view.addSubview(scannerView)}func changeDetectType(){
scannerView.detectType =.line
}func startScan(){
scannerView.scan()}
// MARK: - TCBBarcodeScannerViewDelegate
func scannerView(scannerView:TCBBarcodeScannerView, setupDidFail error:Error){}func scannerView(scannerView:TCBBarcodeScannerView, didOutputCode code:String, codeType type:String){print("Readable Type: \(type) -- Code: \(code)")}nferocious76, nferocious76@gmail.com
TCBBarcode is available under the MIT license. See the LICENSE file for more info.