I'm using to print image on thermal printer this code "GS V 0" but is obsolete. How can I convert this function to print image with "ESC *" command? I'm using the code at following links but it uses "GS V 0". ESC POS SWIFT CODE
Asked
Active
Viewed 557 times
1 Answers
0
The idea is to divide your image into blocks of up to 256 (max width) x 8 (height) pixels and then generate ESC *
and ESC J
commands for each block.
You have 2D array of dots to print as input. You can generate it from the image the way you like.
The code below works with Epson U220
let dots: [[Int]] = [...]
let width = dots[0].count
let height = dots.count
// Calculating how many blocks are there
let horizontalBlocks = width / 256 + (width % 256 > 0 ? 1 : 0)
let verticalBlocks = height / 8 + (height % 8 > 0 ? 1 : 0)
var data = Data()
for verticalBlock in 0 ..< verticalBlocks {
for horizontalBlock in 0 ..< horizontalBlocks {
let currentBlockWidth = min(width - 255 * horizontalBlock, 255)
let escStar: [UInt8] = [0x1B, 0x2A, 0x00, UInt8(currentBlockWidth), 0x00]
data.append(contentsOf: escStar)
let xOffset = horizontalBlock * 256
let yOffset = verticalBlock * 8
for x in 0 ..< currentBlockWidth {
var column: UInt8 = 0
for y in 0 ..< 8 {
let dot = dots[yOffset + y][xOffset + x]
column |= dot << (7 - y)
}
data.append(column)
}
}
// Command to print the buffer
let escJ: [UInt8] = [0x1B, 0x4A, 0x10]
data.append(contentsOf: escJ)
}

Alex Barinov
- 89
- 1
- 6
-
1How can I generate “dots”? – TheCesco1988 Sep 17 '22 at 12:48
-
Basically you need to transform your image into black-white image. There are different algorithms for that. Like https://stackoverflow.com/questions/22422480/apply-black-and-white-filter-to-uiimage for example. – Alex Barinov Sep 18 '22 at 00:40