当前位置 : 主页 > 编程语言 > java >

imageIO图像流 验证码效果案例

来源:互联网 收集:自由互联 发布时间:2022-09-29
package cn.images; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.util.Random; public class CapcahaDemo { public static void main(String[] args) throws Exception { //验证
package cn.images;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;

public class CapcahaDemo {
public static void main(String[] args) throws Exception {
//验证码的长宽
int len = 4;
int width = len * 26;
int height =50;
//验证码中的数字 字母
String letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var rand = new Random();
StringBuilder sub = new StringBuilder();
var cc = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
var g= cc.createGraphics();
setRenderingHint(g);
//背景的随机把颜色
g.setColor(new Color(rand.nextInt(26)+220,rand.nextInt(26)+220,rand.nextInt(26)+220));
//背景 填充矩形 位置 宽高
g.fillRect(0,0,width,height);
//字体颜色
var c = new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256));
g.setColor(c);
//生成四个随机验证码内容
for(int i = 0;i<len;i++){
//将其转换成字符串(返回指定索引处的char值。索引范围是从0到length() - 1 (返回一个大于0小于letter的长度的随机数))
//就是从letter中随机取出一个
String str = String.valueOf(letter.charAt(rand.nextInt(letter.length())));
sub.append(str);
//验证码中内容的位置
var x = i*25+5;
var y = 35;
//设置文字旋转角度
double radianPercent = Math.PI*(rand.nextInt(35)/180D);
if(rand.nextBoolean())
radianPercent = -radianPercent;
//文字正向旋转
g.rotate(radianPercent,x,y);
//验证码内容的字体样式 粗细正常 字体大小[20-40)
g.setFont(new Font("微软雅黑",Font.PLAIN,rand.nextInt(20)+20));
g.drawString(str,x,y);
//文字反向旋转
g.rotate(-radianPercent,x,y);

}
//干扰文件
for(int i=0;i<len*10;i++) {
var x = rand.nextInt(width);
var y = rand.nextInt(height);
String str = String.valueOf(letter.charAt(rand.nextInt(letter.length())));
double radianPercent = Math.PI * (rand.nextInt(35) / 180D);
//if (rand.nextBoolean())
// radianPercent = -radianPercent;
//g.rotate(radianPercent, x, y);

c = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256),rand.nextInt(30)+60);
g.setColor(c);

g.setFont(new Font("微软雅黑", Font.PLAIN, rand.nextInt(10) + 10));
g.drawString(str, x, y);
}

// 干扰正统曲线
//字体颜色相同
g.setColor(new Color(rand.nextInt(255),rand.nextInt(255),rand.nextInt(255)));

int yy = rand.nextInt(35)+15;
int hh = rand.nextInt(20)+10;
int aa = rand.nextInt(60)+20;

for(int x= 0; x<width;x++){
int y = (int)(yy+ hh * Math.sin(x*Math.PI/aa));

g.fillOval(x,y,5,5);
}

g.dispose();

ImageIO.write(cc,"png",new File("cc.png"));

Runtime.getRuntime().exec("cmd /k start cc.png");
}
//设置线条平滑
public static void setRenderingHint(Graphics2D g) {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
}

public static Font getRandFont(int end, int start) {
Random rand = new Random();
Font font = null;
if(rand.nextBoolean()){
font = new Font("微软雅黑",Font.PLAIN , rand.nextInt(end) + start);

}else {
try{
Font f = Font.createFont(Font.TRUETYPE_FONT,new File("fonts/zh164hfyh.ttf"));
font = f.deriveFont(Font.PLAIN,rand.nextInt(end)+ start);
}catch (Exception e){
e.printStackTrace();
}
}
return font;
}

}

网友评论