Page 1 of 2

Convert this C code to Gambas

Posted: Thursday 15th September 2022 2:44pm
by AndyGable
Hi Everyone

Does anyone know how I can convert this code from C to Gambas?

Code: Select all

public static class ImagePrint
{
    /// <summary>
    /// Image convert to Byte Array
    /// </summary>
    /// <param name="LogoPath">Image Path</param>
    /// <param name="printWidth">Image print Horizontal Length</param>
    /// <returns></returns>
    public static byte[] GetLogo(string LogoPath, int printWidth)
    {
        List<byte> byteList = new List<byte>();
        if (!File.Exists(LogoPath))
            return null;
        BitmapData data = GetBitmapData(LogoPath, printWidth);
        BitArray dots = data.Dots;
        byte[] width = BitConverter.GetBytes(data.Width);

        int offset = 0;
        // Initialize Printer
        byteList.Add(Convert.ToByte(Convert.ToChar(0x1B)));
        byteList.Add(Convert.ToByte('@'));

        // Line Spacing Adjust (24/180 inch)
        byteList.Add(Convert.ToByte(Convert.ToChar(0x1B)));
        byteList.Add(Convert.ToByte('3'));
        byteList.Add((byte)24);
        while (offset < data.Height)
        {
            byteList.Add(Convert.ToByte(Convert.ToChar(0x1B)));
            byteList.Add(Convert.ToByte('*'));
            byteList.Add((byte)33);
            byteList.Add(width[0]);
            byteList.Add(width[1]);

            for (int x = 0; x < data.Width; ++x)
            {
                for (int k = 0; k < 3; ++k)
                {
                    byte slice = 0;
                    for (int b = 0; b < 8; ++b)
                    {
                        int y = (((offset / 8) + k) * 8) + b;
                        int i = (y * data.Width) + x;

                        bool v = false;
                        if (i < dots.Length)
                            v = dots[i];

                        slice |= (byte)((v ? 1 : 0) << (7 - b));
                    }
                    byteList.Add(slice);
                }
            }
            offset += 24;
            byteList.Add(Convert.ToByte(0x0A));
        }

        // Return to normal line spacing (30/160 inch)
        byteList.Add(Convert.ToByte(0x1B));
        byteList.Add(Convert.ToByte('3'));
        byteList.Add((byte)30);
        return byteList.ToArray();
    }

    private static BitmapData GetBitmapData(string bmpFileName, int width)
    {
        using (var bitmap = (Bitmap)Bitmap.FromFile(bmpFileName))
        {
            var threshold = 127;
            var index = 0;
            double multiplier = width; // 이미지 width조정
            double scale = (double)(multiplier / (double)bitmap.Width);
            int xheight = (int)(bitmap.Height * scale);
            int xwidth = (int)(bitmap.Width * scale);
            var dimensions = xwidth * xheight;
            var dots = new BitArray(dimensions);

            for (var y = 0; y < xheight; y++)
            {
                for (var x = 0; x < xwidth; x++)
                {
                    var _x = (int)(x / scale);
                    var _y = (int)(y / scale);
                    var color = bitmap.GetPixel(_x, _y);
                    var luminance = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
                    dots[index] = (luminance < threshold);
                    index++;
                }
            }

            return new BitmapData()
            {
                Dots = dots,
                Height = (int)(bitmap.Height * scale),
                Width = (int)(bitmap.Width * scale)
            };
        }
    }

    private class BitmapData
    {
        public BitArray Dots
        {
            get;
            set;
        }

        public int Height
        {
            get;
            set;
        }

        public int Width
        {
            get;
            set;
        }
    }
}
I think this is what I need to print a Logo onto a Thermal Receipt printer (I have a example of how to print a stored logo in the printers memory but no example of how to convert the image and uploaded it to the printers memory)

it produces the following image

Image

of course I would need to work out why the code is adding the extra spaces

Re: Convert this C code to Gambas

Posted: Thursday 15th September 2022 5:25pm
by cogier
Try a different method: -

1/. Create the logo in a drawing app. I used LibreOffice Draw
2/. Export the image to a PDF
3/. In Gambas shell "lp -d PrinterName /Path/to/pdf.pdf"

Done

This took me 10 mins to create and print on a Brother QL-570
Image

Re: Convert this C code to Gambas

Posted: Thursday 15th September 2022 6:43pm
by AndyGable
The recipit printers do not have drivers that you can install on Linux they all use their own ESC/POS commands to convert and print the images

Re: Convert this C code to Gambas

Posted: Thursday 15th September 2022 6:51pm
by vuott
AndyGable wrote: Thursday 15th September 2022 2:44pm this code from C
...C++

Re: Convert this C code to Gambas

Posted: Thursday 15th September 2022 8:15pm
by AndyGable
vuott wrote: Thursday 15th September 2022 6:51pm
AndyGable wrote: Thursday 15th September 2022 2:44pm this code from C
...C++
Thanks for correcting me I have a hard time recognizing code that is not BASIC or Visual Basic or Gambas :)

Re: Convert this C code to Gambas

Posted: Friday 16th September 2022 7:55am
by cogier
What is the printer model?

Re: Convert this C code to Gambas

Posted: Friday 16th September 2022 8:01am
by AndyGable
it is a Epson TM-88V

I have gone down the direct hardware route as I can (at some point) get the cash drawer status as low paper status from the printer and use that with in my EPoS application)

https://www.epson-biz.com/modules/ref_e ... ntent_id=2

This is the link to the ESC/PoS Manual that i have been using for 5 years (as I had a DOS Version of my software before moving it to Linux)

Re: Convert this C code to Gambas

Posted: Friday 16th September 2022 11:22am
by PJBlack
Bildschirmfoto vom 2022-09-16 13-19-50.png
Bildschirmfoto vom 2022-09-16 13-19-50.png (41.33 KiB) Viewed 1501 times
https://download.epson-biz.com/modules/ ... t=3&pid=36

Re: Convert this C code to Gambas

Posted: Friday 16th September 2022 11:40am
by AndyGable
They provide a way to print but it don't give any status to the application (like cover open or paper low)

Ideally I would love to work out how to use the JavaPoS drivers as that does everything for you and it does not matter what printer you're using. (It gives the status of the printer as well as the cash drawer)

Re: Convert this C code to Gambas

Posted: Friday 16th September 2022 3:11pm
by cogier
They provide a way to print but it don't give any status to the application (like cover open or paper low)
Does this help?

https://reference.epson-biz.com/modules ... =124#gs_lr

Image