public static void InitOption(VerificationCodeOption _option)
 {
     if (_option != null)
     {
         lock (typeof(VerificationCodeFactory))
         {
             Option = _option;
         }
     }
 }
Exemple #2
0
        public static IApplicationBuilder UseVerificationCode(this IApplicationBuilder builder, Action <VerificationCodeOption> optionSet)
        {
            if (optionSet == null)
            {
                throw new Exception("VerificationCodeOption 不能为空");
            }
            var option = new VerificationCodeOption();

            optionSet(option);
            VerificationCodeFactory.InitOption(option);
            return(builder.UseMiddleware <VerificationCodeMiddleware>());
        }
        private static IVerificationCodeProvider FilterProvider(object objectType, VerificationCodeOption option)
        {
            int len  = providerList.Count;
            var list = providerList.Values.ToList();;

            for (int i = len - 1; i >= 0; i--)
            {
                var provider = list[i](option);
                if (provider.CanHandle(objectType))
                {
                    return(provider);
                }
            }
            return(new DefaultVerificationCodeProvider(option));
        }
Exemple #4
0
        public ICodeModel CreateImage(List <string> keys, VerificationCodeOption option)
        {
            var imageModel = new DefaultCodeModel();

            if (keys?.Any() == true)
            {
                if (option == null)
                {
                    throw new Exception("请先初始化配置");
                }
                Bitmap       Img         = null;
                Graphics     g           = null;
                MemoryStream ms          = null;
                Image        imageStream = null;
                Random       random      = new Random();
                try
                {
                    var fileList   = Directory.GetFiles(option.ImagePath);
                    int imageCount = fileList.Length;
                    if (imageCount == 0)
                    {
                        throw new Exception("背景图片不能为空");
                    }
                    int    imageRandom = random.Next(1, imageCount);
                    string randomfile  = fileList[imageRandom];
                    imageStream = Image.FromFile(randomfile);
                    Img         = new Bitmap(imageStream, option.ImageWidth, option.ImageHeight);
                    g           = Graphics.FromImage(Img);
                    int codelength = keys.Count;
                    int checkQty   = random.Next(option.DeafaultCheckQty, codelength);
                    for (int i = 0; i < codelength; i++)
                    {
                        int   cindex = random.Next(option.PenColor.Count);
                        int   findex = random.Next(option.FontFamilyName.Count);
                        Font  f      = new Font(option.FontFamilyName[findex], option.FontSize, FontStyle.Bold);
                        Brush b      = new SolidBrush(option.PenColor[cindex]);
                        int   y      = random.Next(option.ImageHeight);
                        if (y > (option.ImageHeight - 30))
                        {
                            y = y - 60;
                        }

                        int x = option.ImageWidth / (i + 1);
                        if ((option.ImageWidth - x) < 50)
                        {
                            x = option.ImageWidth - 60;
                        }
                        string word = keys[i];
                        if (imageModel.CodeList.Count < checkQty)
                        {
                            imageModel.CodeList.Add(new CodePoint()
                            {
                                Word = word,
                                X    = x,
                                Y    = y
                            });
                        }
                        g.DrawString(word, f, b, x, y);
                    }
                    ms = new MemoryStream();
                    Img.Save(ms, ImageFormat.Jpeg);
                    imageModel.ImageBase64String = "data:image/jpg;base64," + Convert.ToBase64String(ms.GetBuffer());
                }
                finally
                {
                    imageStream.Dispose();
                    g.Dispose();
                    Img.Dispose();
                    ms.Dispose();
                }
            }
            return(imageModel);
        }
 public DefaultVerificationCodeProvider(VerificationCodeOption _option)
 {
     option           = _option;
     CodeGenerate     = new DefaultCodeGenerate(_option);
     VerificationCode = new DeafultVerificationCode();
 }
        public ICodeModel CreateImage(List <string> keys, VerificationCodeOption option)
        {
            var imageModel = new TextCodeModel {
                Wrods = keys
            };

            if (keys?.Any() == true)
            {
                if (option == null)
                {
                    throw new Exception("请先初始化配置");
                }
                Bitmap       Img    = null;
                Graphics     g      = null;
                MemoryStream ms     = null;
                Random       random = new Random();
                try
                {
                    Img = new Bitmap(option.ImageWidth, option.ImageHeight);
                    g   = Graphics.FromImage(Img);
                    g.Clear(Color.White);

                    int lineCount = random.Next(3, 6);
                    for (int i = 0; i < lineCount; i++)
                    {
                        var     r1   = random.Next(3, 6);
                        Point[] pArr = new Point[r1];

                        var lineWd = Img.Width / r1;
                        for (int j = 0; j < r1; j++)
                        {
                            pArr[j].X = (lineWd * j) + 10;
                            pArr[j].Y = random.Next(Img.Height);
                        }
                        var cindex = random.Next(option.PenColor.Count);
                        var p      = new Pen(option.PenColor[cindex], 2.5f);
                        g.DrawCurve(p, pArr, 0.5F);
                    }

                    int codelength = keys.Count;
                    var wd         = option.ImageWidth / codelength;
                    for (int i = 0; i < codelength; i++)
                    {
                        int    cindex = random.Next(option.PenColor.Count);
                        int    findex = random.Next(option.FontFamilyName.Count);
                        Font   f      = new Font(option.FontFamilyName[findex], option.FontSize, FontStyle.Bold);
                        Brush  b      = new SolidBrush(option.PenColor[cindex]);
                        int    y      = random.Next(option.ImageHeight - f.Height);
                        int    wx     = wd * (i + 1) > (option.ImageWidth - wd) ? (option.ImageWidth - wd) : wd * (i + 1);
                        int    x      = random.Next(wd * i, wx);
                        string word   = keys[i];
                        g.DrawString(word, f, b, x, y);
                    }
                    g.DrawRectangle(new Pen(Color.Black), 0, 0, Img.Width - 1, Img.Height - 1);
                    ms = new MemoryStream();
                    Img.Save(ms, ImageFormat.Jpeg);
                    imageModel.ImageBase64String = "data:image/jpg;base64," + Convert.ToBase64String(ms.GetBuffer());
                }
                finally
                {
                    g.Dispose();
                    Img.Dispose();
                    ms.Dispose();
                }
            }
            return(imageModel);
        }
Exemple #7
0
 public DefaultCodeGenerate(VerificationCodeOption _option)
 {
     option = _option;
 }