通过 ESC/P 命令(写入 NVRam)将位图写入爱普生 TM88IV



参考链接:

  1. https://lh4.googleusercontent.com/-ceLtiOLVtME/UMBL1yf4RpI/AAAAAAAAAD0/itid5KibW_I/s1015/Doc1.png

  2. https://lh6.googleusercontent.com/-nrphBDA6sjk/UMA_Ukh7ZSI/AAAAAAAAADY/LwjkgobOaBg/s846/MappingTable.png

更新

我现在使用LockBits更改了图像转换,但失败了。有人可以给我一些建议吗?

/// <summary>
/// Orginal command Format in Decimal:
/// 29[GS], 40[(], 76[L], pL, PH, 48(m), 67[fn], 48[a], kc1, kc2, b, xL, xH, yL, yH, c, [d1 -- dk]
/// pL = Lower bit of sum of parameters (m to dk) = 11(m to c) + last bit of image
/// pH = Higher bit of sum of parameters (m to dk) = 11(m to c) + last bit of image
/// kc = Key Code of NVRam, kc1 = first bit of key code, kc2 = second bit of key code. P.S.: The key code will be hardcode H1.
/// b = number of colors = 1
/// xL & xH = Lower bit of image width, e.g. Width = 128 = 0x0080 then xL = 0x80, xH = 0x00
/// </summary>
/// <param name="pLogo"></param>
public void LoadImageToPrinter(Bitmap pLogo)
{
BitmapData oBmpData = pLogo.LockBits(new Rectangle(0, 0, pLogo.Width, pLogo.Height), ImageLockMode.ReadOnly, pLogo.PixelFormat);
//The list contains all the commands in Decimal format
List<int> oCommandList = new List<int>();
//k = (int((xL + xH × 256) + 7)/8) × (yL + yH × 256)
string HexValueOfX = pLogo.Width.ToString("X4");
string HexValueOfY = pLogo.Height.ToString("X4");
//k
int oExpectedImageByteCount = Math.Abs(oBmpData.Stride) * pLogo.Height;
//Total bit used for parameters
int oTotalParameterBitCount = 11 + oExpectedImageByteCount;
//The hex value for the oTotalParameterBitCount
string oTotalParameterBitCountInHex = oTotalParameterBitCount.ToString("X4").PadLeft(4, '0');
//GS, (, L
oCommandList.AddRange(new int[] { 29, 40, 76 });
//pL
oCommandList.Add(GetLowerHexValue(oTotalParameterBitCountInHex));
//pH
oCommandList.Add(GetHigherHexValue(oTotalParameterBitCountInHex));
//m,  fn, a,  kc1, kc2, b
oCommandList.AddRange(new int[] { 48, 67, 48, (int)'H', (int)'1', 1 });
//xL, xH
oCommandList.AddRange(new int[] { GetLowerHexValue(HexValueOfX), GetHigherHexValue(HexValueOfX) });
//yL, yH, c
oCommandList.AddRange(new int[] { GetLowerHexValue(HexValueOfY), GetHigherHexValue(HexValueOfY), 49 });
//Append the image bit to the List
byte[] oImageByte = new byte[oExpectedImageByteCount];
IntPtr oPtr = oBmpData.Scan0;
Marshal.Copy(oPtr, oImageByte, 0, oExpectedImageByteCount);
pLogo.UnlockBits(oBmpData);
//Clear NVRam
mThermalPrinterLibrary.Write(new int[] { 29, 40, 76, 5, 0, 48, 65, 67, 76, 82 }.IntArrayToCharString());
//Store graphics data
mThermalPrinterLibrary.Write(oCommandList.ToArray().IntArrayToCharString());
mThermalPrinterLibrary.Write(oImageByte);
//Print the graphics data
mThermalPrinterLibrary.Write(new int[] { 29, 40, 76, 6, 0, 48, 69, (int)'H', (int)'1', 1, 1 }.IntArrayToCharString());        
}

我多年前在 C 中做过这个。 我没有代码,但它不仅仅是一个片段。 为此,您需要执行以下操作。

  • 了解 BMP 文件格式 - 假设您正在从文件中读取位图。

  • 看看温迪。H(Microsoft Windows SDK),它具有文件头等的C样式定义:BITMAPFILEHEADER,BITMAPCOREHEADER,BITMAPINFOHEADER。

  • 处理
  • 标头以确定位图是否满足您的要求(例如,为了简化处理,您可能希望坚持位图正好为 128 x 98 (BITMAPINFOHEADER.biWidth, BITMAPINFOHEADER.biHeight),有一个平面 (BITMAPINFOHEADER.biPlanes = 1),是单色的 (BITMAPINFOHEADER.biBitCount =1 ),未压缩 (BITMAPINFOHEADER.biCompression = 0)。 这些限制不是绝对必要的,但会简化您的处理。

  • 处理像素数组,并将其转换为 ESCPOS 转义序列所需的格式。

或者,您可能希望放弃使用 ESCPOS,转而使用 OPOS/UPOS/POS for .NET,它提供了用于访问 POS 外围设备的更高级别的 API:POSPrinter 设备公开了一种打印位图的方法,并避免了您需要自己进行格式转换。 您需要下载Epson OPOS ADK才能执行此操作。 祝你好运!

更新

而爱普生的文件就像地狱一样,不明白它需要什么。

您对爱普生的文件有什么困难? 也许你可以尝试问一些关于你不理解的部分的具体问题。

更新 2

以下是一些提示:

GS ( L pL pH m fn [
  • 参数]:pL 和 pH 是 16 位整数值的低位和高阶字节,该整数值指定以下数据的长度(即m fn [参数])。

  • 对于 fn = 67(定义 NV 数据栅格格式),格式为GS ( L pL pH m fn a kc1 kc2 b xL xH yL yH [c d1...dk]1...[c d1...dk]b其中 m = 48,fn = 67,a = 48。

  • KC1 和 KC2 是用于在打印下载数据时识别下载数据的关键代码

  • b 指定下载数据的颜色数,1 表示单色,2 表示双色。

  • xL 和 xH 是 16 位整数值的低阶和高阶字节,该整数值定义图像的宽度(以点(像素)为单位)。

  • yL 和 yH 是 16 位整数值的低阶和高阶字节,该整数值定义图像的高度(以点(像素)为单位)。

  • 对于单色位图,将有一个块c d1 ...dk指定颜色c和像素数据。 对于彩色位图,有一个块c d1 ...每色 dk

  • 像素数据d1 ...dk有 k 个字节,其中k= (宽度 x (高度+7))/8。 设置为 1 的位表示打印该点(像素)。

我不知道以上内容是否比爱普生文档中的注释更清楚;如果不说你不明白哪些部分。

更新 3

下面是一个基于 128 宽 x 98 高位图的示例,如您的问题所示,为简单起见,单色。

您需要 k = (int(width + 7)/8) × (高度) = (
  • int(128+7)/8) x 98 = 16 x 98 = 1568 字节的像素数据(位 = 1 表示点打印): d1到 d1568

  • 假设颜色 c = 颜色 1 = 49 十进制 = 0x31十六进制(也可以是 0x32 或 0x33)

  • 宽度 = 128 = 0x0080,所以 xH = 0x00,xL = 0x80

  • 高度 = 98 = 0x0062,所以 yH = 0x00,yL = 0x62

  • b = 颜色数 = 1

  • 假设您的密钥是"H1",即 kc1 = 'H' = 0x48 和 kc2 = '1' = 0x31

  • 参数长度 =m fn a kc1 kc2 b xL xH yL yH 的长度 c d1...d 1568 =11 + 1568 = 1579= 0x062B十六进制,因此 pL = 0x2B,pH = 0x06。

因此,您发送的数据将是:

GS ( L pL pH m fn a kc1 kc2 b xL xH yL yH c d1...d1568

十六进制:

1D 28 4C 2B 06 30 43 30 48 31 01 80 00 62 00 31 d1...d1568

最新更新