1 package util; 2 3 Import javax. imageIO. imageIO; 4 Import Java. AWT. *; 5 import Java. AWT. image. bufferedimage; 6 Import Java. io. file; 7 Import Java. io. ioexception; 8 Import Java. util. random; 9 10 public class image {11 public static int [] ran () {12 // set the image width and height 13 int width = 150; 14 int Height = 60; 15 // Number of interfering lines 16 int lines = 10; 17 // Verification Code array 18 int [] Random = new int [4]; 19 random r = new random (); 20 Buffer Edimage B = new bufferedimage (width, height, bufferedimage. type_int_rgb); 21 graphics G = B. getgraphics (); 22g. setfont (new font ("", Font. bold, 30); 23 for (INT I = 0; I <4; I ++) {24 int number = R. nextint (10); 25 random [I] = number; 26 int y = 10 + R. nextint (40); // 10 ~ An integer in the range of 40, as Y coordinate 27 // random color, RGB mode 28 color C = new color (R. nextint (1, 255), R. nextint (1, 255), R. nextint (255); 29G. setcolor (c); 30 // G. drawstring ("" + A, 5 + I * width/4, Y); 31 // write the verification code 32G. drawstring (integer. tostring (number), 5 + I * width/4, Y); 33} 34 for (INT I = 0; I <lines; I ++) {35 // set the interference line color 36 color C = new color (R. nextint (1, 255), R. nextint (1, 255), R. nextint (255); 37g. setcolor (c); 38g. drawline (R. nextint (width), R. nextint (height), R. nextint (width), R. nextint (height); 39} 40 try {41 // clear buffer 42g. dispose (); 43 // write 44 ImageIO to the file. write (B, "jpg", new file ("E: \ intellij idea \ mail \ WEB \ imagecode \ test.jpg ")); 45} catch (ioexception e) {46 E. printstacktrace (); 47} 48 return random; 49} 50 51 // test 52 public static void main (string [] ARGs) {53 ran (); 54} 55}
Generate image Verification Code