ESC TSPL指令
tspl是一套通用的标签打印指令,许多主流标签打印机都支持此指令。但是网上资料较少,这里主要分享一下android和ios的打印基本算法。
指令文档
指令文档网上较少,这里引用一个链接吧 TSC条码打印机指令TSPL手册,各个厂商可能基于此指令有少量diy,具体可以查看自己打印机对应厂商的文档。
打印
请详细阅读指令手册,具体打印步骤,以打印图片为例:
- SIZE 设置标签大小
- GAP 设置标签缝大小
- BITMAP 绘制图像
- PRINT 打印
如果需要反向打印,需要在PRINT前调用DIRCTION。
注意:每条指令后需要添加 "\r\n"
android示例代码
///写入指令
public boolean tspl_writeCmd(String cmd, boolean end) {
if (TextUtils.isEmpty(cmd)) {
return false;
}
cmd = end ? (cmd + "\r\n") : cmd;
JXLog.d(TAG, "tspl_writeCmd: " + cmd);
byte[] data;
try {
data = cmd.getBytes();
} catch (Exception var4) {
JXLog.e(TAG, "String getBytes('GBK') failed");
return false;
}
return mAPI.isConnected() && mAPI.flushReadBuffer() && mAPI.writeBuffer(data, 0, data.length);
}
///设置纸张和gap
public boolean tspl_pageSetup(int widthInMM, int heightInMM, int gapInMM) {
if (widthInMM <= 0 || heightInMM <= 0) {
return false;
}
if (widthInMM > maxWidthInMM) widthInMM = maxWidthInMM;
if (heightInMM > maxHeightInMM) heightInMM = maxHeightInMM;
String cmd = String.format("SIZE %d mm, %d mm", widthInMM, heightInMM);
tspl_writeCmd(cmd, true);
tspl_writeCmd(String.format("GAP %d mm,0", gapInMM), true);
return true;
}
///绘制图片
///1. 算法中对图片以2kb大小进行了切割,防止打印机内存不足,可以自行修改此值
///2. 算法中用经验公式对图片进行了二值化处理,每8个点为1mm
public boolean tspl_drawGraphic(int start_x, int start_y, Bitmap bmp) {
int bmp_size_x = bmp.getWidth();
int bmp_size_y = bmp.getHeight();
int byteWidth = (bmp_size_x - 1) / 8 + 1; //8个点一个byte,所以要先计算byteWidth
int byteHeight = bmp_size_y;
if (byteWidth <= 0) return false;
if (byteHeight <= 0) return false;
int unitHeight = 2048 / byteWidth;//分包规则,算法可无需过多理解,一个包就unitHeight*byteWidth个点
int unitCount = (byteHeight - 1) / unitHeight + 1;
int startY, endY;//每个包y方向起始的位置
byte[] dataByte;
int color, A, R, G, B;
for (int n = 0; n < unitCount; n++) {
JXLog.d(TAG, "drawGraphic unit=" + (n + 1) + "/" + unitCount);
startY = n * unitHeight;
if ((startY + unitHeight) > bmp_size_y) {
endY = bmp_size_y;
} else {
endY = startY + unitHeight;
}
int byteLen = (endY - startY) * byteWidth;//每个包数据的字节个数
dataByte = new byte[byteLen];
for (int y = startY; y < endY; y++) {
for (int x = 0; x < bmp_size_x; x++) {
color = bmp.getPixel(x, y);
A = color >> 24 & 0xFF;
R = color >> 16 & 0xFF;
G = color >> 8 & 0xFF;
B = color & 0xFF;
//经验公式二值化,透明视为白色
if (A == 0 || R * 0.3 + G * 0.59 + B * 0.11 > 127) {
dataByte[(y - startY) * byteWidth + x / 8] |= (0x80 >> (x % 8));
}
}
}
int curUnitHeight = n == unitCount - 1 ? (byteHeight - (n * unitHeight)) : unitHeight;
String cmdHeader = "BITMAP " + start_x + "," + (start_y + startY) + "," + byteWidth + "," + curUnitHeight + "," + 0 + ",";
boolean success = tspl_writeCmd(cmdHeader, false) && mAPI.writeBuffer(dataByte, 0, dataByte.length);
if (success) {
mAPI.writeBuffer(new byte[]{0x0D, 0x0A}, 0, 2);
} else {
return false;
}
}
return true;
}
/// 打印
public boolean tspl_print(int count, boolean isReverse) {
if (count < 0) count = 1;
if (count > 65535) count = 65535;
String cmd = String.format("PRINT %d,1", count);
return tspl_direction(isReverse) && tspl_writeCmd(cmd, true);
}
ios代码就不贴了,算法基本一致。如果恰好需要,希望对你有帮助!