Skip to content

Commit 079c236

Browse files
committed
Content-wise comparison/tests
Switched from simple byte comparison to a more "content-focused" method, because on different platforms the Bitmap encoder worked slightly different and thus let the testcases fail, even if images were visually identical.
1 parent cacb223 commit 079c236

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

QRCoderTests/QRCodeRendererTests.cs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace QRCoderTests
1414

1515
public class QRCodeRendererTests
1616
{
17+
1718
#if !NETCOREAPP1_1
1819
[Fact]
1920
[Category("QRRenderer/QRCode")]
@@ -24,14 +25,14 @@ public void can_create_standard_qrcode_graphic()
2425
var bmp = new QRCode(data).GetGraphic(10);
2526

2627
var ms = new MemoryStream();
27-
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
28+
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
2829
var imgBytes = ms.ToArray();
2930
var md5 = new MD5CryptoServiceProvider();
3031
var hash = md5.ComputeHash(imgBytes);
3132
var result = BitConverter.ToString(hash).Replace("-", "").ToLower();
3233
ms.Dispose();
3334

34-
result.ShouldBe("41d3313c10d84034d67d476eec04163f");
35+
result.ShouldBe("a76c8a72e95df3368717663c6be41b3e");
3536
}
3637
#endif
3738

@@ -42,7 +43,7 @@ private string GetAssemblyPath()
4243
{
4344
return
4445
#if NET5_0
45-
System.Reflection.Assembly.GetExecutingAssembly().Location;
46+
AppDomain.CurrentDomain.BaseDirectory;
4647
#else
4748
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace("file:\\", "");
4849
#endif
@@ -59,15 +60,12 @@ public void can_create_qrcode_with_transparent_logo_graphic()
5960
var bmp = new QRCode(data).GetGraphic(10, Color.Black, Color.Transparent, icon: (Bitmap)Image.FromFile(GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"));
6061
//Used logo is licensed under public domain. Ref.: https://thenounproject.com/Iconathon1/collection/redefining-women/?i=2909346
6162

62-
var ms = new MemoryStream();
63-
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
64-
var imgBytes = ms.ToArray();
63+
var imgBytes = PixelsToAveragedByteArray(bmp);
6564
var md5 = new MD5CryptoServiceProvider();
6665
var hash = md5.ComputeHash(imgBytes);
6766
var result = BitConverter.ToString(hash).Replace("-", "").ToLower();
68-
ms.Dispose();
6967

70-
result.ShouldBe("ee65d96c3013f6032b561cc768251eef");
68+
result.ShouldBe("33c250bf306b7cbbd3dd71b6029b8784");
7169
}
7270

7371
[Fact]
@@ -80,16 +78,36 @@ public void can_create_qrcode_with_non_transparent_logo_graphic()
8078
var bmp = new QRCode(data).GetGraphic(10, Color.Black, Color.White, icon: (Bitmap)Bitmap.FromFile(GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"));
8179
//Used logo is licensed under public domain. Ref.: https://thenounproject.com/Iconathon1/collection/redefining-women/?i=2909346
8280

83-
var ms = new MemoryStream();
84-
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
85-
var imgBytes = ms.ToArray();
81+
var imgBytes = PixelsToAveragedByteArray(bmp);
8682
var md5 = new MD5CryptoServiceProvider();
8783
var hash = md5.ComputeHash(imgBytes);
8884
var result = BitConverter.ToString(hash).Replace("-", "").ToLower();
89-
ms.Dispose();
9085

91-
result.ShouldBe("1d718f06f904af4a46748f02af2d4eec");
86+
result.ShouldBe("33c250bf306b7cbbd3dd71b6029b8784");
9287
}
88+
89+
90+
private static byte[] PixelsToAveragedByteArray(Bitmap bmp)
91+
{
92+
//Re-color
93+
var bmpTmp = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
94+
using (var gr = Graphics.FromImage(bmp))
95+
gr.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
96+
97+
//Downscale
98+
var bmpSmall = new Bitmap(bmpTmp, new Size(16, 16));
99+
100+
var bytes = new System.Collections.Generic.List<byte>();
101+
for (int x = 0; x < bmpSmall.Width; x++)
102+
{
103+
for (int y = 0; y < bmpSmall.Height; y++)
104+
{
105+
bytes.AddRange(new byte[] { bmpSmall.GetPixel(x, y).R, bmpSmall.GetPixel(x, y).G, bmpSmall.GetPixel(x, y).B });
106+
}
107+
}
108+
return bytes.ToArray();
109+
}
110+
93111
#endif
94112
}
95113
}

QRCoderTests/SvgQRCodeRendererTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ private string GetAssemblyPath()
2121
{
2222
return
2323
#if NET5_0
24-
System.Reflection.Assembly.GetExecutingAssembly().Location;
24+
AppDomain.CurrentDomain.BaseDirectory;
2525
#else
2626
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace("file:\\", "");
2727
#endif

0 commit comments

Comments
 (0)