[C #] three-dimensional Verification Code (3 dcaptcha)

Source: Internet
Author: User

Source:

3 dcaptchaHttp://www-personal.umich.edu /~ Mressl/3 dcaptcha/

Http://code.google.com/p/3dcaptcha/

Official logo

 

This example is complete.ProgramTranslation, no additional processing.

C #AlgorithmImplementation

/*** Dcaptcha for. Net ** http://www-personal.umich.edu /~ Mressl/3 dcaptcha/(PHP) * http://code.google.com/p/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 ));} // http://en.wikipedia.org/wiki/Cross_product 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]};} // http://graphics.idav.ucdavis.edu/education/GraphicsNotes/Camera-Transform/Camera-Transform.html Private Static double [] cameratransform (double [] C, double []) {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 };}// http://graphics.idav.ucdavis.edu/education/GraphicsNotes/Viewing-Transformation/Viewing-Transformation.html 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, 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 (); // 3 dcha parameters int fontsize = 24; font = new font ("Arial", fontsize); 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,300 0 )); // 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 ;}}}

Usage

 
This. backgroundimage = CAPTCHA. Generate ("Code ");

Effect

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.