当前位置: 首页> 游戏> 攻略 > 沧州英文模板建站_定制平台有哪些_seo入门培训学多久_推广专员是做什么的

沧州英文模板建站_定制平台有哪些_seo入门培训学多久_推广专员是做什么的

时间:2025/7/13 23:17:37来源:https://blog.csdn.net/2402_88323987/article/details/147260089 浏览次数:0次
沧州英文模板建站_定制平台有哪些_seo入门培训学多久_推广专员是做什么的

实现图形验证码:

  private void generateVerificationCode() {GraphicsContext gc = codeCanvas.getGraphicsContext2D();int width = (int) codeCanvas.getWidth();int height = (int) codeCanvas.getHeight();// 清空画布gc.clearRect(0, 0, width, height);// 生成验证码文本(4位字母数字组合)verificationCode = generateRandomCode(4);// 绘制背景gc.setFill(Color.WHITE);gc.fillRect(0, 0, width, height);// 绘制干扰线drawInterferenceLines(gc, width, height);// 绘制验证码文本drawVerificationCode(gc, width, height);// 添加噪点drawNoise(gc, width, height);}private String generateRandomCode(int length) {String chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";StringBuilder sb = new StringBuilder();for (int i = 0; i < length; i++) {sb.append(chars.charAt(random.nextInt(chars.length())));}return sb.toString();}private void drawInterferenceLines(GraphicsContext gc, int width, int height) {gc.setStroke(Color.LIGHTGRAY);for (int i = 0; i < 5; i++) {gc.strokeLine(random.nextInt(width),random.nextInt(height),random.nextInt(width),random.nextInt(height));}}private void drawVerificationCode(GraphicsContext gc, int width, int height) {gc.setFont(Font.font(20));for (int i = 0; i < verificationCode.length(); i++) {// 设置随机颜色gc.setFill(Color.rgb(random.nextInt(150),random.nextInt(150),random.nextInt(150)));// 字符位置计算double x = 20 + i * 20;double y = height - 10;// 保存当前画布状态gc.save();// 添加旋转效果gc.translate(x, y);gc.rotate(random.nextDouble() * 30 - 15);// 绘制字符gc.fillText(String.valueOf(verificationCode.charAt(i)),-10,5);// 恢复画布状态gc.restore();}}private void drawNoise(GraphicsContext gc, int width, int height) {gc.setFill(Color.BLACK);for (int i = 0; i < 50; i++) {gc.fillRect(random.nextInt(width),random.nextInt(height),1, 1);}}

整体思路

  1. 清空画布:在生成新的验证码之前,先清空Canvas上原有的内容。
  2. 生成验证码文本:随机生成一个 4 位的字母数字组合作为验证码。
  3. 绘制背景:在画布上绘制白色背景。
  4. 添加干扰元素:为了防止验证码被机器自动识别,在画布上绘制干扰线和噪点。
  5. 绘制验证码文本:将生成的验证码文本以随机颜色和旋转效果绘制在画布上

 

代码详细解释

generateVerificationCode方法

java

private void generateVerificationCode() {GraphicsContext gc = codeCanvas.getGraphicsContext2D();int width = (int) codeCanvas.getWidth();int height = (int) codeCanvas.getHeight();// 清空画布gc.clearRect(0, 0, width, height);// 生成验证码文本(4位字母数字组合)verificationCode = generateRandomCode(4);// 绘制背景gc.setFill(Color.WHITE);gc.fillRect(0, 0, width, height);// 绘制干扰线drawInterferenceLines(gc, width, height);// 绘制验证码文本drawVerificationCode(gc, width, height);// 添加噪点drawNoise(gc, width, height);
}

  • 首先获取CanvasGraphicsContext对象,用于在画布上进行绘制操作。
  • 接着获取画布的宽度和高度。
  • 调用clearRect方法清空画布上的所有内容。
  • 调用generateRandomCode方法生成一个 4 位的字母数字组合作为验证码。
  • 设置填充颜色为白色,并使用fillRect方法绘制白色背景。
  • 调用drawInterferenceLines方法绘制干扰线。
  • 调用drawVerificationCode方法绘制验证码文本。
  • 调用drawNoise方法添加噪点。
generateRandomCode方法

java

private String generateRandomCode(int length) {String chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";StringBuilder sb = new StringBuilder();for (int i = 0; i < length; i++) {sb.append(chars.charAt(random.nextInt(chars.length())));}return sb.toString();
}

  • 定义一个包含所有可能字符的字符串chars
  • 使用StringBuilder来构建验证码字符串。
  • 通过random.nextInt(chars.length())随机选择一个字符,并添加到StringBuilder中,重复length次。
  • 最后将StringBuilder转换为字符串并返回。
drawInterferenceLines方法

java

private void drawInterferenceLines(GraphicsContext gc, int width, int height) {gc.setStroke(Color.LIGHTGRAY);for (int i = 0; i < 5; i++) {gc.strokeLine(random.nextInt(width),random.nextInt(height),random.nextInt(width),random.nextInt(height));}
}

  • 设置笔触颜色为浅灰色。
  • 使用for循环绘制 5 条干扰线,每条干扰线的起点和终点坐标都是随机生成的。
drawVerificationCode方法

java

private void drawVerificationCode(GraphicsContext gc, int width, int height) {gc.setFont(Font.font(20));for (int i = 0; i < verificationCode.length(); i++) {// 设置随机颜色gc.setFill(Color.rgb(random.nextInt(150),random.nextInt(150),random.nextInt(150)));// 字符位置计算double x = 20 + i * 20;double y = height - 10;// 保存当前画布状态gc.save();// 添加旋转效果gc.translate(x, y);gc.rotate(random.nextDouble() * 30 - 15);// 绘制字符gc.fillText(String.valueOf(verificationCode.charAt(i)),-10,5);// 恢复画布状态gc.restore();}
}

  • 设置字体大小为 20。
  • 遍历验证码的每个字符,为每个字符设置随机颜色。
  • 计算字符的绘制位置。
  • 使用gc.save()保存当前画布状态。
  • 通过gc.translategc.rotate方法对画布进行平移和旋转操作,为字符添加随机旋转效果。
  • 绘制字符。
  • 使用gc.restore()恢复画布状态。
drawNoise方法

java

private void drawNoise(GraphicsContext gc, int width, int height) {gc.setFill(Color.BLACK);for (int i = 0; i < 50; i++) {gc.fillRect(random.nextInt(width),random.nextInt(height),1, 1);}
}

  • 设置填充颜色为黑色。
  • 使用for循环绘制 50 个 1x1 像素的黑色矩形,作为噪点。

综上所述,这段代码通过一系列的绘制操作,在Canvas上生成了一个包含干扰元素的验证码。

 

关键字:沧州英文模板建站_定制平台有哪些_seo入门培训学多久_推广专员是做什么的

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: