171 lines
5.4 KiB
C#
171 lines
5.4 KiB
C#
using SixLabors.ImageSharp;
|
||
using SixLabors.ImageSharp.PixelFormats;
|
||
using SixLabors.ImageSharp.Processing;
|
||
using System.Collections.Generic;
|
||
using System.Text;
|
||
|
||
namespace JingGong.Abp.Print.GoDEX
|
||
{
|
||
/// <summary>
|
||
/// 打印图片
|
||
/// </summary>
|
||
public abstract class GoDEXPrintImage : GoDEXPrintData
|
||
{
|
||
/// <summary>
|
||
/// X起始位置,单位mm
|
||
/// </summary>
|
||
public int PosX { get; set; }
|
||
|
||
/// <summary>
|
||
/// Y起始位置,单位mm
|
||
/// </summary>
|
||
public int PosY { get; set; }
|
||
|
||
/// <summary>
|
||
/// 图片宽,单位mm
|
||
/// </summary>
|
||
public int Width { get; set; }
|
||
|
||
/// <summary>
|
||
/// 图片高,单位mm
|
||
/// </summary>
|
||
public int Height { get; set; }
|
||
|
||
/// <summary>
|
||
/// 打印图像
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public override bool Print()
|
||
{
|
||
var imageData = ProcessImageData();
|
||
|
||
//Q10,10,102,484(圖案命令x,y,width,heigh)
|
||
//x = 自左上角量起之水平位置(單位:dots).
|
||
//y = 自左上角量起之垂直位置(單位:dots).
|
||
//width = 圖檔寬度(單位:byte)
|
||
//height = 圖檔高度(單位:dots)
|
||
EzioInterop.sendcommand($"Q{PosX * Dots},{PosY * Dots},{imageData.Width},{imageData.Height}");
|
||
//EzioInterop.sendcommand($"Q40,10,3,8");
|
||
|
||
//发送图片数据
|
||
//EzioInterop.sendcommand(imageData);
|
||
EzioInterop.sendbuf(imageData.Data, imageData.Data.Length);
|
||
//EzioInterop.sendbuf(new byte[] { 0b01000001, 0b01000001, 0b01000001,
|
||
//0b01000001,0b01000001,0b01000001,
|
||
//0b01000001,0b01000001,0b01000001,
|
||
//0b01000001,0b01000001,0b01000001,
|
||
//0b01000001,0b01000001,0b01000001,
|
||
//0b01000001,0b01000001,0b01000001,
|
||
//0b01000001,0b01000001,0b01000001,
|
||
//0b01000001,0b01000001,0b01000001,}, 24);
|
||
|
||
EzioInterop.sendcommand("\n");
|
||
|
||
return true;
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 处理图片数据
|
||
/// </summary>
|
||
/// <param name="base64"></param>
|
||
/// <returns></returns>
|
||
protected virtual (int Width, int Height, byte[] Data) ProcessImageData()
|
||
{
|
||
var imageBytes = GetImageData();
|
||
|
||
// 加载图像
|
||
using (var image = Image.Load(imageBytes))
|
||
{
|
||
// 获取原始图像尺寸
|
||
int originalWidth = image.Width;
|
||
int originalHeight = image.Height;
|
||
|
||
image.Mutate(ctx =>
|
||
{
|
||
// 转换为灰度图像
|
||
// 进行二值化,128是阈值,低于该值为黑色,高于为白色
|
||
//image.Mutate(x => x.Grayscale().BinaryThreshold(128f / 255f));
|
||
ctx.Grayscale();
|
||
|
||
//调整图像大小
|
||
if (Width != 0 || Height != 0)
|
||
ctx.Resize(originalWidth * 2, originalHeight * 2, KnownResamplers.Bicubic);
|
||
|
||
////适当锐化
|
||
//ctx.GaussianSharpen(3f);
|
||
|
||
////调整对比度
|
||
//ctx.Contrast(1.2f);
|
||
|
||
});
|
||
|
||
return ConvertImageToBytes(image);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取图片数据
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
protected abstract byte[] GetImageData();
|
||
|
||
/// <summary>
|
||
/// 获取图像数据
|
||
/// </summary>
|
||
/// <param name="imageBytes"></param>
|
||
/// <returns></returns>
|
||
protected (int Width, int Height, byte[] Data) ConvertImageToBytes(Image image)
|
||
{
|
||
StringBuilder sb = new StringBuilder();
|
||
|
||
var clonedImage = image.CloneAs<Rgba32>();
|
||
|
||
|
||
// 创建一个列表来存储字节数据
|
||
List<byte> byteList = new List<byte>();
|
||
|
||
// 遍历每一行
|
||
for (int y = 0; y < image.Height; y++)
|
||
{
|
||
byte currentByte = 0;
|
||
int bitCount = 0;
|
||
|
||
for (int x = 0; x < image.Width; x++)
|
||
{
|
||
// 获取当前像素的颜色
|
||
var pixel = clonedImage[x, y];
|
||
|
||
// 黑色为1,白色为0(颠倒值)
|
||
//将灰度转为黑白,选择适当灰度值转为黑色
|
||
byte pixelValue = pixel.R <= 180 ? (byte)1 : (byte)0;
|
||
|
||
// 将像素值存入当前字节
|
||
currentByte |= (byte)(pixelValue << (7 - bitCount));
|
||
bitCount++;
|
||
|
||
// 如果一个字节已满,保存该字节并清空
|
||
if (bitCount == 8)
|
||
{
|
||
sb.Append((char)currentByte);
|
||
|
||
byteList.Add(currentByte);
|
||
currentByte = 0;
|
||
bitCount = 0;
|
||
|
||
}
|
||
}
|
||
|
||
// 如果行末不足8个像素,则补充零
|
||
if (bitCount > 0)
|
||
{
|
||
sb.Append((char)currentByte);
|
||
byteList.Add(currentByte);
|
||
}
|
||
}
|
||
|
||
return (image.Width % 8 == 0 ? image.Width / 8 : image.Width / 8 + 1, image.Height, byteList.ToArray());
|
||
}
|
||
}
|
||
}
|