Align bitmap image to center


I have the following code to generate a bitmap on a ticket for a thermal printer:

String ticketPreSelectedImage = SharedPref.getInstance(this).getTicketPreSelectedImagePath();

if (ticketPreSelectedImage != null) {

    Bitmap image = BitmapFactory.decodeFile(ticketPreSelectedImage);

    ticketBuilder.addCommand(new byte[]{27, 97, 1, 0}); // Esta linea es la que creo que centra la imagen

    ticketBuilder.addImage(image, image.getWidth() * 1, image.getHeight() * 1);

This code what does is align me the image in the ticket to the left and I do not know very well how to center it so that it looks good,


asked by J. Burrueco 30.11.2018 в 09:20

1 answer


The line that marks what you do is send a code ESC / POS . Specifically, it can be translated to the following:

ESC a 1 0

Reviewing the list of ESC / POS commands , we find the following for ESC a :




ESC a n


In Standard mode, align all the data in one line to the selected layout, using n as follows:

  n          Justificado
0, 48 Justificado a la izquierda
1, 49 Centrado
2, 50 Justificado a la derecha

Taking that into account, your code is sending a 0 after 1 which is what you want (centered). So you should have the following in your code:

ticketBuilder.addCommand(new byte[]{27, 97, 1}); //27=ESC 97=a 1=Centrado

P.D. I do not know what library you are using to send the commands, and you may need to send a 0 to mark the end of the command, but I add my answer because I think the information about ESC / P and the command you are using may be useful to you

answered by 30.11.2018 / 10:20