How to convert image to byte array for mobile printer | esc/pos (Bluethooth)

3,476 views
Skip to first unread message

Marcel Klein

unread,
Jan 14, 2018, 5:20:01 PM1/14/18
to MIT App Inventor Forum
Hello Guys!

Is there a way to convert images to byte code for an Pos Printer?
I have to print QR Codes on a Bluethoothe mobile Printer.

I already searched the forum but found nothing, only this thread:
https://groups.google.com/forum/#!searchin/mitappinventortest/image$20byte%7Csort:date/mitappinventortest/6E64ljRlXZc/r7mczQfxAQAJ

But theres no solution posted or an example.

Chris Ward

unread,
Jan 15, 2018, 4:39:45 AM1/15/18
to MIT App Inventor Forum
Hello Marcel

Start at the beginning - is the POS Printer actually on your desktop so you can experiment? Do you have the manufacturer's technical manual about print files? Bear in mind that some have their own drivers/apps such that you would not need to create a print file but instead a media file that you would either pass through the printer driver and send the result via bluetooth or 'simply' send to the printer app.

When defining the code, be sure to identify device specific blocks with an ID for the device, as you may well need to add a different manufacturer's device, requiring different code, in the future.

Marcel Klein

unread,
Jan 15, 2018, 6:54:49 AM1/15/18
to mitappinv...@googlegroups.com
Hi Chris!

Thanks for your reply!

Yes i have the POS Printer on my desktop, i can test it.

If i send Bytes with the AppInventor Bluethooth client i can print text and line feed ect.
Also the NV Logo that is saved on the printer is no problem.

The only problem is the QR Code, i store them on my server and generate them with php script.
I can load them into the canvas with the URL of the code.

Now i dont know how to send it to the printer, i think the printer can create qr codes by itself, but not with such great value.
So i think i have to send it as picture in byte array.

The code to send is like this:

_R1-AT1_KassenID_1_2017-11-16T14:23:38_0,00_0,00_0,00_0,00_0,00_Nj2TEAdWLnH8A4xmigEg9g==_67647ea1_pOdv/BqgpkM=_XkwGWdHdYpuKJdgC+nPepC94W88FAhbaQsmtfTvGuLBy9FyDv5bUGPFLreiU/uGiOEJIKAJF4dHTB0wU+8vkJg==

In QR it looks like this:

58MMPrinter Programmer Manual-20150312.pdf

gerrikoio

unread,
Jan 15, 2018, 7:31:51 AM1/15/18
to MIT App Inventor Forum
I believe the key here is determining which image format to use, as each image format such as bmp, jpeg, png stores additional data (usually as preamble - or headers) other than just byte data and this additional data configuration can often be manufacturer specific. Print drivers usually handle this for you. I did try this once from a server, using PHP, to create XBM (X BitMap) images as this was more manageable and was able to then read the image byte code and send this to a printer via GSM/GPRS, so should work via Bluetooth. If trying to this from an app, maybe look out for converter utility functions that can convert from the more common jpeg/png to xbm or possibly raw (non-compressed) format.

Chris Ward

unread,
Jan 15, 2018, 7:56:32 PM1/15/18
to mitappinv...@googlegroups.com
Hi Marcel

The only problem is the QR Code, i store them on my server and generate them with php script.

I'd say there is another problem, the app will not work if the internet connection is down :)

Can they be generated with Javascript? Or indeed directly with AI? What exactly is your script generating, a PNG file?



The manual is not the greatest, crude translation from Chinese or Japanese. However, ESCZmn looks promising (pdf page 27). Essentially, this is the definition of the QR code, the format before it is converted into an image? So maybe you do not need to handle an image at all......




Daniel K.

unread,
Jan 16, 2018, 1:10:34 PM1/16/18
to MIT App Inventor Forum
Hi Marcel!
If your bluetooth printer doesn't support such a qr-code [Registrierkasse, right?], you will have to print it as an image. You already accomplished to create the image and you know how to send commands via bluetooth. Bitmap images can be printed with the following command:
GS v 0 m xL xH yL yH d1....dk
where x and y is the size of the image and d is the data.
Unfortunately, images cannot be printed directly. You have to convert your image into bytes in the following manner:
The image is divided into groups of 8 pixels. A white pixel has the value 0, a black pixel that will be printed has the value 1.
If your image is 0 1 0 0 0 0 1 0 (two black pixels, one in the second and one in the seventh column), the byte value of this group is 2^1 + 2^6 = 2 + 64 = 66
In the attachment you see a 8x8 smiley image and the corresponding byte values d1...d8
The problem now is how to perform this transformation? In my project I just needed it to print a singe logo, thus I did the transformation once on my PC and then copied the bytes manually into the app. You will have to generate it dynamically - maybe you can find or write an extension that can handle pixel values of an image?
Daniel
smiley.png
Reply all
Reply to author
Forward
0 new messages