|
2 | 2 |
|
3 | 3 | namespace OpenCvSharp.Tests.WeChatQRCode; |
4 | 4 |
|
| 5 | +#pragma warning disable CA1707 // Identifiers should not contain underscores |
| 6 | + |
5 | 7 | public class WeChatQRCodeTest(ITestOutputHelper testOutputHelper) : TestBase |
6 | 8 | { |
7 | | - private const string WechatQcodeDetectorPrototxtPath = "_data/wechat_qrcode/detect.prototxt"; |
8 | | - private const string WechatQcodeDetectorCaffeModelPath = "_data/wechat_qrcode/detect.caffemodel"; |
9 | | - private const string WechatQcodeSuperResolutionPrototxtPath = "_data/wechat_qrcode/sr.prototxt"; |
10 | | - private const string WechatQcodeSuperResolutionCaffeModelPath = "_data/wechat_qrcode/sr.caffemodel"; |
| 9 | + private const string DetectorPrototxtPath = "_data/wechat_qrcode/detect.prototxt"; |
| 10 | + private const string DetectorCaffeModelPath = "_data/wechat_qrcode/detect.caffemodel"; |
| 11 | + private const string SuperResolutionPrototxtPath = "_data/wechat_qrcode/sr.prototxt"; |
| 12 | + private const string SuperResolutionCaffeModelPath = "_data/wechat_qrcode/sr.caffemodel"; |
| 13 | + |
| 14 | + private static readonly string[] ExpectedMultiQRTexts = |
| 15 | + [ |
| 16 | + "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[]^_`{|}", |
| 17 | + "Helloこんにちは你好안녕하세요" |
| 18 | + ]; |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Requires no model files. Verifies the no-arg constructor succeeds. |
| 22 | + /// </summary> |
| 23 | + [Fact] |
| 24 | + public void Constructor_Default_DoesNotThrow() |
| 25 | + { |
| 26 | + using var qr = new OpenCvSharp.WeChatQRCode(); |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Passing null for any string argument must throw ArgumentNullException. |
| 31 | + /// </summary> |
| 32 | + [Theory] |
| 33 | + [InlineData(null, "", "", "")] |
| 34 | + [InlineData("", null, "", "")] |
| 35 | + [InlineData("", "", null, "")] |
| 36 | + [InlineData("", "", "", null)] |
| 37 | + public void Constructor_NullArguments_ThrowsArgumentNullException( |
| 38 | + string? a, string? b, string? c, string? d) |
| 39 | + { |
| 40 | + Assert.Throws<ArgumentNullException>(() => new OpenCvSharp.WeChatQRCode(a!, b!, c!, d!)); |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// DetectAndDecode must throw ArgumentNullException when inputImage is null. |
| 45 | + /// Does not require model files. |
| 46 | + /// </summary> |
| 47 | + [Fact] |
| 48 | + public void DetectAndDecode_NullInput_ThrowsArgumentNullException() |
| 49 | + { |
| 50 | + using var qr = new OpenCvSharp.WeChatQRCode(); |
| 51 | + Assert.Throws<ArgumentNullException>(() => qr.DetectAndDecode(null!, out _)); |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Grayscale image containing 2 QR codes. Both must be decoded correctly. |
| 56 | + /// </summary> |
| 57 | + [Fact] |
| 58 | + public void DetectAndDecode_WithModels_MultiQR_ReturnsTexts() |
| 59 | + { |
| 60 | + SkipIfModelFilesNotFound(); |
| 61 | + |
| 62 | + using var qr = CreateWithModels(); |
| 63 | + using var src = Cv2.ImRead("_data/image/qr_multi.png", ImreadModes.Grayscale); |
11 | 64 |
|
| 65 | + var texts = qr.DetectAndDecode(src, out _); |
| 66 | + |
| 67 | + Assert.Equal(2, texts.Length); |
| 68 | + foreach (var text in texts) |
| 69 | + { |
| 70 | + testOutputHelper.WriteLine(text); |
| 71 | + Assert.NotEmpty(text); |
| 72 | + } |
| 73 | + Assert.Equal( |
| 74 | + ExpectedMultiQRTexts.OrderBy(x => x), |
| 75 | + texts.OrderBy(x => x)); |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Point2f[][] overload must return one array of 4 corners per detected QR code. |
| 80 | + /// </summary> |
12 | 81 | [Fact] |
13 | | - public void WechatQrcodeDecodeRun() |
| 82 | + public void DetectAndDecode_WithModels_Point2fOverload_Returns4CornersPerQR() |
14 | 83 | { |
15 | | - Assert.True(File.Exists(WechatQcodeDetectorPrototxtPath), $"DetectorPrototxt '{WechatQcodeDetectorPrototxtPath}' not found"); |
16 | | - Assert.True(File.Exists(WechatQcodeDetectorCaffeModelPath), $"DetectorcaffeModel '{WechatQcodeDetectorCaffeModelPath}' not found"); |
17 | | - Assert.True(File.Exists(WechatQcodeSuperResolutionPrototxtPath), $"SuperResolutionprototxt '{WechatQcodeSuperResolutionPrototxtPath}' not found"); |
18 | | - Assert.True(File.Exists(WechatQcodeSuperResolutionCaffeModelPath), $"SuperResolutionCaffe_model '{WechatQcodeSuperResolutionCaffeModelPath}' not found"); |
| 84 | + SkipIfModelFilesNotFound(); |
| 85 | + |
| 86 | + using var qr = CreateWithModels(); |
| 87 | + using var src = Cv2.ImRead("_data/image/qr_multi.png", ImreadModes.Grayscale); |
| 88 | + |
| 89 | + var texts = qr.DetectAndDecode(src, out var points); |
| 90 | + |
| 91 | + Assert.Equal(2, texts.Length); |
| 92 | + Assert.Equal(2, points.Length); |
| 93 | + foreach (var corners in points) |
| 94 | + { |
| 95 | + Assert.Equal(4, corners.Length); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Mat[] overload (DetectAndDecodeRaw) must return one non-empty Mat per detected QR code, |
| 101 | + /// with 4 rows (corners) and 2 columns (x, y). |
| 102 | + /// </summary> |
| 103 | + [Fact] |
| 104 | + public void DetectAndDecodeRaw_WithModels_MatOverload_ReturnsCorrectShape() |
| 105 | + { |
| 106 | + SkipIfModelFilesNotFound(); |
| 107 | + |
| 108 | + using var qr = CreateWithModels(); |
| 109 | + using var src = Cv2.ImRead("_data/image/qr_multi.png", ImreadModes.Grayscale); |
19 | 110 |
|
20 | | - using var wechatQrcode = OpenCvSharp.WeChatQRCode.Create( |
21 | | - WechatQcodeDetectorPrototxtPath, WechatQcodeDetectorCaffeModelPath, |
22 | | - WechatQcodeSuperResolutionPrototxtPath, WechatQcodeSuperResolutionCaffeModelPath); |
23 | | - using var src = Cv2.ImRead(@"_data/image/qr_multi.png", ImreadModes.Grayscale); |
| 111 | + var texts = qr.DetectAndDecodeRaw(src, out var bbox); |
24 | 112 |
|
25 | | - wechatQrcode.DetectAndDecode(src, out var rects, out var texts); |
26 | | - Assert.NotEmpty(texts); |
27 | 113 | Assert.Equal(2, texts.Length); |
28 | | - foreach (var item in texts) |
| 114 | + Assert.Equal(2, bbox.Length); |
| 115 | + foreach (var mat in bbox) |
29 | 116 | { |
30 | | - testOutputHelper.WriteLine(item); |
31 | | - Assert.NotEmpty(item); |
| 117 | + Assert.False(mat.Empty()); |
| 118 | + // each corner mat stores 4 (x,y) values → Total() * Channels() == 8 |
| 119 | + Assert.Equal(8, (int)(mat.Total() * mat.Channels())); |
32 | 120 | } |
33 | 121 | } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// QR code containing single-byte (ASCII) characters must decode to the expected string. |
| 125 | + /// </summary> |
| 126 | + [Fact] |
| 127 | + public void DetectAndDecode_WithModels_SinglebyteLetters() |
| 128 | + { |
| 129 | + SkipIfModelFilesNotFound(); |
| 130 | + |
| 131 | + using var qr = CreateWithModels(); |
| 132 | + using var src = Cv2.ImRead("_data/image/qr_singlebyte_letters.png", ImreadModes.Grayscale); |
| 133 | + |
| 134 | + var texts = qr.DetectAndDecode(src, out _); |
| 135 | + |
| 136 | + Assert.Single(texts); |
| 137 | + Assert.Equal( |
| 138 | + "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[]^_`{|}", |
| 139 | + texts[0]); |
| 140 | + } |
| 141 | + |
| 142 | + /// <summary> |
| 143 | + /// QR code containing multibyte (Unicode) characters must decode to the expected string. |
| 144 | + /// </summary> |
| 145 | + [Fact] |
| 146 | + public void DetectAndDecode_WithModels_MultibyteLetters() |
| 147 | + { |
| 148 | + SkipIfModelFilesNotFound(); |
| 149 | + |
| 150 | + using var qr = CreateWithModels(); |
| 151 | + using var src = Cv2.ImRead("_data/image/qr_multibyte_letters.png", ImreadModes.Grayscale); |
| 152 | + |
| 153 | + var texts = qr.DetectAndDecode(src, out _); |
| 154 | + |
| 155 | + Assert.Single(texts); |
| 156 | + Assert.Equal("Helloこんにちは你好안녕하세요", texts[0]); |
| 157 | + } |
| 158 | + |
| 159 | + /// <summary> |
| 160 | + /// detectAndDecode accepts a color (BGR) image in addition to grayscale. |
| 161 | + /// The result must be identical to processing the grayscale version. |
| 162 | + /// </summary> |
| 163 | + [Fact] |
| 164 | + public void DetectAndDecode_WithModels_ColorBGRImage_DetectsQR() |
| 165 | + { |
| 166 | + SkipIfModelFilesNotFound(); |
| 167 | + |
| 168 | + using var qr = CreateWithModels(); |
| 169 | + using var colorSrc = Cv2.ImRead("_data/image/qr_multi.png", ImreadModes.Color); |
| 170 | + |
| 171 | + var texts = qr.DetectAndDecode(colorSrc, out _); |
| 172 | + |
| 173 | + Assert.Equal(2, texts.Length); |
| 174 | + Assert.Equal( |
| 175 | + ExpectedMultiQRTexts.OrderBy(x => x), |
| 176 | + texts.OrderBy(x => x)); |
| 177 | + } |
| 178 | + |
| 179 | + /// <summary> |
| 180 | + /// An image that contains no QR codes must yield empty result arrays. |
| 181 | + /// </summary> |
| 182 | + [Fact] |
| 183 | + public void DetectAndDecode_WithModels_NoQRCode_ReturnsEmpty() |
| 184 | + { |
| 185 | + SkipIfModelFilesNotFound(); |
| 186 | + |
| 187 | + using var qr = CreateWithModels(); |
| 188 | + using var src = LoadImage("lenna.png", ImreadModes.Grayscale); |
| 189 | + |
| 190 | + var texts = qr.DetectAndDecode(src, out var points); |
| 191 | + |
| 192 | + Assert.Empty(texts); |
| 193 | + Assert.Empty(points); |
| 194 | + } |
| 195 | + |
| 196 | + // ------------------------------------------------------------------------- |
| 197 | + |
| 198 | + private static OpenCvSharp.WeChatQRCode CreateWithModels() => |
| 199 | + new(DetectorPrototxtPath, DetectorCaffeModelPath, |
| 200 | + SuperResolutionPrototxtPath, SuperResolutionCaffeModelPath); |
| 201 | + |
| 202 | + private static void SkipIfModelFilesNotFound() |
| 203 | + { |
| 204 | + Assert.True(File.Exists(DetectorPrototxtPath), $"Model file not found: '{DetectorPrototxtPath}'"); |
| 205 | + Assert.True(File.Exists(DetectorCaffeModelPath), $"Model file not found: '{DetectorCaffeModelPath}'"); |
| 206 | + Assert.True(File.Exists(SuperResolutionPrototxtPath), $"Model file not found: '{SuperResolutionPrototxtPath}'"); |
| 207 | + Assert.True(File.Exists(SuperResolutionCaffeModelPath), $"Model file not found: '{SuperResolutionCaffeModelPath}'"); |
| 208 | + } |
34 | 209 | } |
0 commit comments