【C#】三维立体验证码 (3DCaptcha)
本技术来源:
3DCaptcha http://www-personal.umich.edu/~mressl/3dcaptcha/
下载地址 http://code.google.com/p/3dcaptcha/
官方LOGO
本例完全由此样例程序翻译而来,未作任何附加处理.
以下是C#对此算法的实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | /* * 3DCaptcha for .net * * http://www-personal.umich.edu/~mressl/3dcaptcha/ (php) * * Translate : Aimeast * Blog : http://www.cnblogs.com/Aimeast/ */ using System; using System.Drawing; namespace Captcha { public static class Captcha { private static double [] addVector( double [] a, double [] b) { return new double [] { a[0] + b[0], a[1] + b[1], a[2] + b[2] }; } private static double [] scalarProduct( double [] vector, double scalar) { return new double [] { vector[0] * scalar, vector[1] * scalar, vector[2] * scalar }; } private static double dotProduct( double [] a, double [] b) { return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; } private static double norm( double [] vector) { return Math.Sqrt(dotProduct(vector, vector)); } private static double [] normalize( double [] vector) { return scalarProduct(vector, 1.0 / norm(vector)); } private static double [] crossProduct( double [] a, double [] b) { return new double [] { (a[1] * b[2] - a[2] * b[1]), (a[2] * b[0] - a[0] * b[2]), (a[0] * b[1] - a[1] * b[0]) }; } private static double [] vectorProductIndexed( double [] v, double [] m, int i) { return new double []{ v[i + 0] * m[0] + v[i + 1] * m[4] + v[i + 2] * m[8] + v[i + 3] * m[12], v[i + 0] * m[1] + v[i + 1] * m[5] + v[i + 2] * m[9] + v[i + 3] * m[13], v[i + 0] * m[2] + v[i + 1] * m[6] + v[i + 2] * m[10]+ v[i + 3] * m[14], v[i + 0] * m[3] + v[i + 1] * m[7] + v[i + 2] * m[11]+ v[i + 3] * m[15] }; } private static double [] vectorProduct( double [] v, double [] m) { return vectorProductIndexed(v, m, 0); } private static double [] matrixProduct( double [] a, double [] b) { double [] o1 = vectorProductIndexed(a, b, 0); double [] o2 = vectorProductIndexed(a, b, 4); double [] o3 = vectorProductIndexed(a, b, 8); double [] o4 = vectorProductIndexed(a, b, 12); return new double []{ o1[0], o1[1], o1[2], o1[3], o2[0], o2[1], o2[2], o2[3], o3[0], o3[1], o3[2], o3[3], o4[0], o4[1], o4[2], o4[3] }; } private static double [] cameraTransform( double [] C, double [] A) { double [] w = normalize(addVector(C, scalarProduct(A, -1))); double [] y = new double [] { 0, 1, 0 }; double [] u = normalize(crossProduct(y, w)); double [] v = crossProduct(w, u); double [] t = scalarProduct(C, -1); return new double []{ u[0], v[0], w[0], 0, u[1], v[1], w[1], 0, u[2], v[2], w[2], 0, dotProduct(u, t), dotProduct(v, t), dotProduct(w, t), 1 }; } private static double [] viewingTransform( double fov, double n, double f) { fov *= (Math.PI / 180); double cot = 1.0 / Math.Tan(fov / 2); return new double []{ cot, 0, 0, 0, 0, cot, 0, 0, 0, 0, (f + n) / (f - n), -1, 0, 0, 2 * f * n / (f - n), 0 }; } public static Image Generate( string captchaText) { Random rnd = new Random(); // 3dcha parameters int fontsize = 24; Font font = new Font( "Arial" , fontsize); SizeF sizeF; using (Graphics g = Graphics.FromImage( new Bitmap(1, 1))) { sizeF = g.MeasureString(captchaText, font, 0, StringFormat.GenericDefault); } int image2d_x = ( int )sizeF.Width; int image2d_y = ( int )(fontsize * 1.3); double bevel = 4; // Create 2d image Bitmap image2d = new Bitmap(image2d_x, image2d_y); Color black = Color.Black; Color white = Color.White; // Paint 2d image using (Graphics g = Graphics.FromImage(image2d)) { g.Clear(black); g.DrawString(captchaText, font, Brushes.White, 0, 0); } // Calculate projection matrix double [] T = cameraTransform( new double [] { rnd.Next(-90, 90), -200, rnd.Next(150, 250) }, new double [] { 0, 0, 0 } ); T = matrixProduct( T, viewingTransform(60, 300, 3000) ); // Calculate coordinates double [][] coord = new double [image2d_x * image2d_y][]; int count = 0; for ( int y = 0; y < image2d_y; y += 2) { for ( int x = 0; x < image2d_x; x++) { // calculate x1, y1, x2, y2 int xc = x - image2d_x / 2; int zc = y - image2d_y / 2; double yc = -( double )(image2d.GetPixel(x, y).ToArgb() & 0xff) / 256 * bevel; double [] xyz = new double [] { xc, yc, zc, 1 }; xyz = vectorProduct(xyz, T); coord[count] = xyz; count++; } } // Create 3d image int image3d_x = 256; //$image3d_y = $image3d_x / 1.618; int image3d_y = image3d_x * 9 / 16; Bitmap image3d = new Bitmap(image3d_x, image3d_y); Color fgcolor = Color.White; Color bgcolor = Color.Black; using (Graphics g = Graphics.FromImage(image3d)) { g.Clear(bgcolor); count = 0; double scale = 1.75 - ( double )image2d_x / 400; for ( int y = 0; y < image2d_y; y += 2) { for ( int x = 0; x < image2d_x; x++) { if (x > 0) { double x0 = coord[count - 1][0] * scale + image3d_x / 2; double y0 = coord[count - 1][1] * scale + image3d_y / 2; double x1 = coord[count][0] * scale + image3d_x / 2; double y1 = coord[count][1] * scale + image3d_y / 2; g.DrawLine( new Pen(fgcolor), ( float )x0, ( float )y0, ( float )x1, ( float )y1); } count++; } } } return image3d; } } } |
使用方法
1 | this .BackgroundImage = Captcha.Generate( "Code" ); |
效果截图
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)