some additional bullshit such as printing arbitrary text as bitmaps

This commit is contained in:
Maff 2019-10-01 17:01:07 +01:00
parent ad8a799c18
commit e9d702f955
2 changed files with 32 additions and 12 deletions

View File

@ -14,10 +14,11 @@ namespace libpaperang {
public byte End; public byte End;
} }
public enum Model { public enum Model {
P1, P1, // Original model; 57mm feed, 48-byte lines (200DPI), LiPo battery (1Ah)
T1, P1S,// Original "special edition" model; identical to P1 but in different colours
P2, T1, // Label printer model; 15mm feed, unknown-byte lines, 4xAAA battery
P2S P2, // Hi-DPI model; 57mm feed, 96-byte lines (300DPI), LiPo battery (1Ah)
P2S // Hi-DPI "special edition" model; identical to P2 but includes Pomodoro timer functionality
} }
public enum State { public enum State {
Offline, Offline,
@ -36,6 +37,7 @@ namespace libpaperang {
CrcTransmit, CrcTransmit,
Print Print
} }
//I'm sure there are other opcodes available but these are the "core" ones implemented by the official MiaoMiaoJi chinese Windows and OSX apps
public struct Opcodes { public struct Opcodes {
public byte[] NoOp; public byte[] NoOp;
public byte[] LineFeed; public byte[] LineFeed;

View File

@ -46,7 +46,22 @@ namespace paperangapp {
logger.Debug("PrinterInitialised? " + mmj.Printer.PrinterInitialised); logger.Debug("PrinterInitialised? " + mmj.Printer.PrinterInitialised);
logger.Debug("Printer initialised and ready"); logger.Debug("Printer initialised and ready");
} }
private void BtTestLine_Click(object sender, RoutedEventArgs e) => mmj.Feed(200); private void BtTestLine_Click(object sender, RoutedEventArgs e) {
string Font="Consolas";
int FontSize=48;
Bitmap b=new Bitmap(mmj.Printer.LineWidth*8, (FontSize+4)*10);
g=Graphics.FromImage(b);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.DrawString("hello", new Font(Font, FontSize), Brushes.Black, new RectangleF(0, 0, b.Width, b.Height));
g.Flush();
PrintBitmap(b);
g.Dispose();
b.Dispose();
}
private void BtLoadImage_Click(object sender, RoutedEventArgs e) { private void BtLoadImage_Click(object sender, RoutedEventArgs e) {
logger.Debug("Loading image for print"); logger.Debug("Loading image for print");
OpenFileDialog r = new OpenFileDialog { OpenFileDialog r = new OpenFileDialog {
@ -68,22 +83,25 @@ namespace paperangapp {
logger.Debug("Loaded image as Bitmap"); logger.Debug("Loaded image as Bitmap");
_.Dispose(); _.Dispose();
logger.Debug("Disposed of Image"); logger.Debug("Disposed of Image");
bimg=CopyToBpp(bimg); PrintBitmap(bimg);
}
private void PrintBitmap(Bitmap bimg) {
bimg = CopyToBpp(bimg);
logger.Debug("Converted Bitmap to Bitmap with 1-bit colour depth"); logger.Debug("Converted Bitmap to Bitmap with 1-bit colour depth");
//BitArray img = new BitArray(bimg.Height*96*8); //BitArray img = new BitArray(bimg.Height*96*8);
byte[] iimg = new byte[bimg.Height*mmj.Printer.LineWidth]; byte[] iimg = new byte[bimg.Height*mmj.Printer.LineWidth];
byte[] img; byte[] img;
using (MemoryStream s = new MemoryStream()) { using(MemoryStream s = new MemoryStream()) {
bimg.Save(s, ImageFormat.Bmp); bimg.Save(s, ImageFormat.Bmp);
img=s.ToArray(); img = s.ToArray();
} }
logger.Debug("Got bitmap's bytes"); logger.Debug("Got bitmap's bytes");
int startoffset=img.Length-(bimg.Height*mmj.Printer.LineWidth); int startoffset=img.Length-(bimg.Height*mmj.Printer.LineWidth);
logger.Debug("Processing bytes with offset " + startoffset); logger.Debug("Processing bytes with offset " + startoffset);
for(int h=0;h<bimg.Height;h++) { for(int h = 0; h < bimg.Height; h++) {
for (int w=0;w< mmj.Printer.LineWidth; w++) { for(int w = 0; w < mmj.Printer.LineWidth; w++) {
iimg[(mmj.Printer.LineWidth * (bimg.Height-1-h))+(mmj.Printer.LineWidth - 1-w)]=(byte)~ iimg[(mmj.Printer.LineWidth * (bimg.Height - 1 - h)) + (mmj.Printer.LineWidth - 1 - w)] = (byte)~
(img[startoffset+(mmj.Printer.LineWidth * h)+(mmj.Printer.LineWidth - 1-w)]); (img[startoffset + (mmj.Printer.LineWidth * h) + (mmj.Printer.LineWidth - 1 - w)]);
} }
} }
logger.Debug("Have print data of length " + iimg.Length); logger.Debug("Have print data of length " + iimg.Length);