【问题标题】:Print to printer via Bluetooth(non Airprint) in iOS?在 iOS 中通过蓝牙(非 Airprint)打印到打印机?
【发布时间】:2021-09-27 16:38:39
【问题描述】:

我需要将图像从我的 iOS 应用程序打印到未启用 AirPrint 的蓝牙热敏打印机。由于非 AirPrint 打印机无法使用 UIKit 打印,所以我选择使用 3rd 方 SDK。

使用this SDK 并尝试打印可以打印小尺寸图像的图像,但是当涉及到大图像时,它在附加字节时崩溃,说索引超出范围,这就是功能

`private func eachLinePixToCmd(src: [UInt8], nWidth: Int, nHeight: Int, nMode: Int) -> [UInt8] { var 数据 = [UInt8]

    let p0 = [0, 0x80]
    let p1 = [0, 0x40]
    let p2 = [0, 0x20]
    let p3 = [0, 0x10]
    let p4 = [0, 0x08]
    let p5 = [0, 0x04]
    let p6 = [0, 0x02]
    
    let nBytesPerLine: Int = (nWidth + 7) / 8
    var k: Int = 0
    
    for _ in 0..<nHeight {
        data.append(ESC_POSCommand.beginPrintImage(xl: UInt8(nBytesPerLine % 0xff), xH: UInt8(nBytesPerLine / 0xff), yl: UInt8(1), yH: UInt8(0)).rawValue)
        var bytes = [UInt8]()
        for _ in 0..<nBytesPerLine {
            bytes.append(UInt8(p0[Int(src[k])] + p1[Int(src[k + 1])] + p2[Int(src[k + 2])] + p3[Int(src[k + 3])] + p4[Int(src[k + 4])] + p5[Int(src[k + 5])] + p6[Int(src[k + 6])] + Int(src[k + 7])))
            k = k + 8
        }
        data.append(bytes)
    }
    let rdata: [UInt8] = data.flatMap { $0 }
    return rdata
}

}`

如果有其他可用的 SDK 或在哪里对附加字节进行更改,请告诉我?

希望得到答复!

【问题讨论】:

  • 创建了一个宽度 % 8 == 0 的图像,现在没有发生崩溃,但是我的图像在中间停止打印,它没有打印完整的图像

标签: ios swift printers thermal-printer ios-bluetooth


【解决方案1】:

我在打印图像时遇到了同样的问题,这是由于热敏打印机的限制。这里我已经压缩了图像,然后将其发送到打印图像。

func resizeWithWidth(width: CGFloat) - > UIImage ? {
  let imageView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: width, height: CGFloat(ceil(width / size.width * size.height)))))
  imageView.contentMode = .scaleAspectFit
  imageView.image = self
  UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, scale)
  guard
  let context = UIGraphicsGetCurrentContext()
  else {
    return nil
  }
  imageView.layer.render( in: context)
  guard
  let result = UIGraphicsGetImageFromCurrentImageContext()
  else {
    return nil
  }
  UIGraphicsEndImageContext()
  return result
}

//Set the width to 256 for example

let myImage = image.resizeWithWidth(width: 256) !
  let compressData = myImage.jpegData(compressionQuality: 0.6)
//max value is 1.0 and minimum is 0.0
let compressedImage = UIImage(data: compressData!)

var ticImage = Ticket(.image(image, attributes: .alignment(.center)))

if bluetoothPrinterManager.canPrint {
  bluetoothPrinterManager.print(ticImage)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 2016-06-23
    相关资源
    最近更新 更多