Convert this C code to Gambas
Posted
#1
(In Topic #902)
Enthusiast

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
Posted
Guru

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>
Posted
Enthusiast

Posted
Regular

AndyGable said
this code from C
…C++
Europaeus sum !
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Posted
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
Posted
Guru

Posted
Enthusiast

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)
Posted
Enthusiast

Posted
Enthusiast

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)
Posted
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>
Posted
Enthusiast

The hard part is getting the image to convert and upload to the printer memory
1 guest and 0 members have just viewed this.



