您好,我遵循了here中的指南,并且我刚刚构建了自己的功能,类似于指南中的功能。但是我的打印机仍然无法打印完整图像。它仅打印图像的前24行点。打印机型号为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();
}
}
当我尝试在每24行之后打印字节时,它在控制台上可以正常工作,并且不会引发任何异常。可能出了什么问题?