4 Replies Latest reply on Apr 25, 2018 12:12 AM by Simon Hopkin

    iOS Barcode Printing - black rectangle

    Simon Hopkin

      Hi,

       

      I'm trying to print a barcode onto a label but it just prints a black rectangle in the shape of the barcode image.

       

      To generate the barcode I'm using the following:

       

                  let data = "Hello".data(using: .ascii)

                  let filter = CIFilter(name: "CICode128BarcodeGenerator")

                  filter?.setValue(data, forKey: "inputMessage")

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

       

      When I send this to the printer using ZebraPrinter.getGraphicsUtil().printImage(...) it just prints out a black rectangle as mentioned.

       

      I imagine it is some problem with the format of the generated barcode image as it prints out images taken from the iPads camera ok.

       

      Any help on this would be much appreciated.

       

      Kind Regards

        • Re: iOS Barcode Printing - black rectangle
          Steven Si

          If we use the following API from Link-OS SDK to print an image from iOS device, the accepted image type is CGImageRef.

          - printImage:atX:atY:withWidth:withHeight:andIsInsideFormat:error:
          

          Refer to the API documentation on <GraphicsUtil>

            • Re: iOS Barcode Printing - black rectangle
              Simon Hopkin

              Hi Steven,

               

              Thanks for your reply and yes I'm aware of that, I'm calling .cgImage on the UImage before passing to the print method.  Also tried .cgImage on the CIImage created by the filter but get the same black result.

               

              Thanks

                • Re: iOS Barcode Printing - black rectangle
                  Steven Si

                  Let's convert the barcode image from ciImage to cgImage with below code, before sending to the printer. Hope this helps.

                      func getBarcode() -> UIImage {
                          let data = "Hello".data(using: .ascii)
                          let filter = CIFilter(name: "CICode128BarcodeGenerator")
                          filter?.setValue(data, forKey: "inputMessage")
                  //        return UIImage(ciImage: (filter?.outputImage)!)
                          let output = filter?.outputImage
                          return UIImage(cgImage: convertCIImageToCGImage(ciImage:output!))
                      }
                  
                      func convertCIImageToCGImage(ciImage:CIImage) -> CGImage{
                          let ciContext = CIContext.init()
                          let cgImage:CGImage = ciContext.createCGImage(ciImage, from: ciImage.extent)!
                          return cgImage
                      }
                  
                      // Print barcode
                      let barcodeImage = getBarcode()
                      if barcodeImage != nil && barcodeImage!.cgImage != nil {
                          try printer?.getGraphicsUtil().print(barcodeImage!.cgImage!, atX: 0, atY: 0, withWidth: -1, withHeight: -1, andIsInsideFormat: false)
                      }