2

Hello I followed the guide in the post here, and I just built my own function similar to the one in the guide. But my printer still failed to print the full image. It only print the first 24 line dots of the image. The printer model is RPP02N.

public void printImage(Bitmap bitmap) {
    System.out.println("Width: " + bitmap.getWidth());
    System.out.println("Height: " + bitmap.getHeight());

    List<Boolean> dots = new ArrayList<>();
    for (int i=0;i<bitmap.getHeight();i++) {
        for (int j =0; j < bitmap.getWidth(); j++) {
            int pixel = bitmap.getPixel(j, i);

            if (shouldBeBlack(pixel)) {
                dots.add(true);
            } else {
                dots.add(false);
            }
        }
    }

    resetPrinter();

    try {
        OutputStream outputStream = bluetoothConnectivity.getOutputStream();

        int offset = 0;
        byte[] finalFinalBytes = new byte[]{27, 51, 24};

        while (offset < bitmap.getHeight()) {
            byte[] finalBytes = new byte[]{27, 42, 33, (byte) (bitmap.getWidth()%256), (byte) (bitmap.getWidth()/256)};

            for (int kolom = 0; kolom < bitmap.getWidth(); kolom++) {
                byte[] temp = new byte[3];
                for (int bagian=0; bagian<3; bagian++) {
                    byte slice = 0;

                    for (int bit = 0; bit < 8; bit++) {
                        int baris = ((offset/8) + bagian) * 8 + bit;
                        int index = baris * bitmap.getWidth() + kolom;

                        boolean shouldBlack = false;
                        if (index < dots.size()) {
                            shouldBlack = dots.get(index);
                        }

                        slice = (byte) (slice | (shouldBlack ? 1 : 0) << (7-bit));
                    }
                    temp[bagian] = slice;
                }

                finalBytes = mergeArrayBytes(finalBytes, temp);
            }
            finalBytes = mergeArrayBytes(finalBytes, new byte[]{10});

            finalFinalBytes = mergeArrayBytes(finalFinalBytes, finalBytes);
            offset += 24;
        }

        finalFinalBytes = mergeArrayBytes(finalFinalBytes, new byte[]{27, 51, 30});
        outputStream.write(finalFinalBytes);

        resetPrinter();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

When I tried to print the bytes after each 24-line, it works fine on console and doesn't throw any exception. What could have gone wrong?

The logo I want to print: The logo I want to print

Charlie
  • 148
  • 1
  • 16
  • I would start with sending data divided in lines. So send it before the offset += 24; The reason is that even it looks like you send the stream, BT printer may discard data if data is exceeding internal printer limit. – vookimedlo Sep 30 '18 at 21:25
  • @vookimedlo so it means that if the BT printer receives too much data, it will not work as expected? like it stopped printing? – Charlie Oct 01 '18 at 15:43
  • It might be, I have already seen something similar when sending data to the old Canon Pixma. – vookimedlo Oct 01 '18 at 15:51

1 Answers1

0

Try to add line feed after image

outputStream.write(0x0a)

Plugie
  • 1,289
  • 17
  • 25