Android 获取随机验证码功能示例
作者:左岸-- 时间:2023-01-22 10:03:27
验证码功能在各大网站都能用到,下面小编通过实例代码给大家分享Android 获取随机验证码功能,具体代码如下所示:
package cn.hk.image;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
public class VerifyCode {
private int w = 70;
private int h = 35;
private Random r = new Random();
// {"宋体", "华文楷体", "黑体", "华文新魏", "华文隶书", "微软雅黑", "楷体_GB2312"}
private String[] fontNames = {"宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312"};
// 可选字符
private String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";
// 背景色
private Color bgColor = new Color(255, 255, 255);
// 验证码上的文本
private String text ;
// 生成随机的颜色
private Color randomColor () {
int red = r.nextInt(150);
int green = r.nextInt(150);
int blue = r.nextInt(150);
return new Color(red, green, blue);
}
// 生成随机的字体
private Font randomFont () {
int index = r.nextInt(fontNames.length);
String fontName = fontNames[index];//生成随机的字体名称
int style = r.nextInt(4);//生成随机的样式, 0(无样式), 1(粗体), 2(斜体), 3(粗体+斜体)
int size = r.nextInt(5) + 24; //生成随机字号, 24 ~ 28
return new Font(fontName, style, size);
}
// 画干扰线
private void drawLine (BufferedImage image) {
int num = 3;//一共画3条
Graphics2D g2 = (Graphics2D)image.getGraphics();
for(int i = 0; i < num; i++) {//生成两个点的坐标,即4个值
int x1 = r.nextInt(w);
int y1 = r.nextInt(h);
int x2 = r.nextInt(w);
int y2 = r.nextInt(h);
g2.setStroke(new BasicStroke(1.5F));
g2.setColor(Color.BLUE); //干扰线是蓝色
g2.drawLine(x1, y1, x2, y2);//画线
}
}
// 随机生成一个字符
private char randomChar () {
int index = r.nextInt(codes.length());
return codes.charAt(index);
}
// 创建BufferedImage,图片缓存区
private BufferedImage createImage () {
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)image.getGraphics();
g2.setColor(this.bgColor);
g2.fillRect(0, 0, w, h);
return image;
}
// 调用这个方法得到验证码
public BufferedImage getImage () {
BufferedImage image = createImage();//创建图片缓冲区
Graphics2D g2 = (Graphics2D)image.getGraphics();//得到绘制环境
StringBuilder sb = new StringBuilder();//用来装载生成的验证码文本
// 向图片中画4个字符
for(int i = 0; i < 4; i++) {//循环四次,每次生成一个字符
String s = randomChar() + "";//随机生成一个字母
sb.append(s); //把字母添加到sb中
float x = i * 1.0F * w / 4; //设置当前字符的x轴坐标
g2.setFont(randomFont()); //设置随机字体
g2.setColor(randomColor()); //设置随机颜色
g2.drawString(s, x, h-5); //画图
}
this.text = sb.toString(); //把生成的字符串赋给了this.text
drawLine(image); //添加干扰线
return image;
}
// 返回验证码图片上的文本
public String getText () {
return text;
}
// 保存图片到指定的输出流
public static void output (BufferedImage image, OutputStream out)
throws IOException {
ImageIO.write(image, "JPEG", out);
}
}
以上所述是小编给大家介绍的Android 获取随机验证码功能示例网站的支持!
来源:http://blog.csdn.net/zuoan666/article/details/73774016
标签:android,随机,验证码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实战之用springboot+netty实现简单的一对一聊天
2023-12-03 07:28:19
![](https://img.aspxhome.com/file/2023/0/61080_0s.png)
聊聊Spring Cloud Gateway过滤器精确控制异常返回问题
2022-06-23 01:04:14
![](https://img.aspxhome.com/file/2023/5/108215_0s.png)
基于C#实现简单离线注册码生成与验证
2022-02-08 15:22:49
通过特性(attribute)为枚举添加更多信息示例
2023-10-10 09:22:06
Java多线程死锁与资源限制操作
2023-08-31 05:54:30
C#窗体间通讯的几种常用处理方法总结
2021-10-26 05:48:49
![](https://img.aspxhome.com/file/2023/1/94661_0s.jpg)
Android 实现自己的LOG信息
2023-06-08 16:30:30
![](https://img.aspxhome.com/file/2023/7/138397_0s.png)
java开发RocketMQ之NameServer路由管理源码分析
2023-10-27 20:56:12
![](https://img.aspxhome.com/file/2023/6/61266_0s.png)
Java String不可变性实现原理解析
2023-11-09 23:15:53
如何通过Android Stduio来编写一个完整的天气预报APP
2023-10-11 17:45:01
![](https://img.aspxhome.com/file/2023/8/96028_0s.png)
Spring boot 连接多数据源过程详解
2023-11-28 12:09:51
Java动态 代理的应用详解
2023-11-25 08:15:24
Android自定义广播接收
2023-04-30 09:39:31
![](https://img.aspxhome.com/file/2023/8/123278_0s.jpg)
Java面试题冲刺第二十六天--实战编程
2023-05-22 16:01:54
![](https://img.aspxhome.com/file/2023/4/90984_0s.png)
如何创建SpringBoot项目
2022-03-26 16:06:37
![](https://img.aspxhome.com/file/2023/7/68167_0s.png)
java(包括springboot)读取resources下文件方式实现
2021-06-03 20:16:06
![](https://img.aspxhome.com/file/2023/4/68184_0s.png)
详解ASP.NET中Identity的身份验证代码
2022-05-20 04:43:48
![](https://img.aspxhome.com/file/2023/5/108815_0s.png)
MyBatis的SQL执行结果和客户端执行结果不一致问题排查
2022-12-30 20:37:22
![](https://img.aspxhome.com/file/2023/6/73096_0s.jpg)
Android提高之MediaPlayer音视频播放
2021-05-29 08:44:37
![](https://img.aspxhome.com/file/2023/2/102602_0s.jpg)
Unity3D制作序列帧动画的方法
2022-07-04 05:45:33
![](https://img.aspxhome.com/file/2023/6/126056_0s.png)