Skip to content

Commit

Permalink
Optimize image logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Jul 4, 2022
1 parent 0e7afe1 commit 884b5fe
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Window x:Class="Portalum.TrwPrinter.EasyPrinterS3.ControlPanel.ImageConfigWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Portalum.TrwPrinter.EasyPrinterS3.ControlPanel"
mc:Ignorable="d"
Title="ImagePositionWindow"
Height="229"
Width="296"
Background="{StaticResource SecondaryColor}" WindowStartupLocation="CenterOwner">
<Grid>
<Button Style="{StaticResource BaseButtonStyle}" x:Name="ButtonFullSize" Content="Full Size 960x70" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="127" Height="30" Click="ButtonFullSize_Click"/>
<Button Style="{StaticResource BaseButtonStyle}" x:Name="ButtonLeftBottom" Content="Bottom Left 200x20" HorizontalAlignment="Left" Margin="10,79,0,0" VerticalAlignment="Top" Click="ButtonLeftBottom200x20_Click" Width="127" Height="29"/>
<Button Style="{StaticResource BaseButtonStyle}" x:Name="ButtonBottomRight200_20" Content="Bottom Right 200x20" HorizontalAlignment="Left" Margin="10,113,0,0" VerticalAlignment="Top" Width="127" Height="29" Click="ButtonBottomRight200x20_Click"/>
<Button Style="{StaticResource BaseButtonStyle}" x:Name="ButtonHalfSize" Content="Half Size 450x35" HorizontalAlignment="Left" Margin="11,45,0,0" VerticalAlignment="Top" Width="126" Height="29" Click="ButtonHalfSize_Click"/>
<Button Style="{StaticResource BaseButtonStyle}" x:Name="ButtonTopRight200_20" Content="Top Right 200x20" HorizontalAlignment="Left" Margin="10,147,0,0" VerticalAlignment="Top" Width="127" Height="29" Click="ButtonTopRight200_20_Click" />
</Grid>
</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Windows;

namespace Portalum.TrwPrinter.EasyPrinterS3.ControlPanel
{
/// <summary>
/// Interaction logic for ImagePositionWindow.xaml
/// </summary>
public partial class ImageConfigWindow : Window
{
public double X1 { get; set; }
public double X2 { get; set; }
public double Y1 { get; set; }
public double Y2 { get; set; }

public ImageConfigWindow()
{
this.InitializeComponent();
}

private void ButtonLeftBottom200x20_Click(object sender, RoutedEventArgs e)
{
this.X1 = 0;
this.X2 = 200;
this.Y1 = 0;
this.Y2 = 20;

this.DialogResult = true;
}

private void ButtonBottomRight200x20_Click(object sender, RoutedEventArgs e)
{
this.X1 = 760;
this.X2 = 960;
this.Y1 = 0;
this.Y2 = 20;

this.DialogResult = true;
}

private void ButtonFullSize_Click(object sender, RoutedEventArgs e)
{
this.X1 = 0;
this.X2 = 960;
this.Y1 = 0;
this.Y2 = 70;

this.DialogResult = true;
}

private void ButtonHalfSize_Click(object sender, RoutedEventArgs e)
{
this.X1 = 0;
this.X2 = 450;
this.Y1 = 0;
this.Y2 = 35;

this.DialogResult = true;
}

private void ButtonTopRight200_20_Click(object sender, RoutedEventArgs e)
{
this.X1 = 760;
this.X2 = 960;
this.Y1 = 50;
this.Y2 = 70;

this.DialogResult = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
</WindowChrome.WindowChrome>

<Grid>
<GroupBox Template="{StaticResource GroupBoxWithHeader}" x:Name="GroupBoxPrint" Header="Print" Foreground="White" Margin="0,184,0,0" HorizontalAlignment="Left" Width="310" Height="116" VerticalAlignment="Top">
<GroupBox Template="{StaticResource GroupBoxWithHeader}" x:Name="GroupBoxPrint" Header="Print / Erase" Foreground="White" Margin="0,184,0,0" HorizontalAlignment="Left" Width="310" Height="116" VerticalAlignment="Top">
<Grid>
<Button Style="{StaticResource BaseButtonStyle}" x:Name="ButtonPrintImageDemo" Content="Print Image Demo" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Click="ButtonPrintImageDemo_Click" Width="106" Height="29"/>
<Button Style="{StaticResource BaseButtonStyle}" x:Name="ButtonEraseCard" Content="Erase" HorizontalAlignment="Left" Margin="162,10,0,0" VerticalAlignment="Top" Click="ButtonEraseCard_Click" Width="80" Height="29"/>
<Button Style="{StaticResource BaseButtonStyle}" x:Name="ButtonPrintTextDemo" Content="Print Text Demo" HorizontalAlignment="Left" Margin="10,44,0,0" VerticalAlignment="Top" Click="ButtonPrintTextDemo_Click" Width="106" Height="29"/>
<Button Style="{StaticResource BaseButtonStyle}" x:Name="ButtonPrintFullDemo" Content="Print Full Demo" HorizontalAlignment="Left" Margin="162,44,0,0" VerticalAlignment="Top" Click="ButtonPrintFullDemo_Click" Width="106" Height="29"/>
<Button Style="{StaticResource BaseButtonStyle}" x:Name="ButtonPrintImageDemo" Content="Image Demo" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Click="ButtonPrintImageDemo_Click" Width="106" Height="29"/>
<Button Style="{StaticResource BaseButtonStyle}" x:Name="ButtonEraseCard" Content="Erase" HorizontalAlignment="Left" Margin="162,10,0,0" VerticalAlignment="Top" Click="ButtonEraseCard_Click" Width="106" Height="29"/>
<Button Style="{StaticResource BaseButtonStyle}" x:Name="ButtonPrintTextDemo" Content="Text Demo" HorizontalAlignment="Left" Margin="10,44,0,0" VerticalAlignment="Top" Click="ButtonPrintTextDemo_Click" Width="106" Height="29"/>
<Button Style="{StaticResource BaseButtonStyle}" x:Name="ButtonPrintFullDemo" Content="Full Demo" HorizontalAlignment="Left" Margin="162,44,0,0" VerticalAlignment="Top" Click="ButtonPrintFullDemo_Click" Width="106" Height="29"/>
</Grid>
</GroupBox>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,16 @@ private async void ButtonPrintImageDemo_Click(object sender, RoutedEventArgs e)

var filePath = openFileDialog.FileName;

var printDocument = new PrintDocument();
printDocument.AddElement(new ImagePrintElement(filePath, 200));
var imageConfigDialog = new ImageConfigWindow();
imageConfigDialog.Owner = this;
var imageConfigResult = imageConfigDialog.ShowDialog();
if (!imageConfigResult.HasValue || imageConfigResult == false)
{
return;
}

var printDocument = new PrintDocument();
printDocument.AddElement(new ImagePrintElement(filePath, imageConfigDialog.X1, imageConfigDialog.X2, imageConfigDialog.Y1, imageConfigDialog.Y2));
await this._printerClient.PrintDocumentAsync(printDocument);
}

Expand All @@ -232,21 +239,20 @@ private async void ButtonPrintTextDemo_Click(object sender, RoutedEventArgs e)
return;
}

var printDocument = new PrintDocument(rotate180Degree: false);
printDocument.AddElement(new TextPrintElement("X0/Y0", 0, 0));
printDocument.AddElement(new TextPrintElement("X15/Y0", 15, 0));
printDocument.AddElement(new TextPrintElement("X17/Y0", 17, 0));
printDocument.AddElement(new TextPrintElement("X20/Y0", 20, 0));
printDocument.AddElement(new TextPrintElement("X25/Y0 (Large)", 25, 0, TextSize.Large));
printDocument.AddElement(new TextPrintElement("X50/Y10 (Small)", 50, 10, TextSize.Small));
printDocument.AddElement(new TextPrintElement("X55/Y20", 55, 20));
printDocument.AddElement(new TextPrintElement("X200/Y30", 200, 30));
printDocument.AddElement(new TextPrintElement("X300/Y0", 300, 0));
printDocument.AddElement(new TextPrintElement("X300/Y40", 300, 40));
printDocument.AddElement(new TextPrintElement("X300/Y70", 300, 70));
printDocument.AddElement(new TextPrintElement("X400/Y50", 400, 50));
printDocument.AddElement(new TextPrintElement("X800/Y0", 800, 0));
printDocument.AddElement(new TextPrintElement("X800/Y50", 800, 50));
var printDocument = new PrintDocument();

printDocument.AddElement(new TextPrintElement("X0/Y0", 0, 0, TextSize.Small));
printDocument.AddElement(new TextPrintElement("X0/Y65", 0, 65, TextSize.Small));

printDocument.AddElement(new TextPrintElement("X0/Y35", 0, 35, TextSize.Medium));
printDocument.AddElement(new TextPrintElement("X400/Y35", 400, 35, TextSize.Large));
printDocument.AddElement(new TextPrintElement("X800/Y35", 800, 35, TextSize.Medium));

printDocument.AddElement(new TextPrintElement("X840/Y0", 840, 0, TextSize.Small));
printDocument.AddElement(new TextPrintElement("X840/Y65", 840, 65, TextSize.Small));

printDocument.AddElement(new TextPrintElement("X200/Y65 Rotated90", 200, 65, TextSize.Medium, TextOrientation.Rotated90));
printDocument.AddElement(new TextPrintElement("X300/Y20 double", 300, 20, TextSize.Medium, TextOrientation.Normal, true, true));

await this._printerClient.PrintDocumentAsync(printDocument);
}
Expand All @@ -268,10 +274,10 @@ private async void ButtonPrintFullDemo_Click(object sender, RoutedEventArgs e)
var filePath = openFileDialog.FileName;

var printDocument = new PrintDocument();
printDocument.AddElement(new ImagePrintElement(filePath, 650));
printDocument.AddElement(new TextPrintElement("Max Mustermann", 35, 650, TextSize.Large));
printDocument.AddElement(new TextPrintElement("Langestraße 4a", 42, 650, TextSize.Medium));
printDocument.AddElement(new TextPrintElement("10115 Berlin", 49, 650, TextSize.Medium));
printDocument.AddElement(new ImagePrintElement(filePath, 0, 400, 10, 60));
printDocument.AddElement(new TextPrintElement("Max Mustermann", 500, 40, TextSize.Large));
printDocument.AddElement(new TextPrintElement("Langestraße 4a", 500, 35, TextSize.Medium));
printDocument.AddElement(new TextPrintElement("10115 Berlin", 500, 30, TextSize.Medium));

await this._printerClient.PrintDocumentAsync(printDocument);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ImageHelperTest
public async Task SetDisplayTextAsyncTest()
{
var imageData = File.ReadAllBytes(@"../../../../../doc/2bit307x326.png");
var test = ImageHelper.GetImagePrintPackage(imageData, false);
var test = ImageHelper.GetImagePrintPackage(imageData);
}
}
}
7 changes: 1 addition & 6 deletions src/Portalum.TrwPrinter.EasyPrinterS3/Helpers/ImageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Portalum.TrwPrinter.EasyPrinterS3.Helpers
{
public static class ImageHelper
{
public static ImagePrintPackage GetImagePrintPackage(byte[] imageData, bool rotate90degree)
public static ImagePrintPackage GetImagePrintPackage(byte[] imageData)
{
using var image = Image.Load<Rgba32>(imageData);

Expand All @@ -28,11 +28,6 @@ public static ImagePrintPackage GetImagePrintPackage(byte[] imageData, bool rota
//FloydSteinberg->Good
//Stucki->Good

if (rotate90degree)
{
image.Mutate(x => x.Rotate(90));
}

var averageLuminance = CalculateAverageLuminance(imageData);
if (averageLuminance < 125)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>

<Version>1.0.9</Version>
<Version>1.0.10</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
9 changes: 3 additions & 6 deletions src/Portalum.TrwPrinter.EasyPrinterS3/PrintDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ public void AddElement(PrintElementBase printElement)
this._printElements.Add(printElement);
}

public async Task<byte[]> GetPrintDataAsync(
PrintPositionInfo printPositionInfo,
CancellationToken cancellationToken = default)
public async Task<byte[]> GetPrintDataAsync(CancellationToken cancellationToken = default)
{
var startY = 0;
var endY = 96;
var startX = 100;
var endX = 1100;
var startY = 0;
var endY = 96;

using var memoryStream = new MemoryStream();

Expand All @@ -67,7 +65,6 @@ public async Task<byte[]> GetPrintDataAsync(

foreach (var printElement in this._printElements)
{
printElement.SetPrintPositionInfo(printPositionInfo);
var elementPrintData = await printElement.GetPrintDataAsync();
await memoryStream.WriteAsync(elementPrintData, 0, elementPrintData.Length, cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,81 @@
using Portalum.TrwPrinter.EasyPrinterS3.Helpers;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System.Text;

namespace Portalum.TrwPrinter.EasyPrinterS3.PrintElements
{
public class ImagePrintElement : PrintElementBase
{
private readonly string _imagePath;
// neu: 2022-10-12
// private readonly int _positionY;
private readonly int _positionX1;
private readonly int _positionY1;
private readonly int _positionX2;
private readonly int _positionY2;
private readonly int _pixelMultiplier = 8;
private readonly int _paddingByteCount = 13;

private readonly bool _rotate90Degree;
private readonly string _imagePath;
private readonly double _positionX1;
private readonly double _positionX2;
private readonly double _positionY1;
private readonly double _positionY2;

public ImagePrintElement(
string imagePath,
int positionX1 = 0,
int positionY1 = 0,
int positionX2 = 0,
int positionY2 = 0,
bool rotate90Degree = false)
double positionX1,
double positionX2,
double positionY1,
double positionY2)
{
this._imagePath = imagePath;
this._positionX1 = positionX1;
this._positionY1 = positionY1;
this._positionX2 = positionX2;
this._positionY1 = positionY1;
this._positionY2 = positionY2;
this._rotate90Degree = rotate90Degree;
}

public override async Task<byte[]> GetPrintDataAsync(CancellationToken cancellationToken = default)
private async Task<byte[]> PreparePrintImageAsync(CancellationToken cancellationToken = default)
{
var paddingByteCount = 13;
var paddingData = Enumerable.Repeat((byte)0x00, paddingByteCount).ToArray();
var imagePrintCommand = new byte[] { 0x1B, 0x51 };
var width = (this._positionX2 - this._positionX1);
var height = (this._positionY2 - this._positionY1) * this._pixelMultiplier;
var offsetY = (this._maxHeight - this._positionY2) * this._pixelMultiplier;

#if (NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER)
var imageData = await File.ReadAllBytesAsync(this._imagePath, cancellationToken);
#else
var imageData = File.ReadAllBytes(this._imagePath);
#endif

var imagePrintPackage = ImageHelper.GetImagePrintPackage(imageData, this._rotate90Degree);
using var image = Image.Load<Rgba32>(imageData);
image.Mutate(x => x.Resize((int)width, (int)height, false));

using var printImage = new Image<Rgba32>((int)width, (int)(height + offsetY));

printImage.Mutate(o => o
.BackgroundColor(Color.White)
.DrawImage(image, new Point(0, (int)offsetY), 1f)
.Rotate(90)
);

using var memoryStreamPrintImage = new MemoryStream();
await printImage.SaveAsPngAsync(memoryStreamPrintImage);
printImage.SaveAsPng("test.png");

return memoryStreamPrintImage.ToArray();
}

public override async Task<byte[]> GetPrintDataAsync(CancellationToken cancellationToken = default)
{
var paddingData = Enumerable.Repeat((byte)0x00, this._paddingByteCount).ToArray();
var imagePrintCommand = new byte[] { 0x1B, 0x51 };

var imagePositionCommandData = new byte[] { 0x1B, 0x25, 0x79 };
//var offsetX = 1000 - this._positionX2;
//var offsetX = 130;
//var offsetX = (int)this._positionX2 - 200;// - this._startingPointOfPrintingAreaX;
//var imagePositionData = Encoding.ASCII.GetBytes($"{(int)offsetX:D4}");
var imagePositionData = Encoding.ASCII.GetBytes($"{this.ConvertX((int)this._positionX2):D4}");

var imagePositionCommandData = new byte[] { 0x1B, 0x25, 0x79 }; //%y
var printData = await this.PreparePrintImageAsync(cancellationToken);

// muss noch um PosX1 ... usw ergänzt werden
//var imagePositionData = Encoding.ASCII.GetBytes($"{this.GetPrinterPositionX(this._positionY):D4}");
var imagePositionData = Encoding.ASCII.GetBytes($"{this.ConvertX(this._positionX1):D4}");
var imagePrintPackage = ImageHelper.GetImagePrintPackage(printData);

using var memoryStream = new MemoryStream();

Expand All @@ -60,7 +86,7 @@ public override async Task<byte[]> GetPrintDataAsync(CancellationToken cancellat
{
await memoryStream.WriteAsync(imagePrintCommand, 0, imagePrintCommand.Length, cancellationToken);

var lengthInfoData = new byte[] { (byte)(imagePrintPackage.BytesPerRow + paddingByteCount) };
var lengthInfoData = new byte[] { (byte)(imagePrintPackage.BytesPerRow + this._paddingByteCount) };
await memoryStream.WriteAsync(lengthInfoData, 0, lengthInfoData.Length, cancellationToken);

await memoryStream.WriteAsync(paddingData, 0, paddingData.Length, cancellationToken);
Expand Down
Loading

0 comments on commit 884b5fe

Please sign in to comment.