Skip to content

Instantly share code, notes, and snippets.

@Alvarocda
Created June 9, 2020 12:31
Show Gist options
  • Save Alvarocda/1e7f155d73a4fd9bd11b5e5dceb94b5a to your computer and use it in GitHub Desktop.
Save Alvarocda/1e7f155d73a4fd9bd11b5e5dceb94b5a to your computer and use it in GitHub Desktop.
public async Task ImprimeLogo()
{
byte[] ESC_Init = { 27, 64 };
byte[] LF = { 10 };
byte[] restPrinter = { (byte)27, (byte)64 };
BluetoothSocket socket = BSocketPadrao;
SKBitmap bmp1;
using (var stream = Forms.Context.Resources.OpenRawResource(Resource.Drawable.semTitulo))
{
bmp1 = SKBitmap.Decode(stream);
}
Bitmap bmp = bmp1.ToBitmap();
BitmapUtils bmpUtils = new BitmapUtils();
// O Segundo parametro é a quantiade de pontos por linha que a impressora suporte
// Impressoras de 58mm = 384, impressoras de 80mm = 576
byte[] byteArray = await bmpUtils.POS_PrintBMP(bmp, 384 , 0);
await socket.OutputStream.WriteAsync(ESC_Init, 0, ESC_Init.Length);
await socket.OutputStream.WriteAsync(LF, 0, LF.Length);
await socket.OutputStream.WriteAsync(SELECT_BIT_IMAGE_MODE, 0, SELECT_BIT_IMAGE_MODE.Length);
await socket.OutputStream.WriteAsync(byteArray, 0, byteArray.Length);
await socket.OutputStream.WriteAsync(LF, 0, LF.Length);
await socket.OutputStream.WriteAsync(restPrinter, 0, restPrinter.Length);
}
class BitmapUtils
{
private static int WIDTH_58 = 384;
private static int WIDTH_80 = 576;
private static byte[] chartobyte;
private static int[] p0;
private static int[] p1;
private static int[] p2;
private static int[] p3;
private static int[] p4;
private static int[] p5;
private static int[] p6;
public byte[] buf;
public int index;
public BitmapUtils()
{
p0 = new int[] { 0, 128 };
p1 = new int[] { 0, 64 };
p2 = new int[] { 0, 32 };
p3 = new int[] { 0, 16 };
p4 = new int[] { 0, 8 };
p5 = new int[] { 0, 4 };
p6 = new int[] { 0, 2 };
chartobyte = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15 };
}
public async Task<byte[]> POS_PrintBMP(Bitmap bitmap, int n, int n2)
{
n = (n + 7) / 8 * 8;
int n3 = (bitmap.Height * n / bitmap.Width + 7) / 8;
Bitmap bitmap2 = bitmap;
if (bitmap.Width == n)
{
await EachLinePixToCmd(await ThresholdToBWPic(await ToGrayscale(bitmap2)), n, n2);
}
bitmap2 = await ResizeImage(bitmap, n, n3 * 8);
return await EachLinePixToCmd(await ThresholdToBWPic(await ToGrayscale(bitmap2)), n, n2);
/*var1 = (var1 + 7) / 8 * 8;
int var3 = (bitmap.Height * var1 / bitmap.Width + 7) / 8;
Bitmap var4 = bitmap;
if (bitmap.Width != var1)
{
var4 = await ResizeImage(bitmap, var1, var3 * 8);
}
return await EachLinePixToCmd(await ThresholdToBWPic(await ToGrayscale(var4)), var1, var2);*/
}
public async Task<Bitmap> ToGrayscale(Bitmap bitmap)
{
Bitmap bitmap2 = Bitmap.CreateBitmap(bitmap.Width, bitmap.Height, Bitmap.Config.Argb8888);
Canvas canvas = new Canvas(bitmap2);
Paint paint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.SetSaturation(0.0f);
paint.SetColorFilter((ColorFilter)new ColorMatrixColorFilter(colorMatrix));
canvas.DrawBitmap(bitmap, 0.0f, 0.0f, paint);
return bitmap2;
}
public async Task<byte[]> ThresholdToBWPic(Bitmap bitmap)
{
int[] array = new int[bitmap.Width * bitmap.Height];
byte[] array2 = new byte[bitmap.Width * bitmap.Height];
bitmap.GetPixels(array, 0, bitmap.Width, 0, 0, bitmap.Width, bitmap.Height);
FormatKThreshold(array, bitmap.Width, bitmap.Height, array2);
return array2;
}
private void FormatKThreshold(int[] array, int n, int n2, byte[] array2)
{
int i = 0;
int n3 = 0;
int n4 = 0;
while (i < n2)
{
for (int j = 0; j < n; ++j)
{
n3 += (array[n4] & 0xFF);
++n4;
}
++i;
}
int n5 = n3 / n2 / n;
int n6 = 0;
for (int k = 0; k < n2; ++k)
{
for (int l = 0; l < n; ++l)
{
if ((array[n6] & 0xFF) > n5)
{
array2[n6] = 0;
}
else
{
array2[n6] = 1;
}
++n6;
}
}
}
public async Task<Bitmap> ResizeImage(Bitmap bitmap, int var1, int var2)
{
int width = bitmap.Width;
int height = bitmap.Height;
float var5 = (float)var1 / (float)width;
float var6 = (float)var2 / (float)height;
Matrix matrix = new Matrix();
matrix.PostScale(var5, var6);
return Bitmap.CreateBitmap(bitmap, 0, 0, width, height, matrix, true);
}
public async Task<byte[]> EachLinePixToCmd(byte[] array, int n, int n2)
{
int n3 = array.Length / n;
int n4 = n / 8;
int n5 = n4 + 8;
byte[] array2 = new byte[n3 * n5];
int i = 0;
n = 0;
while (i < n3)
{
int n6 = i * n5;
array2[n6 + 0] = 29;
array2[n6 + 1] = 118;
array2[n6 + 2] = 48;
array2[n6 + 3] = (byte)(n2 & 0x1);
array2[n6 + 4] = (byte)(n4 % 256);
array2[n6 + 5] = (byte)(n4 / 256);
array2[n6 + 6] = 1;
array2[n6 + 7] = 0;
for (int j = 0; j < n4; ++j)
{
array2[n6 + 8 + j] = (byte)(p0[array[n]] + p1[array[n + 1]] + p2[array[n + 2]] + p3[array[n + 3]] + p4[array[n + 4]] + p5[array[n + 5]] + p6[array[n + 6]] + array[n + 7]);
n += 8;
}
++i;
}
return array2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment