zoukankan      html  css  js  c++  java
  • Java画出验证码图片

    package com;
    
    import javax.imageio.ImageIO;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.Random;
    //验证码
    public class Imageyan {
        public static void main(String[] args) throws IOException {
            int id = 4;
            int w = id*50;
            int h = 80;
            BufferedImage buf = new BufferedImage(w,h,1);
            Graphics g = buf.getGraphics();
            g.setColor(Color.WHITE);
            g.fillRect(0,0,w,h);
            Random ran = new Random();
            //设置字
            Font font = new Font("宋体",Font.PLAIN,40);
            g.setFont(font);
            for(int i = 0;i < id;i++) {
                Color color = new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255),255);
                g.setColor(color);
                String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
                String str = "";
                str = chars.charAt(ran.nextInt(chars.length()))+"";
                int x = 20 + i * 45;
                int y = 50;
                g.drawString(str, x, y);
            }
            //添加干扰文字
            for(int i = 0;i < id;i++) {
                Color color = new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255),100);
                g.setColor(color);
                String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
                String str = "";
                str = chars.charAt(ran.nextInt(chars.length()))+"";
                g.drawString(str, ran.nextInt(w),ran.nextInt(h));
            }
            //添加线条
            for (int a = 0; a < 30; a++) {
                Color color = new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255),ran.nextInt(255));
                g.setColor(color);
                g.drawLine(ran.nextInt(w),ran.nextInt(w),ran.nextInt(w),ran.nextInt(w));
            }
            //添加弧线
            for (int a = 0; a < 30; a++) {
                Color color = new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255),ran.nextInt(255));
                g.setColor(color);
                g.drawArc(ran.nextInt(w),ran.nextInt(w),ran.nextInt(w),ran.nextInt(w),ran.nextInt(360),ran.nextInt(360));
            }
    
            g.dispose();
            ImageIO.write(buf,"jpg",new File("d:/img/five.jpg"));
        }
    }
  • 相关阅读:
    浅析C#中的套接字编程
    在 C# 中通过 P/Invoke 调用Win32 DLL
    读书笔记c#高级编程 委托和事件
    如何将 .net framework 打包进 msi安装包,使得安装时自动安装
    自实现input上传指定文件到服务器
    Thrift初探:简单实现C#通讯服务程序
    C# 使用NLog记录日志
    C# winform程序怎么打包成安装项目(图解)
    VUE3.0+Vant VS Code入门教程
    WCF入门教程2——创建第一个WCF程序
  • 原文地址:https://www.cnblogs.com/zxwen/p/9526446.html
Copyright © 2011-2022 走看看