java实现生成各种图片验证码(算术及中文)

import com.wf.captcha.*;
import com.wf.captcha.base.Captcha;

import java.io.FileOutputStream;

/**
 * Created by zfwy on 2020/11/2.
 * 验证码
 */
public class Demo01 {
    public static void main(String[] args)throws Exception {
        String path = "D:\\Music\\";
        //png图
        SpecCaptcha captcha = new SpecCaptcha(130, 40);
        FileOutputStream outputStream = new FileOutputStream(path + "0png类型.png");
        System.out.println(captcha.text());
        captcha.out(outputStream);
        //gif动图
        GifCaptcha gifCaptcha = new GifCaptcha(130, 40);
        FileOutputStream outputStream1 = new FileOutputStream(path + "1gif类型.gif");
        //设置纯大写字母验证码TYPE_ONLY_UPPER
        gifCaptcha.setCharType(Captcha.TYPE_ONLY_UPPER);
        //设置字体
        gifCaptcha.setFont(Captcha.FONT_5);
        System.out.println(gifCaptcha.text());
        gifCaptcha.out(outputStream1);
        //中文类型
        ChineseCaptcha chineseCaptcha = new ChineseCaptcha(130, 40);
        FileOutputStream outputStream2 = new FileOutputStream(path + "2中文类型.png");
        System.out.println(chineseCaptcha.text()); chineseCaptcha.out(outputStream2);
        //中文类型gif动图
        ChineseGifCaptcha chineseGifCaptcha = new ChineseGifCaptcha(130, 40);
        FileOutputStream outputStream3 = new FileOutputStream(path + "3中文类型动图.gif");
        System.out.println(chineseGifCaptcha.text()); chineseGifCaptcha.out(outputStream3);
        //算术类型
        ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(180, 40);
        FileOutputStream outputStream4 = new FileOutputStream(path + "4算术类型.png");
        //设置几位数的运算,几个数计算
        arithmeticCaptcha.setLen(3);
        //打印算式
        System.out.println(arithmeticCaptcha.getArithmeticString());
        //打印算式的结果
        System.out.println(arithmeticCaptcha.text());
        arithmeticCaptcha.out(outputStream4);
    }
}
导入以下依赖
  <!--图片验证码-->
        <dependency>
            <groupId>com.github.whvcse</groupId>
            <artifactId>easy-captcha</artifactId>
            <version>1.6.2</version>
        </dependency>
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java生成随机短信验证码,可以使用以下代码: ```java import java.util.Random; public class SmsCodeGenerator { public static void main(String[] args) { int codeLength = 6; // 验证码的长度 String code = generateCode(codeLength); System.out.println("随机生成的短信验证码:" + code); } public static String generateCode(int length) { // 验证码字符集 String charset = "0123456789"; // 创建随机数生成器 Random random = new Random(); // 生成指定长度的验证码 StringBuilder codeBuilder = new StringBuilder(); for (int i = 0; i < length; i++) { int randomIndex = random.nextInt(charset.length()); char randomChar = charset.charAt(randomIndex); codeBuilder.append(randomChar); } return codeBuilder.toString(); } } ``` 上述代码中,我们首先定义了验证码的长度 `codeLength`,这里设置为 6。 然后,我们使用 `generateCode()` 方法生成指定长度的随机验证码。在该方法中,我们定义了验证码字符集 `charset`,其中包含了数字 0 到 9。 接下来,我们创建了一个随机数生成器 `random`,使用 `random.nextInt(charset.length())` 生成一个随机索引,然后从字符集中取出对应索引的字符,循环生成指定长度的验证码。 最后,我们将生成的验证码以字符串形式返回。 你可以在运行代码后,查看控制台输出的随机生成的短信验证码。 希望这个示例对你有帮助!如果你有任何疑问,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子非我鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值