Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate a barcode from a string in Swift?

I am a new iOS developer. I was wondering how can I generate a barcode in Swift.

I have the code already, there are multiple resources from where to learn how to read a barcode, but I didn't find any that talks about generating one from a string.

Thanks a lot!

P.S. I know there is a similar question about this, but it's for Objective-C. I don't know Obj-C and I find it difficult coming from .NET.

like image 604
Andrei Dobrin Avatar asked Feb 16 '15 13:02

Andrei Dobrin


People also ask

How do I generate a barcode with details?

To create a barcodeSelect the barcode type: EAN-13, UPC-A, Code 39, or ITF. Fill in the product category information in the barcode data box. Click on the barcode title box and barcode note if you want to add them in the barcode. Add a name for the barcode in the title box and more details in the note box.

Can I generate my own barcode?

You can get barcode scanners on Amazon or another specialized online store for under $50. Most barcode generators are free. In fact, you can use the Barcode Font in Microsoft Word to generate your own barcodes. Therefore, with less than $100, you can create a system set to make your own barcodes.

What is a barcode generator?

The barcode generator allows you to create a barcode graphic by providing barcode symbology and data. Click on the Generate Barcode to create a graphic containing your barcode. Right click to copy or save the barcode, then paste or insert the barcode into your document.


1 Answers

You could use a CoreImage (import CoreImage) filter to do that!

    class Barcode {         class func fromString(string : String) -> UIImage? {              let data = string.data(using: .ascii)              if let filter = CIFilter(name: "CICode128BarcodeGenerator") {                   filter.setValue(data, forKey: "inputMessage")                   if let outputCIImage = filter.outputImage {                        return UIImage(ciImage: outputCIImage)                   }              }              return nil         }     }      let img = Barcode.fromString("whateva") 

A newer version, with guard and failable initialiser:

extension UIImage {      convenience init?(barcode: String) {         let data = barcode.data(using: .ascii)         guard let filter = CIFilter(name: "CICode128BarcodeGenerator") else {             return nil         }         filter.setValue(data, forKey: "inputMessage")         guard let ciImage = filter.outputImage else {             return nil         }         self.init(ciImage: ciImage)     }  } 

Usage:

let barcode = UIImage(barcode: "some text") // yields UIImage? 

According to the docs :

Generates an output image representing the input data according to the ISO/IEC 15417:2007 standard. The width of each module (vertical line) of the barcode in the output image is one pixel. The height of the barcode is 32 pixels. To create a barcode from a string or URL, convert it to an NSData object using the NSASCIIStringEncoding string encoding.

like image 143
Matteo Pacini Avatar answered Oct 01 '22 18:10

Matteo Pacini