@@ -14,6 +14,7 @@ namespace QRCoderTests
14
14
15
15
public class QRCodeRendererTests
16
16
{
17
+
17
18
#if ! NETCOREAPP1_1
18
19
[ Fact ]
19
20
[ Category ( "QRRenderer/QRCode" ) ]
@@ -24,14 +25,14 @@ public void can_create_standard_qrcode_graphic()
24
25
var bmp = new QRCode ( data ) . GetGraphic ( 10 ) ;
25
26
26
27
var ms = new MemoryStream ( ) ;
27
- bmp . Save ( ms , System . Drawing . Imaging . ImageFormat . Bmp ) ;
28
+ bmp . Save ( ms , System . Drawing . Imaging . ImageFormat . Gif ) ;
28
29
var imgBytes = ms . ToArray ( ) ;
29
30
var md5 = new MD5CryptoServiceProvider ( ) ;
30
31
var hash = md5 . ComputeHash ( imgBytes ) ;
31
32
var result = BitConverter . ToString ( hash ) . Replace ( "-" , "" ) . ToLower ( ) ;
32
33
ms . Dispose ( ) ;
33
34
34
- result . ShouldBe ( "41d3313c10d84034d67d476eec04163f " ) ;
35
+ result . ShouldBe ( "a76c8a72e95df3368717663c6be41b3e " ) ;
35
36
}
36
37
#endif
37
38
@@ -42,7 +43,7 @@ private string GetAssemblyPath()
42
43
{
43
44
return
44
45
#if NET5_0
45
- System . Reflection . Assembly . GetExecutingAssembly ( ) . Location ;
46
+ AppDomain . CurrentDomain . BaseDirectory ;
46
47
#else
47
48
Path . GetDirectoryName ( System . Reflection . Assembly . GetExecutingAssembly ( ) . CodeBase ) . Replace ( "file:\\ " , "" ) ;
48
49
#endif
@@ -59,15 +60,12 @@ public void can_create_qrcode_with_transparent_logo_graphic()
59
60
var bmp = new QRCode ( data ) . GetGraphic ( 10 , Color . Black , Color . Transparent , icon : ( Bitmap ) Image . FromFile ( GetAssemblyPath ( ) + "\\ assets\\ noun_software engineer_2909346.png" ) ) ;
60
61
//Used logo is licensed under public domain. Ref.: https://thenounproject.com/Iconathon1/collection/redefining-women/?i=2909346
61
62
62
- var ms = new MemoryStream ( ) ;
63
- bmp . Save ( ms , System . Drawing . Imaging . ImageFormat . Png ) ;
64
- var imgBytes = ms . ToArray ( ) ;
63
+ var imgBytes = PixelsToAveragedByteArray ( bmp ) ;
65
64
var md5 = new MD5CryptoServiceProvider ( ) ;
66
65
var hash = md5 . ComputeHash ( imgBytes ) ;
67
66
var result = BitConverter . ToString ( hash ) . Replace ( "-" , "" ) . ToLower ( ) ;
68
- ms . Dispose ( ) ;
69
67
70
- result . ShouldBe ( "ee65d96c3013f6032b561cc768251eef " ) ;
68
+ result . ShouldBe ( "33c250bf306b7cbbd3dd71b6029b8784 " ) ;
71
69
}
72
70
73
71
[ Fact ]
@@ -80,16 +78,36 @@ public void can_create_qrcode_with_non_transparent_logo_graphic()
80
78
var bmp = new QRCode ( data ) . GetGraphic ( 10 , Color . Black , Color . White , icon : ( Bitmap ) Bitmap . FromFile ( GetAssemblyPath ( ) + "\\ assets\\ noun_software engineer_2909346.png" ) ) ;
81
79
//Used logo is licensed under public domain. Ref.: https://thenounproject.com/Iconathon1/collection/redefining-women/?i=2909346
82
80
83
- var ms = new MemoryStream ( ) ;
84
- bmp . Save ( ms , System . Drawing . Imaging . ImageFormat . Png ) ;
85
- var imgBytes = ms . ToArray ( ) ;
81
+ var imgBytes = PixelsToAveragedByteArray ( bmp ) ;
86
82
var md5 = new MD5CryptoServiceProvider ( ) ;
87
83
var hash = md5 . ComputeHash ( imgBytes ) ;
88
84
var result = BitConverter . ToString ( hash ) . Replace ( "-" , "" ) . ToLower ( ) ;
89
- ms . Dispose ( ) ;
90
85
91
- result . ShouldBe ( "1d718f06f904af4a46748f02af2d4eec " ) ;
86
+ result . ShouldBe ( "33c250bf306b7cbbd3dd71b6029b8784 " ) ;
92
87
}
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
+
93
111
#endif
94
112
}
95
113
}
0 commit comments