Generate verification code in picture format

From , 5 Years ago, written in Java, viewed 133 times.
URL https://pastebin.vip/view/49cbb759
  1. package photo;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.image.BufferedImage;
  7. import java.io.ByteArrayInputStream;
  8. import java.io.ByteArrayOutputStream;
  9. import java.io.InputStream;
  10. import java.util.Random;
  11.  
  12. import com.sun.image.codec.jpeg.JPEGCodec;
  13. import com.sun.image.codec.jpeg.JPEGImageEncoder;
  14.  
  15.  
  16. public class ImageCodeAction {
  17.         private InputStream input;
  18.  
  19.         private static int WIDTH = 300;
  20.  
  21.         private static int HEIGHT = 100;
  22.  
  23.         private static int NUM = 4;
  24.  
  25.         private static char[] seq = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
  26.                         'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  27.                         'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8',
  28.                         '9' };
  29.  
  30.         public String execute() {
  31.                 byte[] image = randomImage();
  32.                 input = new ByteArrayInputStream(image);
  33.                 return "success";
  34.         }
  35.  
  36.         /**
  37.          *
  38.          * @return
  39.          */
  40.         private byte[] randomImage() {
  41.                 Random r = new Random();
  42.  
  43.                 // 图片的内存映像
  44.                 BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
  45.                                 BufferedImage.TYPE_INT_RGB);
  46.  
  47.                 // 获得画笔对象
  48.                 Graphics g = image.getGraphics();
  49.                 g.setColor(randomColor(r));
  50.                 g.fillRect(0, 0, WIDTH, HEIGHT);
  51.                 g.setColor(new Color(0, 0, 0));
  52.  
  53.                 // 用于存储随机生成的验证码
  54.                 StringBuffer number = new StringBuffer();
  55.  
  56.                 // 绘制验证码
  57.                 for (int i = 0; i < NUM; i++) {
  58.                         g.setColor(randomColor(r));
  59.                         int h = (int) ((HEIGHT * 60 / 100) * r.nextDouble() + (HEIGHT * 30 / 100));
  60.                         g.setFont(new Font(null, Font.BOLD | Font.ITALIC, h));
  61.                         String ch = String.valueOf(seq[r.nextInt(seq.length)]);
  62.                         number.append(ch);
  63.                         g.drawString(ch, i * WIDTH / NUM * 90 / 100, h);
  64.                 }
  65.  
  66.                 session.put("code", number.toString());
  67.                 System.out.println(number.toString());
  68.  
  69.                 // 绘制干扰线
  70.                 for (int i = 0; i <= 12; i++) {
  71.                         g.setColor(randomColor(r));
  72.                         g.drawLine(r.nextInt(WIDTH), r.nextInt(HEIGHT), r.nextInt(WIDTH), r
  73.                                         .nextInt(HEIGHT));
  74.  
  75.                 }
  76.  
  77.                 ByteArrayOutputStream os = new ByteArrayOutputStream();
  78.  
  79.                 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
  80.  
  81.                 // 把BufferedImage对象中的图像信息编码后
  82.                 // 向创建该对象(encoder)时指定的输出流输出
  83.                 try {
  84.                         encoder.encode(image);
  85.                         return os.toByteArray();
  86.                 } catch (Exception e) {
  87.                         throw new RuntimeException(e);
  88.                 }
  89.         }
  90.  
  91.         private Color randomColor(Random r) {
  92.                 return new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
  93.         }
  94.  
  95.         public InputStream getInput() {
  96.                 return input;
  97.         }
  98.  
  99.         public void setInput(InputStream input) {
  100.                 this.input = input;
  101.         }
  102.  
  103. }
  104. //java/1306

Reply to "Generate verification code in picture format"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website