Barcode Generation from Within iOS App

Barcode Generation from within iOS App

The only free library to do this is Cocoa-Touch-Barcodes, which is a fork of cocoabarcodes. If you are considering commercial libraries, there is one called iPhone Barcode Generator.

update Check this objective-c port of ZXing: https://github.com/TheLevelUp/ZXingObjC

Barcode generator code or SDK for ipad

This library worked for me...

https://github.com/netshade/Cocoa-Touch-Barcodes

How to generate a barcode image in ios

I guess this is the interface of the class you're using: OBLinear.h

If that's so, it looks like you can generate a UIImage just by doing this:

UIImage *image;
[plLinear drawWithImage:&image];

Then you can do whatever you want with image. For example, you can convert it to a PNG using UIImagePNGRepresentation and upload the PNG data to your server, or save it to a file.

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

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.

Barcode scanner doesn't work with codes generated by the app itself

On your code, you generate a code-128 bar code, so you need a scanner capable of reading code-128 too.

You need to add code128 on the list of object type you want to read :

output.metadataObjectTypes = [.qr, .ean8, .ean13, .code128]


Related Topics



Leave a reply



Submit