以下是代码示例:
private void printLogo() {
// 确定打印机支持Logo打印
byte[] cmd = new byte[]{0x1D, 0x28, 0x6B, 0x03, 0x00, 0x01, 0x02};
mPrinter.write(cmd);
// 将Logo转换为位图格式
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
byte[] data = PrintUtils.bitmap2Bytes(bmp);
// 将位图数据发送给打印机
byte[] printImageData = new byte[]{0x1D, 0x2A, 0x57, (byte) (bmp.getWidth() / 8 % 256), (byte) (bmp.getWidth() / 8 / 256), (byte) (bmp.getHeight() % 256), (byte) (bmp.getHeight() / 256)};
mPrinter.write(printImageData);
mPrinter.write(data);
}
// 将Bitmap转换为字节数组 public static byte[] bitmap2Bytes(Bitmap bitmap) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); return stream.toByteArray(); }