我正在 Android Studio 中使用 Java 开发一个 Android 应用程序,旨在使用 TSPL 命令在 TSC DA220 打印机上打印标签。我的目标是在标签上包含公司徽标,该徽标以 .png 文件形式存储在
res/drawables
目录中。
我首先尝试使用
BITMAP X, Y, width, height, mode, bitmap data
命令打印图像,方法是将徽标转换为表示字节数组的字符串。这是我用于此方法的代码片段:
public class ChosenAndCookedSticker implements PrintableModel, TsplLanguage.Support {
...
@Override
public List<Command> getTsplCommandList() {
List<Command> commandList = new ArrayList<>();
commandList.add(new DirectionCommandTspl(DirectionCommandTspl.REVERSE));
commandList.add(new LabelSizeCommandTspl(new DimensionTspl(58, Dimension.MM), new DimensionTspl(58, Dimension.MM)));
commandList.add(new PrintSpeedCommandTspl(PrintSpeedCommandTspl.SPEED_4_INCH_PER_SECOND));
commandList.add(new DensityCommandTspl(15));
commandList.add(new FeedCommandTspl(FeedCommandTspl.GAP_FEED, new DimensionTspl(3, Dimension.MM), new DimensionTspl(0, Dimension.MM)));
commandList.add(new ImageBufferClearCommandTspl());
commandList.add(new PrintQualityCommandTspl(PrintQualityCommandTspl.OPTIMUM));
commandList.add(new CodepageTspl(CodepageTspl.UTF8));
String hexImage = convertImageToHex(R.drawable.logo);
commandList.add(new BitmapCommandTspl(
new DimensionTspl(25, Dimension.MM),
new DimensionTspl(1.2, Dimension.MM),
new DimensionTspl(10, Dimension.MM),
new DimensionTspl(5, Dimension.MM),
0,
hexImage));
return commandList;
}
private String convertImageToHex(int drawableId) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), drawableId);
byte[] bytes = convertBitmapToByteArray(bitmap);
return convertByteArrayToHex(bytes);
}
private String convertByteArrayToHex(byte[] bytes) {
StringBuilder hexString = new StringBuilder();
for (byte b : bytes) {
String hex = Integer.toHexString(0xFF & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
private byte[] convertBitmapToByteArray(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
return stream.toByteArray();
}
...
}
public class BitmapCommandTspl extends Command {
private final DimensionTspl x;
private final DimensionTspl y;
private final DimensionTspl width;
private final DimensionTspl height;
private final int mode;
private final String bitmapData;
public BitmapCommandTspl(DimensionTspl x, DimensionTspl y, DimensionTspl width, DimensionTspl height, int mode, String bitmapData) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.mode = mode;
this.bitmapData = bitmapData;
}
@Override
public String getCommand() {
return String.format(
"BITMAP %d,%d,%d,%d,%d,\"%s\"",
x, y, width, height, mode, bitmapData
);
}
}
我期望打印机输出徽标,但是,打印机生成的不是预期的图像,而是尺寸正确的黑色条带,但没有任何实际图像内容。
考虑采用不同的方法,然后我研究了 PUTPNG 命令,该命令是为处理 PNG 文件而设计的。我在文档中找到的命令格式是
PUTPNG x, y, "filename"
。我这里的挑战是弄清楚如何在这个 TSPL 命令中正确引用可绘制的 PNG 文件,因为简单地指定在 Android 中工作的文件路径似乎并不能直接转换为打印机可读的命令。
我的做法如下。 使用 TSC 控制台将 BMP 格式的图像发送到打印机。 然后只需使用命令:
PUTBMP 89,10,"YourImageName.BMP",8,100
我将该命令用作其他命令中的参数,
尊重对打印机的命令,
在所有先前的处理之后,必须以在其视图中格式化的方式进行传递。
如果你想在某些参数中传递变量,
您需要关闭、连接变量并重新打开字符串
与 TSPL 命令中的其余参数一起使用,直到结束。
至少当我使用 TSCLIB.DLL 中的“sendcommand(command)”时,它是这样工作的。
From TSC.sendcommand("PUTBMP "_X_","_Y_",""IMAGE.BMP"",8,100")
在上面的示例中,请注意我的变量 X 和 Y 在字符串之外。
它们与通过 _(下划线)传递命令的字符串连接起来。
文件名仍然有一个特殊性,因为它需要是一个字符串,
在 PUTBMP 命令的第三个参数中。
所以这个参数不需要关闭字符串,
但它需要接收一个信号,在您将从内存中访问的文件名的开头
和结尾处生成双引号。
就我而言,我在开头和结尾提供两个双引号“”,
要在 PUTBMP 命令的第三个参数中作为字符串传递。