I'm trying to make my Android JAVA app print using a GC420t zebra printer, everything related to the USB connection works just fine, but when it comes to printing i'm facing a problem when converting my image to a byte array to send it to the printer with the GW command, when printing using all the other commands it works fine, but when adding the image or it doesn't print anything where the image should be, or it print only random lines.
I think there's a problem with how i'm encoding it, i can see the image just fine inside the bitmap, but i have no idea of what to do to fix the result of the conversion, have anybody faced a similar problem?
This is the code i'm using:
private String processEPLCommands(String[] eplCommands) {
StringBuilder processedCommands = new StringBuilder();
for (String command : eplCommands) {
if (command.startsWith("[IMG]")) {
String[] parts = command.split(";");
if (parts.length == 5) {
x = Integer.valueOf(parts[1]);
y = Integer.valueOf(parts[2]);
String imageUrl = parts[4];
int rotation = Integer.parseInt(parts[3]);
String cmdImg = sendImageToPrinter(x,y,downloadAndRotateImage(imageUrl, rotation));
if(cmdImg != null) {
processedCommands.append(cmdImg);
}
}
} else {
processedCommands.append(command);
processedCommands.append("\n");
}
}
return processedCommands.toString();
}
private Bitmap downloadAndRotateImage(String imageUrl, int rotation) {
AtomicBoolean isRunning = new AtomicBoolean(true);
AtomicReference<Bitmap> bitmap = new AtomicReference<Bitmap>();
new Thread(() -> {
try {
bitmap.set(BitmapFactory.decodeStream(new URL(imageUrl).openStream()));
isRunning.set(false);
} catch (IOException e) {
Log.e("printer", "Failed to download or process image", e);
isRunning.set(false);
}
}).start();
while(isRunning.get()){
}
return bitmap.get() == null?null:rotateBitmap(bitmap.get(), rotation);
}
private Bitmap rotateBitmap(Bitmap bitmap, int rotation) {
Matrix matrix = new Matrix();
matrix.postRotate(rotation);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
public static String sendImageToPrinter(int top, int left, Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
int p3 = (int) Math.ceil((double) bitmap.getWidth() / 8);
String header = String.format("GW%d,%d,%d,%d,", top, left, p3, bitmap.getHeight());
baos.write(header.getBytes("US-ASCII"));
int canvasWidth = p3 * 8;
for (int y = 0; y < bitmap.getHeight(); ++y) {
for (int x = 0; x < canvasWidth;) {
byte abyte = 0;
for (int b = 0; b < 8; ++b, ++x) {
int dot = 1;
if (x < bitmap.getWidth()) {
int color = bitmap.getPixel(x, y);
int luminance = (int) ((Color.red(color) * 0.3) + (Color.green(color) * 0.59) + (Color.blue(color) * 0.11));
dot = luminance > 127 ? 1 : 0;
}
abyte |= (byte) (dot << (7 - b));
}
baos.write(abyte);
}
}
return new String(baos.toByteArray(), "windows-1252");
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Example of what i'm receiving from the conversion: GW512,590,16,125,���������������������������������
I've tried changing the encoding "windows-1252", and tried using a base64 to convert the bitmap to a byte array, but it didn't work.
�(character encoding problem) present as images should be represented as hex codes. Try sending this string insteadString printMe = "^XA^FO0,0^GFA,120,120,4,,::::::::FCFC7BFC2080806,2081806,:2080C06,20F8706,2080186,2080086,20800C6,2080086,20F9F86,,:::::::::^FS^XZ";