当前位置: 首页 > news >正文

网站怎么做优化百度能搜索到全网营销软件

网站怎么做优化百度能搜索到,全网营销软件,交互做的好网站,乌鲁木齐建网站生成简单的验证码图片 几个简单使用到的类具体实现VerifyCode.classVerifyCodeUtil.classUtilTest.class 测试结果 几个简单使用到的类 BufferedImage : 图像 Graphics2D :图像的上下文 Color : 颜色对象 Font : 字体对象 具体信息大家可以查…

生成简单的验证码图片

  • 几个简单使用到的类
  • 具体实现
    • VerifyCode.class
    • VerifyCodeUtil.class
    • UtilTest.class
  • 测试结果

几个简单使用到的类

BufferedImage : 图像
Graphics2D :图像的上下文
Color : 颜色对象
Font : 字体对象
具体信息大家可以查一下JDK文档。我用的是这个 这个。

具体实现

VerifyCode.class

包含了验证码图片的基本信息,包括但不局限于图片的尺寸、背景颜色、干扰线的数目,干扰线的颜色、验证码的格式、验证码的颜色等等。

import java.awt.*;
import java.util.Random;/*** @author 三文鱼* @title 验证码实体类* @description 包含验证码的必要信息* @date 2022/4/26**/
public class VerifyCode {//图片的宽、高private int width;private int height;//干扰线的条数private int lineCount;//干扰线的颜色private Color lineColor;//干扰线的长度private int lineLength;//验证码背景颜色private Color bgColor;//验证码颜色private Color codeColor;//验证码类型 字符、数字、或者字符数字的随机组合private int codeType;//旋转角度private int angle;//字体private Font font;//用于产生随机颜色和数字private static Random random;public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public int getLineCount() {return lineCount;}public void setLineCount(int lineCount) {this.lineCount = lineCount;}public Color getBgColor() {return bgColor;}public void setBgColor(Color bgColor) {this.bgColor = bgColor;}public Color getCodeColor() {return codeColor;}public void setCodeColor(Color codeColor) {this.codeColor = codeColor;}public int getCodeType() {return codeType;}public void setCodeType(int codeType) {this.codeType = codeType;}public int getAngle() {return angle;}public void setAngle(int angle) {this.angle = angle;}public Font getFont() {return font;}public void setFont(Font font) {this.font = font;}public static Random getRandom() {return random;}public static void setRandom(Random random) {VerifyCode.random = random;}public Color getLineColor() {return lineColor;}public void setLineColor(Color lineColor) {this.lineColor = lineColor;}public int getLineLength() {return lineLength;}public void setLineLength(int lineLength) {this.lineLength = lineLength;}static {random = new Random();}public VerifyCode() {}//不产生干扰线public VerifyCode(int width, int height) {this.width = width;this.height = height;}public VerifyCode(int width, int height, int lineCount) {this.width = width;this.height = height;this.lineCount = lineCount;}public VerifyCode(int width, int height, int lineCount , int codeType) {this.width = width;this.height = height;this.lineCount = lineCount;this.codeType = codeType;}public VerifyCode(int width, int height, int lineCount , int codeType , int angle) {this.width = width;this.height = height;this.lineCount = lineCount;this.codeType = codeType;this.angle = angle;}public VerifyCode(int width, int height, int lineCount, int codeType, int angle, Font font) {this.width = width;this.height = height;this.lineCount = lineCount;this.codeType = codeType;this.angle = angle;this.font = font;}public VerifyCode(int width, int height, int lineCount, Color bgColor) {this.width = width;this.height = height;this.lineCount = lineCount;this.bgColor = bgColor;}public VerifyCode(int width, int height, int lineCount, Color bgColor, Color codeColor) {this.width = width;this.height = height;this.lineCount = lineCount;this.bgColor = bgColor;this.codeColor = codeColor;}public VerifyCode(int width, int height, int lineCount, Color bgColor, Color codeColor, int codeType) {this.width = width;this.height = height;this.lineCount = lineCount;this.bgColor = bgColor;this.codeColor = codeColor;this.codeType = codeType;}public VerifyCode(int width, int height, int lineCount, Color bgColor, Color codeColor, int codeType, int angle) {this.width = width;this.height = height;this.lineCount = lineCount;this.bgColor = bgColor;this.codeColor = codeColor;this.codeType = codeType;this.angle = angle;}public VerifyCode(int width, int height, int lineCount, Color bgColor, Color codeColor, int codeType, int angle, Font font) {this.width = width;this.height = height;this.lineCount = lineCount;this.bgColor = bgColor;this.codeColor = codeColor;this.codeType = codeType;this.angle = angle;this.font = font;}
}

VerifyCodeUtil.class

该类是验证码图片的工具类,用于生成验证码图片,也可以将生成的验证码图片以输出流的方式输出到文件或者相应的response中。

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;/*** @author 三文鱼* @title 验证码工具类* @description* @date 2022/4/26**/
public class VerifyCodeUtil {private static final String TARGAET_STRING = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuioplkjhgfdsazxcvbnm0123456789";/** @description 获得验证码图片对象* @author 三文鱼* @date 16:07 2022/4/26 * @param verifyCode* @return java.awt.image.BufferedImage**/public static BufferedImage getVerifyCodeImage(VerifyCode verifyCode) {if (verifyCode == null)return null;if(verifyCode.getWidth() <=105 || verifyCode.getHeight() <= 35) {verifyCode.setWidth(105);verifyCode.setHeight(35);}int width = verifyCode.getWidth();int height = verifyCode.getHeight();//以三原色为底色构建图像final BufferedImage bufferedImage = new BufferedImage(width , height, BufferedImage.TYPE_INT_RGB);final Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics();//随机数种子final Random random = VerifyCode.getRandom();//设置默认底色和边框if(verifyCode.getBgColor() != null) {graphics2D.setColor(verifyCode.getBgColor());}else {graphics2D.setColor(Color.WHITE);}//填充颜色graphics2D.fillRect(0 , 0 , verifyCode.getWidth() , verifyCode.getHeight());//绘出边框graphics2D.drawRect(0 , 0 , verifyCode.getWidth()-1 , verifyCode.getHeight() - 1);//生成干扰线int count = verifyCode.getLineCount();//干扰线颜色Color lineColor = verifyCode.getLineColor();//干扰线的长度int lineLength = verifyCode.getLineLength();if (lineLength == 0)lineLength = 1;if(count == 0) {count = 20;}for (int i =0; i <count; i++) {if(lineColor != null) {graphics2D.setColor(lineColor);}else {graphics2D.setColor(getRandomColor(random));}//生成随机的起点坐标int x = random.nextInt(width - 1 - 1);int y = random.nextInt(height - 1 -1);//生成随机的终点坐标int xEnd = x + random.nextInt(lineLength);int yEnd = y + random.nextInt(lineLength);//绘制线条到图像里面graphics2D.drawLine(x, y , xEnd, yEnd);}//获取验证码用于生成图片final String code = getRandomCode(random , 0);//验证码颜色、字体、旋转角度Color fontColor = verifyCode.getCodeColor();Font font = verifyCode.getFont();int angle = verifyCode.getAngle();int rotateAngle = 0;for(int i =0; i < code.length(); i++) {//设置字体颜色if(fontColor == null) {graphics2D.setColor(getRandomColor(random));}else {graphics2D.setColor(fontColor);}//设置字体样式if (font == null) {graphics2D.setFont(getRandomFont(random , height));}else{graphics2D.setFont(font);}int x = (width/5)*i + width/5;if(angle != 0) {//旋转图像rotateAngle = random.nextInt(angle) - angle/2;graphics2D.rotate(Math.toRadians(rotateAngle) ,x  , height/2);}//绘制字符graphics2D.drawString(String.valueOf(code.charAt(i)) , x , height/2);if(angle != 0) {//逆向旋转 将图像转正graphics2D.rotate(-Math.toRadians(rotateAngle) ,x  , height/2);}}graphics2D.dispose();// 图象生效  释放资源return bufferedImage;}/** @description  获取随机的颜色对象* @author 三文鱼* @date 16:07 2022/4/26* @param random* @return java.awt.Color**/public static Color getRandomColor(Random random) {return new Color(random.nextInt(255) , random.nextInt(255), random.nextInt(255));}/** @description 产生随机字符串* @author 三文鱼* @date 16:06 2022/4/26 * @param random* @param type  0-字母  1-数字 2-数字+字母* @return java.lang.String**/public static String getRandomCode(Random random , int type) {StringBuilder stringBuilder = new StringBuilder();int base = 0;int length = 51;//截取数字if(type == 1) {length = 9;base = 52;}//数字 字符串结合if(type == 2) {length = 61;}for(int i =0; i < 4; i++) {stringBuilder.append(TARGAET_STRING .charAt(random.nextInt(length) + base));}return stringBuilder.toString();}/** @description 设置随机字体大小* @author 三文鱼* @date 16:05 2022/4/26 * @param random* @param height* @return java.awt.Font**/public static Font getRandomFont(Random random , int height) {return new Font("楷体" , Font.BOLD , random.nextInt(20) + height/3);}/** @description 将图片以流的形式输出  可以是本地的文件或者是response的输出流* @author 三文鱼* @date 16:05 2022/4/26* @param image* @param out* @return void**/public static void storeImagetoF(BufferedImage image , OutputStream out)  throws IOException {ImageIO.write(image ,"JPEG", out);}
}

UtilTest.class

简单测试类

import java.awt.*;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;/*** @author 三文鱼* @title* @description* @date 2022/4/26**/
public class UtilTest {public static void main(String[] args) {//默认参数生成的验证码图片//VerifyCode verifyCode = new VerifyCode();//自定义参数生成的验证码图片VerifyCode verifyCode = new VerifyCode(300 , 100 , 200 , Color.WHITE , Color.BLACK, 2 , 45);for (int i = 0; i < 5; i++) {try{Thread.sleep(1000);//文件名称String imageName = getNowDate();//输出的完整路径String totalPath = "F:\\学习记录\\image\\" + imageName +".jpg";VerifyCodeUtil.storeImagetoF(VerifyCodeUtil.getVerifyCodeImage(verifyCode) , new FileOutputStream(totalPath));}catch (Exception exception){exception.printStackTrace();}}}/** @description 获取当前的时间作为图片文件名称* @author 三文鱼* @date 16:17 2022/4/26* @return java.lang.String**/public static String getNowDate() {SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");Date date = new Date(System.currentTimeMillis());return simpleDateFormat.format(date);}
}

测试结果

默认生成的验证码文件,由于默认是宽是一百,高是三十,所以放大后会模糊。
在这里插入图片描述
自定义数据生成为验证码图片
在这里插入图片描述

http://www.ds6.com.cn/news/923.html

相关文章:

  • 人个做外贸用什么网站好网站网页设计
  • 怎么做网站地图网络推广seo怎么做
  • 手机网站淘宝客学生个人网页优秀模板
  • 用dw做销售网站谷歌排名优化
  • 太原市住房和城乡建设委员会官方网站百度seo查询收录查询
  • 移动端app开发需要哪些技术win7优化大师官网
  • 购物网站seo搜索引擎优化方案优就业seo课程学多久
  • 商城网站开发公司视频号下载器手机版
  • 平面网站模版谷歌关键词优化怎么做
  • 关于做情侣的网站的图片大全东莞网络营销销售
  • 网站推广方法 优帮云乐天seo培训
  • 网站开发与设计 课程简介昆明seo关键字推广
  • 中国大连网站百度安全中心
  • 做的网站用户密码在哪里找永久免费客服系统
  • 廊坊三河市疫情最新消息seo入门培训课程
  • 沧州企业网站专业定制企业网站模板html
  • 做零售外贸网站有哪些免费的网站推广在线推广
  • 沧州免费网站建设白山seo
  • asp购物网站客户查看购物车广告点击一次多少钱
  • scrm服务商福州seo技巧培训
  • 武汉房地产政策最新消息大丰seo排名
  • win怎么开做网站其他端口网站如何推广运营
  • 做动漫网站如何应用数据绑定网络广告公司
  • 中国最大的外包公司有哪些深圳seo优化服务商
  • 北京公司网站建设定如何推广网站
  • 网站用户体验存在问题chrome官网下载
  • 昆明网站建设论坛网站建设策划
  • 突出网站建设 突出能力优化落实防控措施
  • 盘锦市政建设集团网站软文写作是什么意思
  • 网站建设方案书备案事件营销成功案例