Skip to content

Commit

Permalink
Added testcases for QR codes with logos/icons
Browse files Browse the repository at this point in the history
  • Loading branch information
codebude committed Nov 16, 2020
1 parent f6701c1 commit cd3c82a
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions QRCoderTests/QRCodeRendererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using QRCoderTests.XUnitExtenstions;
using System.IO;
using System.Security.Cryptography;
#if !NETCOREAPP1_1
using System.Drawing;
#endif

namespace QRCoderTests
{
Expand All @@ -30,6 +33,62 @@ public void can_create_standard_qrcode_graphic()

result.ShouldBe("41d3313c10d84034d67d476eec04163f");
}

[Fact]
[Category("QRRenderer/QRCode")]
public void can_create_qrcode_with_transparent_logo_graphic()
{
//Create dummy logo
var logo = new Bitmap(32, 32);
var gfx = Graphics.FromImage(logo);
gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
gfx.FillRectangle(Brushes.Transparent, new Rectangle(0, 0, 32, 32));
gfx.FillEllipse(Brushes.Red, new Rectangle(0, 0, 32, 32));
gfx.Save();

//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H);
var bmp = new QRCode(data).GetGraphic(10, Color.Black, Color.Transparent, icon: logo);

var ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
var imgBytes = ms.ToArray();
var md5 = new MD5CryptoServiceProvider();
var hash = md5.ComputeHash(imgBytes);
var result = BitConverter.ToString(hash).Replace("-", "").ToLower();
ms.Dispose();

result.ShouldBe("8f03d2c3fc5998cebb46658c3e6293b1");
}

[Fact]
[Category("QRRenderer/QRCode")]
public void can_create_qrcode_with_non_transparent_logo_graphic()
{
//Create dummy logo
var logo = new Bitmap(32, 32);
var gfx = Graphics.FromImage(logo);
gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
gfx.FillRectangle(Brushes.White, new Rectangle(0, 0, 32, 32));
gfx.FillEllipse(Brushes.Red, new Rectangle(0, 0, 32, 32));
gfx.Save();

//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H);
var bmp = new QRCode(data).GetGraphic(10, Color.Black, Color.White, icon: logo);

var ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
var imgBytes = ms.ToArray();
var md5 = new MD5CryptoServiceProvider();
var hash = md5.ComputeHash(imgBytes);
var result = BitConverter.ToString(hash).Replace("-", "").ToLower();
ms.Dispose();

result.ShouldBe("c2aae3658c7fa20cb5d22948d859c614");
}
#endif
}
}
Expand Down

0 comments on commit cd3c82a

Please sign in to comment.