Generate image Verification Code

Source: Internet
Author: User
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

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.