Skip to content

Commit 884b5fe

Browse files
committed
Optimize image logic
1 parent 0e7afe1 commit 884b5fe

File tree

11 files changed

+244
-99
lines changed

11 files changed

+244
-99
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Window x:Class="Portalum.TrwPrinter.EasyPrinterS3.ControlPanel.ImageConfigWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:Portalum.TrwPrinter.EasyPrinterS3.ControlPanel"
7+
mc:Ignorable="d"
8+
Title="ImagePositionWindow"
9+
Height="229"
10+
Width="296"
11+
Background="{StaticResource SecondaryColor}" WindowStartupLocation="CenterOwner">
12+
<Grid>
13+
<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"/>
14+
<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"/>
15+
<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"/>
16+
<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"/>
17+
<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" />
18+
</Grid>
19+
</Window>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System.Windows;
2+
3+
namespace Portalum.TrwPrinter.EasyPrinterS3.ControlPanel
4+
{
5+
/// <summary>
6+
/// Interaction logic for ImagePositionWindow.xaml
7+
/// </summary>
8+
public partial class ImageConfigWindow : Window
9+
{
10+
public double X1 { get; set; }
11+
public double X2 { get; set; }
12+
public double Y1 { get; set; }
13+
public double Y2 { get; set; }
14+
15+
public ImageConfigWindow()
16+
{
17+
this.InitializeComponent();
18+
}
19+
20+
private void ButtonLeftBottom200x20_Click(object sender, RoutedEventArgs e)
21+
{
22+
this.X1 = 0;
23+
this.X2 = 200;
24+
this.Y1 = 0;
25+
this.Y2 = 20;
26+
27+
this.DialogResult = true;
28+
}
29+
30+
private void ButtonBottomRight200x20_Click(object sender, RoutedEventArgs e)
31+
{
32+
this.X1 = 760;
33+
this.X2 = 960;
34+
this.Y1 = 0;
35+
this.Y2 = 20;
36+
37+
this.DialogResult = true;
38+
}
39+
40+
private void ButtonFullSize_Click(object sender, RoutedEventArgs e)
41+
{
42+
this.X1 = 0;
43+
this.X2 = 960;
44+
this.Y1 = 0;
45+
this.Y2 = 70;
46+
47+
this.DialogResult = true;
48+
}
49+
50+
private void ButtonHalfSize_Click(object sender, RoutedEventArgs e)
51+
{
52+
this.X1 = 0;
53+
this.X2 = 450;
54+
this.Y1 = 0;
55+
this.Y2 = 35;
56+
57+
this.DialogResult = true;
58+
}
59+
60+
private void ButtonTopRight200_20_Click(object sender, RoutedEventArgs e)
61+
{
62+
this.X1 = 760;
63+
this.X2 = 960;
64+
this.Y1 = 50;
65+
this.Y2 = 70;
66+
67+
this.DialogResult = true;
68+
}
69+
}
70+
}

src/Portalum.TrwPrinter.EasyPrinterS3.ControlPanel/MainWindow.xaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
</WindowChrome.WindowChrome>
2121

2222
<Grid>
23-
<GroupBox Template="{StaticResource GroupBoxWithHeader}" x:Name="GroupBoxPrint" Header="Print" Foreground="White" Margin="0,184,0,0" HorizontalAlignment="Left" Width="310" Height="116" VerticalAlignment="Top">
23+
<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">
2424
<Grid>
25-
<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"/>
26-
<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"/>
27-
<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"/>
28-
<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"/>
25+
<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"/>
26+
<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"/>
27+
<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"/>
28+
<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"/>
2929
</Grid>
3030
</GroupBox>
3131

src/Portalum.TrwPrinter.EasyPrinterS3.ControlPanel/MainWindow.xaml.cs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,16 @@ private async void ButtonPrintImageDemo_Click(object sender, RoutedEventArgs e)
219219

220220
var filePath = openFileDialog.FileName;
221221

222-
var printDocument = new PrintDocument();
223-
printDocument.AddElement(new ImagePrintElement(filePath, 200));
222+
var imageConfigDialog = new ImageConfigWindow();
223+
imageConfigDialog.Owner = this;
224+
var imageConfigResult = imageConfigDialog.ShowDialog();
225+
if (!imageConfigResult.HasValue || imageConfigResult == false)
226+
{
227+
return;
228+
}
224229

230+
var printDocument = new PrintDocument();
231+
printDocument.AddElement(new ImagePrintElement(filePath, imageConfigDialog.X1, imageConfigDialog.X2, imageConfigDialog.Y1, imageConfigDialog.Y2));
225232
await this._printerClient.PrintDocumentAsync(printDocument);
226233
}
227234

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

235-
var printDocument = new PrintDocument(rotate180Degree: false);
236-
printDocument.AddElement(new TextPrintElement("X0/Y0", 0, 0));
237-
printDocument.AddElement(new TextPrintElement("X15/Y0", 15, 0));
238-
printDocument.AddElement(new TextPrintElement("X17/Y0", 17, 0));
239-
printDocument.AddElement(new TextPrintElement("X20/Y0", 20, 0));
240-
printDocument.AddElement(new TextPrintElement("X25/Y0 (Large)", 25, 0, TextSize.Large));
241-
printDocument.AddElement(new TextPrintElement("X50/Y10 (Small)", 50, 10, TextSize.Small));
242-
printDocument.AddElement(new TextPrintElement("X55/Y20", 55, 20));
243-
printDocument.AddElement(new TextPrintElement("X200/Y30", 200, 30));
244-
printDocument.AddElement(new TextPrintElement("X300/Y0", 300, 0));
245-
printDocument.AddElement(new TextPrintElement("X300/Y40", 300, 40));
246-
printDocument.AddElement(new TextPrintElement("X300/Y70", 300, 70));
247-
printDocument.AddElement(new TextPrintElement("X400/Y50", 400, 50));
248-
printDocument.AddElement(new TextPrintElement("X800/Y0", 800, 0));
249-
printDocument.AddElement(new TextPrintElement("X800/Y50", 800, 50));
242+
var printDocument = new PrintDocument();
243+
244+
printDocument.AddElement(new TextPrintElement("X0/Y0", 0, 0, TextSize.Small));
245+
printDocument.AddElement(new TextPrintElement("X0/Y65", 0, 65, TextSize.Small));
246+
247+
printDocument.AddElement(new TextPrintElement("X0/Y35", 0, 35, TextSize.Medium));
248+
printDocument.AddElement(new TextPrintElement("X400/Y35", 400, 35, TextSize.Large));
249+
printDocument.AddElement(new TextPrintElement("X800/Y35", 800, 35, TextSize.Medium));
250+
251+
printDocument.AddElement(new TextPrintElement("X840/Y0", 840, 0, TextSize.Small));
252+
printDocument.AddElement(new TextPrintElement("X840/Y65", 840, 65, TextSize.Small));
253+
254+
printDocument.AddElement(new TextPrintElement("X200/Y65 Rotated90", 200, 65, TextSize.Medium, TextOrientation.Rotated90));
255+
printDocument.AddElement(new TextPrintElement("X300/Y20 double", 300, 20, TextSize.Medium, TextOrientation.Normal, true, true));
250256

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

270276
var printDocument = new PrintDocument();
271-
printDocument.AddElement(new ImagePrintElement(filePath, 650));
272-
printDocument.AddElement(new TextPrintElement("Max Mustermann", 35, 650, TextSize.Large));
273-
printDocument.AddElement(new TextPrintElement("Langestraße 4a", 42, 650, TextSize.Medium));
274-
printDocument.AddElement(new TextPrintElement("10115 Berlin", 49, 650, TextSize.Medium));
277+
printDocument.AddElement(new ImagePrintElement(filePath, 0, 400, 10, 60));
278+
printDocument.AddElement(new TextPrintElement("Max Mustermann", 500, 40, TextSize.Large));
279+
printDocument.AddElement(new TextPrintElement("Langestraße 4a", 500, 35, TextSize.Medium));
280+
printDocument.AddElement(new TextPrintElement("10115 Berlin", 500, 30, TextSize.Medium));
275281

276282
await this._printerClient.PrintDocumentAsync(printDocument);
277283
}

src/Portalum.TrwPrinter.EasyPrinterS3.UnitTest/ImageHelperTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ImageHelperTest
1212
public async Task SetDisplayTextAsyncTest()
1313
{
1414
var imageData = File.ReadAllBytes(@"../../../../../doc/2bit307x326.png");
15-
var test = ImageHelper.GetImagePrintPackage(imageData, false);
15+
var test = ImageHelper.GetImagePrintPackage(imageData);
1616
}
1717
}
1818
}

src/Portalum.TrwPrinter.EasyPrinterS3/Helpers/ImageHelper.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Portalum.TrwPrinter.EasyPrinterS3.Helpers
1010
{
1111
public static class ImageHelper
1212
{
13-
public static ImagePrintPackage GetImagePrintPackage(byte[] imageData, bool rotate90degree)
13+
public static ImagePrintPackage GetImagePrintPackage(byte[] imageData)
1414
{
1515
using var image = Image.Load<Rgba32>(imageData);
1616

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

31-
if (rotate90degree)
32-
{
33-
image.Mutate(x => x.Rotate(90));
34-
}
35-
3631
var averageLuminance = CalculateAverageLuminance(imageData);
3732
if (averageLuminance < 125)
3833
{

src/Portalum.TrwPrinter.EasyPrinterS3/Portalum.TrwPrinter.EasyPrinterS3.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

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

26-
<Version>1.0.9</Version>
26+
<Version>1.0.10</Version>
2727
</PropertyGroup>
2828

2929
<ItemGroup>

src/Portalum.TrwPrinter.EasyPrinterS3/PrintDocument.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@ public void AddElement(PrintElementBase printElement)
3333
this._printElements.Add(printElement);
3434
}
3535

36-
public async Task<byte[]> GetPrintDataAsync(
37-
PrintPositionInfo printPositionInfo,
38-
CancellationToken cancellationToken = default)
36+
public async Task<byte[]> GetPrintDataAsync(CancellationToken cancellationToken = default)
3937
{
40-
var startY = 0;
41-
var endY = 96;
4238
var startX = 100;
4339
var endX = 1100;
40+
var startY = 0;
41+
var endY = 96;
4442

4543
using var memoryStream = new MemoryStream();
4644

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

6866
foreach (var printElement in this._printElements)
6967
{
70-
printElement.SetPrintPositionInfo(printPositionInfo);
7168
var elementPrintData = await printElement.GetPrintDataAsync();
7269
await memoryStream.WriteAsync(elementPrintData, 0, elementPrintData.Length, cancellationToken);
7370
}

src/Portalum.TrwPrinter.EasyPrinterS3/PrintElements/ImagePrintElement.cs

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,81 @@
11
using Portalum.TrwPrinter.EasyPrinterS3.Helpers;
2+
using SixLabors.ImageSharp;
3+
using SixLabors.ImageSharp.PixelFormats;
4+
using SixLabors.ImageSharp.Processing;
25
using System.Text;
36

47
namespace Portalum.TrwPrinter.EasyPrinterS3.PrintElements
58
{
69
public class ImagePrintElement : PrintElementBase
710
{
8-
private readonly string _imagePath;
9-
// neu: 2022-10-12
10-
// private readonly int _positionY;
11-
private readonly int _positionX1;
12-
private readonly int _positionY1;
13-
private readonly int _positionX2;
14-
private readonly int _positionY2;
11+
private readonly int _pixelMultiplier = 8;
12+
private readonly int _paddingByteCount = 13;
1513

16-
private readonly bool _rotate90Degree;
14+
private readonly string _imagePath;
15+
private readonly double _positionX1;
16+
private readonly double _positionX2;
17+
private readonly double _positionY1;
18+
private readonly double _positionY2;
1719

1820
public ImagePrintElement(
1921
string imagePath,
20-
int positionX1 = 0,
21-
int positionY1 = 0,
22-
int positionX2 = 0,
23-
int positionY2 = 0,
24-
bool rotate90Degree = false)
22+
double positionX1,
23+
double positionX2,
24+
double positionY1,
25+
double positionY2)
2526
{
2627
this._imagePath = imagePath;
2728
this._positionX1 = positionX1;
28-
this._positionY1 = positionY1;
2929
this._positionX2 = positionX2;
30+
this._positionY1 = positionY1;
3031
this._positionY2 = positionY2;
31-
this._rotate90Degree = rotate90Degree;
3232
}
3333

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

4040
#if (NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER)
4141
var imageData = await File.ReadAllBytesAsync(this._imagePath, cancellationToken);
4242
#else
4343
var imageData = File.ReadAllBytes(this._imagePath);
4444
#endif
4545

46-
var imagePrintPackage = ImageHelper.GetImagePrintPackage(imageData, this._rotate90Degree);
46+
using var image = Image.Load<Rgba32>(imageData);
47+
image.Mutate(x => x.Resize((int)width, (int)height, false));
48+
49+
using var printImage = new Image<Rgba32>((int)width, (int)(height + offsetY));
50+
51+
printImage.Mutate(o => o
52+
.BackgroundColor(Color.White)
53+
.DrawImage(image, new Point(0, (int)offsetY), 1f)
54+
.Rotate(90)
55+
);
56+
57+
using var memoryStreamPrintImage = new MemoryStream();
58+
await printImage.SaveAsPngAsync(memoryStreamPrintImage);
59+
printImage.SaveAsPng("test.png");
60+
61+
return memoryStreamPrintImage.ToArray();
62+
}
63+
64+
public override async Task<byte[]> GetPrintDataAsync(CancellationToken cancellationToken = default)
65+
{
66+
var paddingData = Enumerable.Repeat((byte)0x00, this._paddingByteCount).ToArray();
67+
var imagePrintCommand = new byte[] { 0x1B, 0x51 };
68+
69+
var imagePositionCommandData = new byte[] { 0x1B, 0x25, 0x79 };
70+
//var offsetX = 1000 - this._positionX2;
71+
//var offsetX = 130;
72+
//var offsetX = (int)this._positionX2 - 200;// - this._startingPointOfPrintingAreaX;
73+
//var imagePositionData = Encoding.ASCII.GetBytes($"{(int)offsetX:D4}");
74+
var imagePositionData = Encoding.ASCII.GetBytes($"{this.ConvertX((int)this._positionX2):D4}");
4775

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

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

5480
using var memoryStream = new MemoryStream();
5581

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

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

6692
await memoryStream.WriteAsync(paddingData, 0, paddingData.Length, cancellationToken);

0 commit comments

Comments
 (0)