0

We can convert an image to a byte array for sending to ESC/POS printer.
I use the code here: https://stackoverflow.com/a/14099717/1853275
it works like charm.

simple question: is it possible to convert this byte array back to original image?
if yes, please tell me how...

bigger picture:
I have created a TCP/IP listener which acts as a POS printer.
It receives data from a POS application and then forwards to real printer.
in between, I have to get the products on the receipt and print something extra based on the products.
so, I need to convert the graphics data to image (then OCR) to read the products on receipt.

here's one captured ESC/POS receipt:
pastebin.com/vUDHjGuj
(we can convert it to byte array, then send to printer. it prints a receipt.)

I know the structure of captured data. we can remove ESC/POS commands and split the array by 0x0A LF. I just need a way to convert the graphics byte arrays to image and put the slices together to create image of receipt.
graphics mode sent to printer is: 24 bit double density (0x1B 0x2A 0x21)

please help...
many thanks

below is one of the pieces I tried, but it gives all black.
I send one slice (byte[]), with width=512 (from captured data), height: 1 (or any)

public static Bitmap FromByteArray(byte[] ByteArray, Size Size)

{

Bitmap bmp = new Bitmap(Size.Width, Size.Height, PixelFormat.Format24bppRgb);

BitmapData bmpdata = bmp.LockBits(new Rectangle(0, 0, Size.Width, Size.Height),ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

IntPtr ptr = bmpdata.Scan0;

System.Runtime.InteropServices.Marshal.Copy(ByteArray, 0, ptr, ByteArray.Length);

bmp.UnlockBits(bmpdata);

return bmp;

}
XeeChaun
  • 57
  • 1
  • 7
  • Try to write the opposite of bitmap to ESC conversionr, undoubtedly it's not just a single line copy otherwise all the other code was useless...don't you think so? – Adriano Repetti May 10 '18 at 07:29
  • @AdrianoRepetti I understand that I need reverse of that. but I needed help/hints in doing that. or possibly if someone have done that. if I knew how to do it already, I don't know why would I post a question – XeeChaun May 10 '18 at 07:58
  • But unfortunately it's not the kind of questions that are on topic in Stack Overflow. You are more or less asking "how to do it" or "do you already have that code?". See also [ask] – Adriano Repetti May 10 '18 at 09:07

1 Answers1

4

You are working with a printer's page description language, so firstly just be aware that this is not an entry-level task.

In your code, there is no specific "fix". It appears to be based on some assumptions that are not grounded in the realities of this data format.

  • Your sample file is an image? No, there are other commands in the file as well (cut, newline) that you need to be able to parse and ignore to get at the image data.
  • Your sample file is one image? No, there are 20 small images in your sample file in thin slices.
  • Your image is 24 bits per pixel? No, there is one bit per pixel, arranged in columns that are 24 pixels tall. Each image has an arbitrary width which you can read from the data.
  • Raster data is laid out in rows? No, it's column-major.

So if you want working C# code, as mentioned in a comment, you need to correctly reverse the process in C# example you linked. You can inform this effort with a good debugger, a copy of the Epson documentation for ESC *, and some small sample files to convert.

Example

It's absolutely possible to parse ESC/POS to recover the original image. I maintain an open source project which does this, and it will process your sample file (github, related blog post). The code for parsing ESC * is located in SelectBitImageMode.php. It's not C#, but it works.

I had to convert your sample file back to binary to use it:

cat esc_pos_graphics_printing_data.txt | tr -d '\r' | tr -d '\n' | \
    python3 -c "import sys, binascii; sys.stdout.buffer.write(binascii.unhexlify(input().strip()))" > \
    esc_pos_graphics_printing_data.bin

Then to extract the images from your receipt and join them, I used escpos-tools (git, composer, php) plus ImageMagick:

git clone https://github.com/receipt-print-hq/escpos-tools.git
cd escpos-tools
composer install
php escimages.php --file ../esc_pos_graphics_printing_data.bin 
convert esc_pos_graphics_printing_data-*.png -append output.png

The receipt is:

enter image description here

Best of luck!

mike42
  • 1,608
  • 14
  • 24
  • many thanks for spending your time on this. I wish I had found those few points easily that day. but I could find nothing. I then spent a few hours on that code which generates ESC/POS commands by taking image, and figured out everything myself, the hard way. I was able to reproduce the same in C#. – XeeChaun May 20 '18 at 14:33