How to generate Data Matrix in Swift?

880 views Asked by At

Is there any way to create data matrix natively in Swift, without using any 3rd party library? I'm searching in the whole internet and cannot find a way of generating data matrix. There are only instructions about generating barcodes:

class Barcode {

  class func fromString(string : String) -> UIImage? {

    let data = string.data(using: .ascii)
    let filter = CIFilter(name: "CICode128BarcodeGenerator")
    filter?.setValue(data, forKey: "inputMessage")

    return UIImage(ciImage: (filter?.outputImage)!)
  }

}

let img = Barcode.fromString("my_string_goes_here")

Is there any way to modify this code to generate data matrix instead of barcodes?

I've tried using also ZXingObjC library to generate data matrix, but I cannot scan the output image later. My code is:

func generateDataMatrix(from xml: String) {
    let writer = ZXMultiFormatWriter()
    let result: ZXBitMatrix? = try? writer.encode(xml, format: kBarcodeFormatDataMatrix, width: Int32(barcodeImageView.frame.width), height: Int32(barcodeImageView.frame.height))
    if result != nil {
        let image: CGImage = ZXImage(matrix: result).cgimage

        barcodeImageView.image = UIImage(cgImage: image)
    }
}

I really appreciate any your help! Thanks in advance!

0

There are 0 answers