Convert this C code to Gambas

Post

Posted
Rating:
#1 (In Topic #902)
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
Hi Everyone

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

Code

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

<IMG src="https://i.stack.imgur.com/ti4ML.jpg%5D"> </IMG>

of course I would need to work out why the code is adding the extra spaces
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
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
<IMG src="https://www.cogier.com/gambas/feicom.png"> </IMG>
Online now: No Back to the top

Post

Posted
Rating:
#3
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
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
Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
vuott is in the usergroup ‘Regular’

AndyGable said

 this code from C

…C++

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#5
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

vuott said

AndyGable said

 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 :)
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Guru
cogier is in the usergroup ‘Guru’
What is the printer model?
Online now: No Back to the top

Post

Posted
Rating:
#7
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
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.…os/index.php?content_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)
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Enthusiast
PJBlack is in the usergroup ‘Enthusiast’
Online now: No Back to the top

Post

Posted
Rating:
#9
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
 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)
Online now: No Back to the top

Post

Posted
Rating:
#10
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.epso….php?content_id=124#gs_lr

<IMG src="https://www.cogier.com/gambas/Epson1.png"> </IMG>
Online now: No Back to the top

Post

Posted
Rating:
#11
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
 Yea that's what I'm going to be working on this weekend

The hard part is getting the image to convert and upload to the printer memory
Online now: No Back to the top
1 guest and 0 members have just viewed this.