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?