diff --git a/Lazy.Captcha.Core/CaptchaOptions.cs b/Lazy.Captcha.Core/CaptchaOptions.cs index 12ad5ed3b819bcbd2c149eeebbddfa61e5c3e971..c4aefce51b3411815c8b0e291659430e94ecf394 100644 --- a/Lazy.Captcha.Core/CaptchaOptions.cs +++ b/Lazy.Captcha.Core/CaptchaOptions.cs @@ -28,7 +28,7 @@ namespace Lazy.Captcha.Core if (value.ContainsChinese()) { - this.ImageOption.FontFamily = DefaultFontFamilies.Instance.Kaiti; + this.ImageOption.FontFamily = DefaultFontFamilies.Kaiti; } _captchaType = value; diff --git a/Lazy.Captcha.Core/CaptchaService.cs b/Lazy.Captcha.Core/CaptchaService.cs index 9809d2bf70577fd2ec5e21afe39022733de250ce..c94ef660bfaf0ffeeb188025c07d8f26335518e6 100644 --- a/Lazy.Captcha.Core/CaptchaService.cs +++ b/Lazy.Captcha.Core/CaptchaService.cs @@ -34,7 +34,7 @@ namespace Lazy.Captcha.Core public virtual CaptchaData Generate(string captchaId, int? expirySeconds = null) { var _captchaCodeGenerator = GetCodeGenerator(); - var _captchaImageGenerator = GetImmageGenerator(); + var _captchaImageGenerator = GetImageGenerator(); var (renderText, code) = _captchaCodeGenerator.Generate(CaptchaOptions.CodeLength); var image = _captchaImageGenerator.Generate(renderText, CaptchaOptions.ImageOption); @@ -102,7 +102,7 @@ namespace Lazy.Captcha.Core } } - protected virtual ICaptchaImageGenerator GetImmageGenerator() + protected virtual ICaptchaImageGenerator GetImageGenerator() { return new DefaultCaptchaImageGenerator(); } diff --git a/Lazy.Captcha.Core/CaptchaServiceBuilder.cs b/Lazy.Captcha.Core/CaptchaServiceBuilder.cs index 8d807b470534f37fd97da41850de8fd49b7549a7..37636f9ce4bb0cd409db847d3ac129e8ddc9b3a2 100644 --- a/Lazy.Captcha.Core/CaptchaServiceBuilder.cs +++ b/Lazy.Captcha.Core/CaptchaServiceBuilder.cs @@ -35,7 +35,7 @@ namespace Lazy.Captcha.Core var options = new CaptchaOptions(); options.ImageOption.Width = 98; options.ImageOption.Height = 35; - options.ImageOption.ForegroundColors = DefaultColors.Instance.Colors; + options.ImageOption.ForegroundColors = DefaultColors.Colors; return options; } diff --git a/Lazy.Captcha.Core/CaptchaServiceCollectionExtensions.cs b/Lazy.Captcha.Core/CaptchaServiceCollectionExtensions.cs index 51437aee3b8834dc434b52d3436b1a20dc263b9d..718351d79c6ca124a559a219b2be303b71689ee4 100644 --- a/Lazy.Captcha.Core/CaptchaServiceCollectionExtensions.cs +++ b/Lazy.Captcha.Core/CaptchaServiceCollectionExtensions.cs @@ -34,7 +34,7 @@ namespace Microsoft.Extensions.DependencyInjection var fontFamily = configuration?.GetSection("CaptchaOptions:ImageOption:FontFamily")?.Value; if (!string.IsNullOrWhiteSpace(fontFamily)) { - options.ImageOption.FontFamily = DefaultFontFamilies.Instance.GetFontFamily(fontFamily); + options.ImageOption.FontFamily = DefaultFontFamilies.GetFontFamily(fontFamily); } var backgroundColor = configuration?.GetSection("CaptchaOptions:ImageOption:BackgroundColor")?.Value; @@ -62,9 +62,9 @@ namespace Microsoft.Extensions.DependencyInjection } } } - if (options.ImageOption.ForegroundColors == null || options.ImageOption.ForegroundColors.Count() == 0) + if (options.ImageOption.ForegroundColors == null || options.ImageOption.ForegroundColors.Count == 0) { - options.ImageOption.ForegroundColors = DefaultColors.Instance.Colors; + options.ImageOption.ForegroundColors = DefaultColors.Colors; } }); if (optionsAction != null) services.PostConfigure(optionsAction); diff --git a/Lazy.Captcha.Core/CaptchaType.cs b/Lazy.Captcha.Core/CaptchaType.cs index 9228795fccee38ced2e187f24fa0821950c94f26..a78ee35db4679739b98da7124dc65da87ab82b83 100644 --- a/Lazy.Captcha.Core/CaptchaType.cs +++ b/Lazy.Captcha.Core/CaptchaType.cs @@ -11,61 +11,61 @@ namespace Lazy.Captcha.Core.Generator /// /// 默认(英文字符大小写,数字混合) /// - DEFAULT=0, + DEFAULT = 0, /// /// 中文 /// - CHINESE=1, + CHINESE = 1, /// /// 数字 /// - NUMBER=2, + NUMBER = 2, /// /// 中文数字小写 /// - NUMBER_ZH_CN=3, + NUMBER_ZH_CN = 3, /// /// 中文数字大写 /// - NUMBER_ZH_HK=4, + NUMBER_ZH_HK = 4, /// /// 英文字符大小写混合 /// - WORD=5, + WORD = 5, /// /// 英文字符小写 /// - WORD_LOWER=6, + WORD_LOWER = 6, /// /// 英文字符大写 /// - WORD_UPPER=7, + WORD_UPPER = 7, /// /// 英文小写,数字混合 /// - WORD_NUMBER_LOWER=8, + WORD_NUMBER_LOWER = 8, /// /// 英文大写,数字混合 /// - WORD_NUMBER_UPPER=9, + WORD_NUMBER_UPPER = 9, /// /// 数字计算 /// - ARITHMETIC=10, + ARITHMETIC = 10, /// /// 数字计算,中文 /// - ARITHMETIC_ZH=11 + ARITHMETIC_ZH = 11 } } \ No newline at end of file diff --git a/Lazy.Captcha.Core/DefaultColors.cs b/Lazy.Captcha.Core/DefaultColors.cs index 0a0d8aa35ffced3c7656ce504b97dda28c2370df..1cf51993c6ac889a2d5ef4c881ede704932f428b 100644 --- a/Lazy.Captcha.Core/DefaultColors.cs +++ b/Lazy.Captcha.Core/DefaultColors.cs @@ -9,10 +9,10 @@ namespace Lazy.Captcha.Core { public class DefaultColors { - public static DefaultColors Instance = new DefaultColors(); + public static readonly DefaultColors Instance = new DefaultColors(); - public List Colors = new List - { + public static List Colors { get; private set; } = + [ SKColor.Parse("#0087ff"), SKColor.Parse("#339933"), SKColor.Parse("#ff6666"), @@ -24,7 +24,7 @@ namespace Lazy.Captcha.Core SKColor.Parse("#0066cc"), SKColor.Parse("#cc3333"), SKColor.Parse("#0099cc"), - SKColor.Parse("#003366"), - }; + SKColor.Parse("#003366") + ]; } } diff --git a/Lazy.Captcha.Core/DefaultFontFamilies.cs b/Lazy.Captcha.Core/DefaultFontFamilies.cs index e6bb12d71eb78082aac4f42ece36c7627c28c271..415eb87c883c15aa9651a74685a2cb2b0b449bff 100644 --- a/Lazy.Captcha.Core/DefaultFontFamilies.cs +++ b/Lazy.Captcha.Core/DefaultFontFamilies.cs @@ -11,7 +11,7 @@ namespace Lazy.Captcha.Core public static readonly DefaultFontFamilies Instance = new DefaultFontFamilies(); private static readonly List _fontFamilies = []; - private static readonly Dictionary FamilyNameMapper = new Dictionary + private static Dictionary FamilyNameMapper { get; set; } = new() { { "actionj", "Action Jackson" }, { "epilog", "Epilog" }, @@ -48,7 +48,7 @@ namespace Lazy.Captcha.Core /// /// 获取字体 /// - public SKTypeface GetFontFamily(string name) + public static SKTypeface GetFontFamily(string name) { var realName = "Epilog"; var normalizeName = name.ToLowerInvariant(); @@ -65,57 +65,57 @@ namespace Lazy.Captcha.Core /// /// ACTIONJ /// - public SKTypeface Actionj => GetFontFamily("Actionj"); + public static SKTypeface Actionj => GetFontFamily("Actionj"); /// /// Epilog /// - public SKTypeface Epilog => GetFontFamily("Epilog"); + public static SKTypeface Epilog => GetFontFamily("Epilog"); /// /// Fresnel /// - public SKTypeface Fresnel => GetFontFamily("Fresnel"); + public static SKTypeface Fresnel => GetFontFamily("Fresnel"); /// /// headache /// - public SKTypeface Headache => GetFontFamily("Headache"); + public static SKTypeface Headache => GetFontFamily("Headache"); /// /// Lexo /// - public SKTypeface Lexo => GetFontFamily("Lexo"); + public static SKTypeface Lexo => GetFontFamily("Lexo"); /// /// Prefix /// - public SKTypeface Prefix => GetFontFamily("Prefix"); + public static SKTypeface Prefix => GetFontFamily("Prefix"); /// /// Progbot /// - public SKTypeface Progbot => GetFontFamily("Progbot"); + public static SKTypeface Progbot => GetFontFamily("Progbot"); /// /// Ransom /// - public SKTypeface Ransom => GetFontFamily("Ransom"); + public static SKTypeface Ransom => GetFontFamily("Ransom"); /// /// Robot /// - public SKTypeface Robot => GetFontFamily("Robot"); + public static SKTypeface Robot => GetFontFamily("Robot"); /// /// Scandal /// - public SKTypeface Scandal => GetFontFamily("Scandal"); + public static SKTypeface Scandal => GetFontFamily("Scandal"); /// /// 楷体 /// - public SKTypeface Kaiti => GetFontFamily("Kaiti"); + public static SKTypeface Kaiti => GetFontFamily("Kaiti"); } [Obsolete("拼写错误。请使用 `DefaultFontFamilies` 代替。")] diff --git a/Lazy.Captcha.Core/Generator/Image/Option/CaptchaImageGeneratorOption.cs b/Lazy.Captcha.Core/Generator/Image/Option/CaptchaImageGeneratorOption.cs index 99bfd724c3361e3b6107355a41911d666c82f92c..234f056d9922bb608e7e5d5c9d9313f9ed4a12c2 100644 --- a/Lazy.Captcha.Core/Generator/Image/Option/CaptchaImageGeneratorOption.cs +++ b/Lazy.Captcha.Core/Generator/Image/Option/CaptchaImageGeneratorOption.cs @@ -24,7 +24,7 @@ namespace Lazy.Captcha.Core.Generator.Image.Option /// /// FontFamily /// - public SKTypeface FontFamily { get; set;}= DefaultFontFamilies.Instance.Kaiti; + public SKTypeface FontFamily { get; set;} = DefaultFontFamilies.Kaiti; /// /// 字体大小 /// diff --git a/README.md b/README.md index 7dbafd127b1271e44cf282e985ca7d1c18cd5351..978dfa59ee21a38c0e2cc977dd8de98c2c7cd3f5 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,7 @@ builder.Services.AddCaptcha(builder.Configuration); ##### 代码配置 -``` c# +``` csharp // 全部配置 builder.Services.AddCaptcha(builder.Configuration, option => option.CaptchaType = CaptchaType.WORD; // 验证码类型 @@ -157,7 +157,7 @@ builder.Services.AddCaptcha(builder.Configuration, option => option.ImageOption.InterferenceLineCount = 2; // 干扰线数量 option.ImageOption.FontSize = 36; // 字体大小 - option.ImageOption.FontFamily = DefaultFontFamilys.Instance.Actionj; // 字体 + option.ImageOption.FontFamily = DefaultFontFamilys.Actionj; // 字体 /* * 中文使用kaiti,其他字符可根据喜好设置(可能部分转字符会出现绘制不出的情况)。 @@ -171,7 +171,6 @@ builder.Services.AddCaptcha(builder.Configuration, option => #### 3. Controller ```csharp - [Route("captcha")] [ApiController] public class CaptchaController : Controller @@ -228,7 +227,7 @@ public class CaptchaController : Controller ##### 配置文件方式 -```json +```json5 { "CaptchaOptions": { "RateLimit": { @@ -387,12 +386,12 @@ public class RandomCaptcha : DefaultCaptcha // 如果包含中文时,使用kaiti字体,否则文字乱码 if (options.CaptchaType.ContainsChinese()) { - options.ImageOption.FontFamily = DefaultFontFamilys.Instance.Kaiti; + options.ImageOption.FontFamily = DefaultFontFamilys.Kaiti; options.ImageOption.FontSize = 24; } else { - options.ImageOption.FontFamily = DefaultFontFamilys.Instance.Actionj; + options.ImageOption.FontFamily = DefaultFontFamilys.Actionj; } // 动静随机 @@ -459,7 +458,7 @@ public class ResourceFontFamilysFinder ``` #### 4. 设置option -``` c# +``` csharp // 内存存储, 基于appsettings.json配置 builder.Services.AddCaptcha(builder.Configuration, options => { @@ -476,7 +475,7 @@ builder.Services.AddCaptcha(builder.Configuration, options => 先安装SkiaSharp, 再安装Lazy.Captcha.Core #### 2. Global.asax增加 -``` c# +``` csharp public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() @@ -496,7 +495,7 @@ public class MvcApplication : System.Web.HttpApplication .Height(35) .FontSize(20) .CaptchaType(CaptchaType.ARITHMETIC) - .FontFamily(DefaultFontFamilys.Instance.Ransom) + .FontFamily(DefaultFontFamilys.Ransom) .InterferenceLineCount(3) .Animation(false) .Build(); @@ -506,7 +505,7 @@ public class MvcApplication : System.Web.HttpApplication ``` #### 3. Controller使用 -``` c# +``` csharp public class CaptchaController : Controller { [HttpGet] @@ -570,7 +569,7 @@ var imageGenerator = new DefaultCaptchaImageGenerator(); var imageGeneratorOption = new CaptchaImageGeneratorOption() { // 必须设置 - ForegroundColors = DefaultColors.Instance.Colors + ForegroundColors = DefaultColors.Colors }; var bytes = imageGenerator.Generate("hello", imageGeneratorOption); ``` diff --git a/Sample.MvcFramework/Global.asax.cs b/Sample.MvcFramework/Global.asax.cs index 7034b72671e411802e9fe9ed97d79023a429c8d6..0e87994391df576035eb462949c2e56915d7eeec 100644 --- a/Sample.MvcFramework/Global.asax.cs +++ b/Sample.MvcFramework/Global.asax.cs @@ -31,7 +31,7 @@ namespace Sample.MvcFramework .Height(35) .FontSize(20) .CaptchaType(CaptchaType.ARITHMETIC) - .FontFamily(DefaultFontFamilies.Instance.Ransom) + .FontFamily(DefaultFontFamilies.Ransom) .InterferenceLineCount(3) .Animation(false) .Build(); diff --git a/Sample.NetCore/Controllers/CaptchaController.cs b/Sample.NetCore/Controllers/CaptchaController.cs index decd6889d3cca7d3d0cc70ccdaf9fc098c6731e9..15970b84c7f312bf8cee5e48bdfa88f2b1cc251f 100644 --- a/Sample.NetCore/Controllers/CaptchaController.cs +++ b/Sample.NetCore/Controllers/CaptchaController.cs @@ -131,7 +131,7 @@ namespace Sample.NetCore.Controllers .Height(35) .FontSize(26) .CaptchaType(Enum.Parse(type)) - .FontFamily(DefaultFontFamilies.Instance.GetFontFamily(font)) + .FontFamily(DefaultFontFamilies.GetFontFamily(font)) .InterferenceLineCount(2) .Animation(false) .TextBold(textBold) @@ -153,7 +153,7 @@ namespace Sample.NetCore.Controllers var imageGeneratorOption = new CaptchaImageGeneratorOption() { // 字体颜色 - ForegroundColors = DefaultColors.Instance.Colors + ForegroundColors = DefaultColors.Colors }; var bytes = imageGenerator.Generate("hello", imageGeneratorOption); var stream = new MemoryStream(bytes); diff --git a/Sample.NetCore/RandomCaptcha.cs b/Sample.NetCore/RandomCaptcha.cs index 91ff3046151d927b370c331533d8df9f5d9a963b..ecb6bd68f5bd0110ae809377f91e0b3235f277ff 100644 --- a/Sample.NetCore/RandomCaptcha.cs +++ b/Sample.NetCore/RandomCaptcha.cs @@ -39,12 +39,12 @@ namespace Sample.NetCore // 如果包含中文时,使用kaiti字体,否则文字乱码 if (options.CaptchaType.ContainsChinese()) { - options.ImageOption.FontFamily = DefaultFontFamilies.Instance.Kaiti; + options.ImageOption.FontFamily = DefaultFontFamilies.Kaiti; options.ImageOption.FontSize = 24; } else { - options.ImageOption.FontFamily = DefaultFontFamilies.Instance.Actionj; + options.ImageOption.FontFamily = DefaultFontFamilies.Actionj; } // 动静随机 diff --git a/Sample.Winfrom/OptionProviders/FontFamilyOptionProvider.cs b/Sample.Winfrom/OptionProviders/FontFamilyOptionProvider.cs index c883fd19801441da66f458d826943602d62edbe9..7db7606b8753c2a9eee146463909cb83353a572a 100644 --- a/Sample.Winfrom/OptionProviders/FontFamilyOptionProvider.cs +++ b/Sample.Winfrom/OptionProviders/FontFamilyOptionProvider.cs @@ -14,17 +14,17 @@ namespace Sample.Winfrom.OptionProviders { return new List { - new FontFamilyOption("Actionj", DefaultFontFamilies.Instance.Actionj), - new FontFamilyOption("Kaiti", DefaultFontFamilies.Instance.Kaiti), - new FontFamilyOption("Fresnel", DefaultFontFamilies.Instance.Fresnel), - new FontFamilyOption("Prefix", DefaultFontFamilies.Instance.Prefix), - new FontFamilyOption("Ransom", DefaultFontFamilies.Instance.Ransom), - new FontFamilyOption("Scandal", DefaultFontFamilies.Instance.Scandal), - new FontFamilyOption("Epilog", DefaultFontFamilies.Instance.Epilog), - new FontFamilyOption("Headache", DefaultFontFamilies.Instance.Headache), - new FontFamilyOption("Lexo", DefaultFontFamilies.Instance.Lexo), - new FontFamilyOption("Progbot", DefaultFontFamilies.Instance.Progbot), - new FontFamilyOption("Robot", DefaultFontFamilies.Instance.Robot), + new FontFamilyOption("Actionj", DefaultFontFamilies.Actionj), + new FontFamilyOption("Kaiti", DefaultFontFamilies.Kaiti), + new FontFamilyOption("Fresnel", DefaultFontFamilies.Fresnel), + new FontFamilyOption("Prefix", DefaultFontFamilies.Prefix), + new FontFamilyOption("Ransom", DefaultFontFamilies.Ransom), + new FontFamilyOption("Scandal", DefaultFontFamilies.Scandal), + new FontFamilyOption("Epilog", DefaultFontFamilies.Epilog), + new FontFamilyOption("Headache", DefaultFontFamilies.Headache), + new FontFamilyOption("Lexo", DefaultFontFamilies.Lexo), + new FontFamilyOption("Progbot", DefaultFontFamilies.Progbot), + new FontFamilyOption("Robot", DefaultFontFamilies.Robot), }; } }