495 lines
14 KiB
C#
495 lines
14 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Drawing;
|
||
using System.Linq;
|
||
using System.Text;
|
||
|
||
namespace PrintDemo
|
||
{
|
||
public class PrinterCmdUtils
|
||
{
|
||
/// <summary>
|
||
/// 换码
|
||
/// </summary>
|
||
public const byte ESC = 27; // 换码
|
||
/// <summary>
|
||
/// 文本分隔符
|
||
/// </summary>
|
||
public const byte FS = 28; // 文本分隔符
|
||
/// <summary>
|
||
/// 组分隔符
|
||
/// </summary>
|
||
public const byte GS = 29; // 组分隔符
|
||
/// <summary>
|
||
/// 数据连接换码
|
||
/// </summary>
|
||
public const byte DLE = 16; // 数据连接换码
|
||
/// <summary>
|
||
/// 传输结束
|
||
/// </summary>
|
||
public const byte EOT = 4; // 传输结束
|
||
/// <summary>
|
||
/// 询问字符
|
||
/// </summary>
|
||
public const byte ENQ = 5; // 询问字符
|
||
/// <summary>
|
||
/// 空格
|
||
/// </summary>
|
||
public const byte SP = 32; // 空格
|
||
/// <summary>
|
||
/// 横向列表
|
||
/// </summary>
|
||
public const byte HT = 9; // 横向列表
|
||
/// <summary>
|
||
/// 打印并换行(水平定位)
|
||
/// </summary>
|
||
public const byte LF = 10; // 打印并换行(水平定位)
|
||
/// <summary>
|
||
/// 归位键
|
||
/// </summary>
|
||
public const byte CR = 13; // 归位键
|
||
/// <summary>
|
||
/// 走纸控制(打印并回到标准模式(在页模式下))
|
||
/// </summary>
|
||
public const byte FF = 12; // 走纸控制(打印并回到标准模式(在页模式下) )
|
||
/// <summary>
|
||
/// 作废(页模式下取消打印数据 )
|
||
/// </summary>
|
||
public const byte CAN = 24; // 作废(页模式下取消打印数据 )
|
||
|
||
/// <summary>
|
||
/// 打印纸一行最大的字节
|
||
/// </summary>
|
||
private const int LINE_BYTE_SIZE = 32;
|
||
/// <summary>
|
||
/// 分隔符
|
||
/// </summary>
|
||
private const String SEPARATOR = "$";
|
||
private static StringBuilder sb = new StringBuilder();
|
||
|
||
/// <summary>
|
||
/// 打印机初始化
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static byte[] init_printer()
|
||
{
|
||
byte[] result = new byte[2];
|
||
result[0] = ESC;
|
||
result[1] = 64;
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 打开钱箱
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static byte[] open_money()
|
||
{
|
||
byte[] result = new byte[5];
|
||
result[0] = ESC;
|
||
result[1] = 112;
|
||
result[2] = 48;
|
||
result[3] = 64;
|
||
result[4] = 0;
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 换行
|
||
/// </summary>
|
||
/// <param name="lineNum">要换几行</param>
|
||
/// <returns></returns>
|
||
public static byte[] nextLine(int lineNum)
|
||
{
|
||
byte[] result = new byte[lineNum];
|
||
for (int i = 0; i < lineNum; i++)
|
||
{
|
||
result[i] = LF;
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 绘制下划线(1点宽)
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static byte[] underlineWithOneDotWidthOn()
|
||
{
|
||
byte[] result = new byte[3];
|
||
result[0] = ESC;
|
||
result[1] = 45;
|
||
result[2] = 1;
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 绘制下划线(2点宽)
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static byte[] underlineWithTwoDotWidthOn()
|
||
{
|
||
byte[] result = new byte[3];
|
||
result[0] = ESC;
|
||
result[1] = 45;
|
||
result[2] = 2;
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 取消绘制下划线
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static byte[] underlineOff()
|
||
{
|
||
byte[] result = new byte[3];
|
||
result[0] = ESC;
|
||
result[1] = 45;
|
||
result[2] = 0;
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 选择加粗模式
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static byte[] boldOn()
|
||
{
|
||
byte[] result = new byte[3];
|
||
result[0] = ESC;
|
||
result[1] = 69;
|
||
result[2] = 0xF;
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 取消加粗模式
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static byte[] boldOff()
|
||
{
|
||
byte[] result = new byte[3];
|
||
result[0] = ESC;
|
||
result[1] = 69;
|
||
result[2] = 0;
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 左对齐
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static byte[] alignLeft()
|
||
{
|
||
byte[] result = new byte[3];
|
||
result[0] = ESC;
|
||
result[1] = 97;
|
||
result[2] = 0;
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 居中对齐
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static byte[] alignCenter()
|
||
{
|
||
byte[] result = new byte[3];
|
||
result[0] = ESC;
|
||
result[1] = 97;
|
||
result[2] = 1;
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 右对齐
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static byte[] alignRight()
|
||
{
|
||
byte[] result = new byte[3];
|
||
result[0] = ESC;
|
||
result[1] = 97;
|
||
result[2] = 2;
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 水平方向向右移动col列
|
||
/// </summary>
|
||
/// <param name="col"></param>
|
||
/// <returns></returns>
|
||
public static byte[] set_HT_position(byte col)
|
||
{
|
||
byte[] result = new byte[4];
|
||
result[0] = ESC;
|
||
result[1] = 68;
|
||
result[2] = col;
|
||
result[3] = 0;
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 字体变大为标准的n倍
|
||
/// </summary>
|
||
/// <param name="num"></param>
|
||
/// <returns></returns>
|
||
public static byte[] fontSizeSetBig(int num)
|
||
{
|
||
byte realSize = 0;
|
||
switch (num)
|
||
{
|
||
case 1:
|
||
realSize = 0;
|
||
break;
|
||
case 2:
|
||
realSize = 17;
|
||
break;
|
||
case 3:
|
||
realSize = 34;
|
||
break;
|
||
case 4:
|
||
realSize = 51;
|
||
break;
|
||
case 5:
|
||
realSize = 68;
|
||
break;
|
||
case 6:
|
||
realSize = 85;
|
||
break;
|
||
case 7:
|
||
realSize = 102;
|
||
break;
|
||
case 8:
|
||
realSize = 119;
|
||
break;
|
||
}
|
||
byte[] result = new byte[3];
|
||
result[0] = 29;
|
||
result[1] = 33;
|
||
result[2] = realSize;
|
||
return result;
|
||
}
|
||
/// <summary>
|
||
/// 字体纵向变大为标准的n倍
|
||
/// </summary>
|
||
/// <param name="num"></param>
|
||
/// <returns></returns>
|
||
public static byte[] fontSizeSetHeight(int num)
|
||
{
|
||
byte realSize = 0;
|
||
switch (num)
|
||
{
|
||
case 1:
|
||
realSize = 0;
|
||
break;
|
||
case 2:
|
||
realSize = 1;
|
||
break;
|
||
case 3:
|
||
realSize = 2;
|
||
break;
|
||
case 4:
|
||
realSize = 3;
|
||
break;
|
||
case 5:
|
||
realSize = 4;
|
||
break;
|
||
case 6:
|
||
realSize = 5;
|
||
break;
|
||
case 7:
|
||
realSize = 6;
|
||
break;
|
||
case 8:
|
||
realSize = 7;
|
||
break;
|
||
}
|
||
byte[] result = new byte[3];
|
||
result[0] = 29;
|
||
result[1] = 33;
|
||
result[2] = realSize;
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 字体横向变大为标准的n倍
|
||
/// </summary>
|
||
/// <param name="num"></param>
|
||
/// <returns></returns>
|
||
public static byte[] fontSizeSetWidth(int num)
|
||
{
|
||
byte realSize = 0;
|
||
switch (num)
|
||
{
|
||
case 1:
|
||
realSize = 0;
|
||
break;
|
||
case 2:
|
||
realSize = 16;
|
||
break;
|
||
case 3:
|
||
realSize = 32;
|
||
break;
|
||
case 4:
|
||
realSize = 48;
|
||
break;
|
||
case 5:
|
||
realSize = 64;
|
||
break;
|
||
case 6:
|
||
realSize = 80;
|
||
break;
|
||
case 7:
|
||
realSize = 96;
|
||
break;
|
||
case 8:
|
||
realSize = 112;
|
||
break;
|
||
}
|
||
byte[] result = new byte[3];
|
||
result[0] = 29;
|
||
result[1] = 33;
|
||
result[2] = realSize;
|
||
return result;
|
||
}
|
||
/// <summary>
|
||
/// 字体取消倍宽倍高
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static byte[] fontSizeSetSmall()
|
||
{
|
||
byte[] result = new byte[3];
|
||
result[0] = ESC;
|
||
result[1] = 33;
|
||
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 进纸并全部切割
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static byte[] feedPaperCutAll()
|
||
{
|
||
byte[] result = new byte[4];
|
||
result[0] = GS;
|
||
result[1] = 86;
|
||
result[2] = 65;
|
||
result[3] = 0;
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 进纸并切割(左边留一点不切)
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static byte[] feedPaperCutPartial()
|
||
{
|
||
byte[] result = new byte[4];
|
||
result[0] = GS;
|
||
result[1] = 86;
|
||
result[2] = 66;
|
||
result[3] = 0;
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 打印图片
|
||
/// </summary>
|
||
/// <param name="bmp"></param>
|
||
/// <returns></returns>
|
||
public static byte[] bmpToByte(Bitmap bmp)
|
||
{
|
||
int h = bmp.Height / 24 + 1;
|
||
int w = bmp.Width;
|
||
byte[][] all = new byte[4 + 2 * h + h * w][];
|
||
|
||
all[0] = new byte[] { 0x1B, 0x33, 0x00 };
|
||
|
||
Color pixelColor;
|
||
// ESC * m nL nH 点阵图
|
||
byte[] escBmp = new byte[] { 0x1B, 0x2A, 0x21, (byte)(w % 256), (byte)(w / 256) };
|
||
|
||
// 每行进行打印
|
||
for (int i = 0; i < h; i++)
|
||
{
|
||
all[i * (w + 2) + 1] = escBmp;
|
||
for (int j = 0; j < w; j++)
|
||
{
|
||
byte[] data = new byte[] { 0x00, 0x00, 0x00 };
|
||
for (int k = 0; k < 24; k++)
|
||
{
|
||
if (((i * 24) + k) < bmp.Height)
|
||
{
|
||
pixelColor = bmp.GetPixel(j, (i * 24) + k);
|
||
if (pixelColor.R == 0)
|
||
{
|
||
data[k / 8] += (byte)(128 >> (k % 8));
|
||
}
|
||
}
|
||
}
|
||
all[i * (w + 2) + j + 2] = data;
|
||
}
|
||
//换行
|
||
all[(i + 1) * (w + 2)] = PrinterCmdUtils.nextLine(1);
|
||
}
|
||
all[h * (w + 2) + 1] = PrinterCmdUtils.nextLine(2);
|
||
all[h * (w + 2) + 2] = PrinterCmdUtils.feedPaperCutAll();
|
||
all[h * (w + 2) + 3] = PrinterCmdUtils.open_money();
|
||
|
||
return PrinterCmdUtils.byteMerger(all);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 切纸
|
||
/// </summary>
|
||
/// <param name="byte_1"></param>
|
||
/// <param name="byte_2"></param>
|
||
/// <returns></returns>
|
||
public static byte[] byteMerger(byte[] byte_1, byte[] byte_2)
|
||
{
|
||
byte[] byte_3 = new byte[byte_1.Length + byte_2.Length];
|
||
System.Array.Copy(byte_1, 0, byte_3, 0, byte_1.Length);
|
||
System.Array.Copy(byte_2, 0, byte_3, byte_1.Length, byte_2.Length);
|
||
return byte_3;
|
||
}
|
||
|
||
public static byte[] byteMerger(byte[][] byteList)
|
||
{
|
||
int Length = 0;
|
||
for (int i = 0; i < byteList.Length; i++)
|
||
{
|
||
Length += byteList[i].Length;
|
||
}
|
||
byte[] result = new byte[Length];
|
||
|
||
int index = 0;
|
||
for (int i = 0; i < byteList.Length; i++)
|
||
{
|
||
byte[] nowByte = byteList[i];
|
||
for (int k = 0; k < byteList[i].Length; k++)
|
||
{
|
||
result[index] = nowByte[k];
|
||
index++;
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public static byte[][] byte20Merger(byte[] bytes)
|
||
{
|
||
int size = bytes.Length / 10 + 1;
|
||
byte[][] result = new byte[size][];
|
||
for (int i = 0; i < size; i++)
|
||
{
|
||
byte[] by = new byte[((i + 1) * 10) - (i * 10)];
|
||
//从bytes中的第 i * 10 个位置到第 (i + 1) * 10 个位置;
|
||
System.Array.Copy(bytes, i * 10, by, 0, (i + 1) * 10);
|
||
result[i] = by;
|
||
}
|
||
return result;
|
||
}
|
||
}
|
||
}
|