使用bbpos打印机打印图像

时间:2017-12-04 12:30:16

标签: android service printing bluetooth

我想在android中使用bbpos打印机打印收据。我能够正确打印文本但不能打印图像。

我已经拍摄了一个可绘制的图像并转换为字节数组,但是它打印了一些随机字符但不是图像我认为我需要再次将其转换为位图而不是将其发送到打印机。

这是我的代码

public class PrintingService {

private static byte[] INIT = {0x1B, 0x40};
private static byte[] POWER_ON = {0x1B, 0x3D, 0x01};
private static byte[] NEW_LINE = {0x0A};
private static byte[] ALIGN_LEFT = {0x1B, 0x61, 0x00};
private static byte[] ALIGN_CENTER = {0x1B, 0x61, 0x01};
private static byte[] EMPHASIZE_ON = {0x1B, 0x45, 0x01};
private static byte[] EMPHASIZE_OFF = {0x1B, 0x45, 0x00};
private static byte[] FONT_5X12 = {0x1B, 0x4D, 0x01};
private static byte[] FONT_8X12 = {0x1B, 0x4D, 0x02};
private static byte[] FONT_10X18 = {0x1B, 0x4D, 0x03};
private static byte[] FONT_SIZE_0 = {0x1D, 0x21, 0x00};
private static byte[] FONT_SIZE_1 = {0x1D, 0x21, 0x11};
private static byte[] CHAR_SPACING_0 = {0x1B, 0x20, 0x00};
private ArrayList<VehicleInfo> mVehileInfo;
private double mTotal;
private Drawable mDrawable;

public PrintingService(Drawable drawable, ArrayList<VehicleInfo> vehicleInfo, double total)
{
    mVehileInfo = vehicleInfo;
    mTotal = total;
    mDrawable = drawable;
}

public byte[] getData() throws IOException
{
    ByteArrayOutputStream baos = null;
    byte[] dataToPrint = null;
    baos = new ByteArrayOutputStream();
    writeHeader(baos);

    Bitmap bitmap = ((BitmapDrawable)mDrawable).getBitmap();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    dataToPrint = baos.toByteArray();




    return dataToPrint;
}

private void writeHeader(ByteArrayOutputStream baos) throws IOException {

    baos.write(INIT);
    baos.write(POWER_ON);
    baos.write(NEW_LINE);
    baos.write(ALIGN_CENTER);
    baos.write(FONT_SIZE_1);
    baos.write(EMPHASIZE_ON);
    baos.write(FONT_5X12);
    baos.write(CHAR_SPACING_0);
    baos.write("Print Demo".getBytes());
    baos.write(NEW_LINE);
    baos.write(NEW_LINE);
    baos.write("Sparken IT Solutions".getBytes());
    baos.write(NEW_LINE);
    baos.write("Surya Plaza".getBytes());
    baos.write(NEW_LINE);
    baos.write("Pune-411030".getBytes());
    baos.write(EMPHASIZE_OFF);
    baos.write(NEW_LINE);

}

private void writeRow(ByteArrayOutputStream baos, String tag, String value) throws IOException {
    if (!ValidationUtils.isStringNull(value)) {

        baos.write(EMPHASIZE_ON);
        baos.write(FONT_10X18);
        baos.write(tag.getBytes());
        baos.write(value.getBytes());
        baos.write(NEW_LINE);
        baos.write(EMPHASIZE_OFF);
    }

   }

 }

1 个答案:

答案 0 :(得分:0)

不确定您使用的是什么版本的bbpos,但我相信从版本3.4+开始,有一个BBDeviceController.getImageCommand(Bitmap)方法。

不确定这是否是您要找的。