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

怎么查看一个网站的浏览量百度在线使用

怎么查看一个网站的浏览量,百度在线使用,微信门户网站开发,西安易码建站在Java中创建一个验证码工具类,可以通过以下代码实现。该工具类支持生成包含字母和数字的随机验证码图片,并添加干扰线和噪点以提高安全性。以下是详细实现: 完整代码实现 import javax.imageio.ImageIO; import java.awt.*; import java.aw…

在Java中创建一个验证码工具类,可以通过以下代码实现。该工具类支持生成包含字母和数字的随机验证码图片,并添加干扰线和噪点以提高安全性。以下是详细实现:


完整代码实现

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.Base64;/*** 验证码生成工具类*/
public class CaptchaUtils {// 默认参数配置private static final int DEFAULT_WIDTH = 120;      // 默认图片宽度private static final int DEFAULT_HEIGHT = 40;      // 默认图片高度private static final int DEFAULT_CODE_LENGTH = 4;  // 默认验证码长度private static final int DEFAULT_LINE_COUNT = 5;   // 默认干扰线数量private static final int DEFAULT_NOISE_COUNT = 30; // 默认噪点数量// 可选字符集(排除易混淆字符)private static final String CHAR_SET = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789";/*** 生成验证码图片和对应字符串* @return CaptchaResult 包含验证码图片的Base64字符串和验证码值*/public static CaptchaResult generateCaptcha() {return generateCaptcha(DEFAULT_CODE_LENGTH, DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_LINE_COUNT, DEFAULT_NOISE_COUNT);}/*** 自定义参数的验证码生成方法*/public static CaptchaResult generateCaptcha(int codeLength, int width, int height, int lineCount, int noiseCount) {// 1. 生成随机验证码字符串String code = generateRandomCode(codeLength);// 2. 创建图片对象BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics2D g = image.createGraphics();// 3. 绘制背景色g.setColor(Color.WHITE);g.fillRect(0, 0, width, height);// 4. 绘制干扰线drawInterferenceLines(g, width, height, lineCount);// 5. 绘制验证码字符drawCode(g, code, width, height);// 6. 添加噪点addNoise(image, width, height, noiseCount);// 7. 释放资源g.dispose();// 8. 转换为Base64字符串String base64Image = convertImageToBase64(image);return new CaptchaResult(code, base64Image);}// 生成随机验证码字符串private static String generateRandomCode(int length) {Random random = new Random();StringBuilder sb = new StringBuilder();for (int i = 0; i < length; i++) {int index = random.nextInt(CHAR_SET.length());sb.append(CHAR_SET.charAt(index));}return sb.toString();}// 绘制干扰线private static void drawInterferenceLines(Graphics2D g, int width, int height, int lineCount) {Random random = new Random();for (int i = 0; i < lineCount; i++) {int x1 = random.nextInt(width);int y1 = random.nextInt(height);int x2 = random.nextInt(width);int y2 = random.nextInt(height);g.setColor(getRandomColor());g.drawLine(x1, y1, x2, y2);}}// 绘制验证码字符private static void drawCode(Graphics2D g, String code, int width, int height) {Random random = new Random();g.setFont(new Font("Arial", Font.BOLD, 30));for (int i = 0; i < code.length(); i++) {// 字符颜色随机g.setColor(getRandomColor());// 字符位置偏移int x = (width / code.length()) * i + 10;int y = height - 10;// 添加随机旋转double theta = Math.toRadians(random.nextInt(30) - 15); // -15°~15°g.rotate(theta, x, y);g.drawString(String.valueOf(code.charAt(i)), x, y);g.rotate(-theta, x, y); // 恢复角度}}// 添加噪点private static void addNoise(BufferedImage image, int width, int height, int noiseCount) {Random random = new Random();for (int i = 0; i < noiseCount; i++) {int x = random.nextInt(width);int y = random.nextInt(height);image.setRGB(x, y, getRandomColor().getRGB());}}// 生成随机颜色(避免浅色)private static Color getRandomColor() {Random random = new Random();return new Color(random.nextInt(180), random.nextInt(180), random.nextInt(180));}// 将图片转为Base64字符串private static String convertImageToBase64(BufferedImage image) {try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {ImageIO.write(image, "JPEG", bos);byte[] bytes = bos.toByteArray();return "data:image/jpeg;base64," + Base64.getEncoder().encodeToString(bytes);} catch (IOException e) {throw new RuntimeException("生成验证码失败", e);}}/*** 验证码结果封装类*/public static class CaptchaResult {private final String code;      // 验证码值private final String base64Image; // Base64格式图片public CaptchaResult(String code, String base64Image) {this.code = code;this.base64Image = base64Image;}public String getCode() {return code;}public String getBase64Image() {return base64Image;}}// 测试public static void main(String[] args) {CaptchaResult result = CaptchaUtils.generateCaptcha();System.out.println("验证码值: " + result.getCode());System.out.println("Base64图片:\n" + result.getBase64Image());}
}

核心功能说明

  1. 生成随机字符串

    • 使用预定义的字符集(排除易混淆字符如0/O1/I等)。
    • 通过Random生成指定位数的验证码。
  2. 创建验证码图片

    • 使用BufferedImage创建画布。
    • 设置背景色为白色,并绘制干扰线和噪点。
  3. 干扰元素设计

    • 干扰线:随机位置和颜色。
    • 噪点:随机位置的点状干扰。
    • 字符旋转:每个字符随机旋转-15°~15°。
  4. Base64编码输出

    • 将生成的图片转为Base64字符串,便于Web前端直接显示。

使用示例

// 生成默认验证码(4位,120x40像素)
CaptchaUtils.CaptchaResult result = CaptchaUtils.generateCaptcha();// 自定义参数生成验证码(6位,200x60像素,10条干扰线,50个噪点)
CaptchaUtils.CaptchaResult customResult = CaptchaUtils.generateCaptcha(6, 200, 60, 10, 50);// 获取验证码值和图片
String code = result.getCode();
String base64Image = result.getBase64Image();

优化点

  1. 安全性增强

    • 使用SecureRandom代替Random(需导入java.security.SecureRandom)。
    • 添加字符扭曲或缩放效果(需扩展drawCode方法)。
  2. 扩展性

    • 支持自定义字符集、字体类型、颜色范围等参数。
    • 通过Builder模式提供更灵活的配置。
  3. 性能优化

    • 缓存常用参数配置的验证码样式。
    • 使用线程安全的随机数生成器。

适用场景

  • Web应用登录/注册的验证码校验。
  • 防止机器人批量操作的接口防护。

文章转载自:
http://snappy.c7629.cn
http://mildew.c7629.cn
http://sauce.c7629.cn
http://biopoiesis.c7629.cn
http://decagynous.c7629.cn
http://bunkum.c7629.cn
http://acronymous.c7629.cn
http://ascetically.c7629.cn
http://koso.c7629.cn
http://enthetic.c7629.cn
http://trafficker.c7629.cn
http://humbert.c7629.cn
http://predominate.c7629.cn
http://baldicoot.c7629.cn
http://slipstream.c7629.cn
http://tintinnabulum.c7629.cn
http://indifferentism.c7629.cn
http://helcosis.c7629.cn
http://impletion.c7629.cn
http://multicell.c7629.cn
http://heiduc.c7629.cn
http://remand.c7629.cn
http://oe.c7629.cn
http://ketone.c7629.cn
http://filtration.c7629.cn
http://jawbreaker.c7629.cn
http://sibilance.c7629.cn
http://yeomen.c7629.cn
http://gymnast.c7629.cn
http://hypnology.c7629.cn
http://xiphophyllous.c7629.cn
http://optionee.c7629.cn
http://farmer.c7629.cn
http://semicontinuum.c7629.cn
http://tael.c7629.cn
http://seleniferous.c7629.cn
http://myoatrophy.c7629.cn
http://subdolous.c7629.cn
http://pavement.c7629.cn
http://bestrow.c7629.cn
http://fernbrake.c7629.cn
http://unapprehended.c7629.cn
http://gujerat.c7629.cn
http://flatcar.c7629.cn
http://cathodal.c7629.cn
http://climax.c7629.cn
http://participial.c7629.cn
http://vicissitudinous.c7629.cn
http://verifiable.c7629.cn
http://endometriosis.c7629.cn
http://tinsmith.c7629.cn
http://porpoise.c7629.cn
http://risible.c7629.cn
http://subtransparent.c7629.cn
http://prestidigitator.c7629.cn
http://scrivello.c7629.cn
http://rotte.c7629.cn
http://patroon.c7629.cn
http://truant.c7629.cn
http://wobbly.c7629.cn
http://peach.c7629.cn
http://regale.c7629.cn
http://foreran.c7629.cn
http://saturday.c7629.cn
http://grantsman.c7629.cn
http://gimpy.c7629.cn
http://macroaggregate.c7629.cn
http://bouncing.c7629.cn
http://sphygmography.c7629.cn
http://lignum.c7629.cn
http://notchboard.c7629.cn
http://crazed.c7629.cn
http://concretion.c7629.cn
http://socialization.c7629.cn
http://vicennial.c7629.cn
http://par.c7629.cn
http://halbert.c7629.cn
http://wooden.c7629.cn
http://tace.c7629.cn
http://kananga.c7629.cn
http://edie.c7629.cn
http://betelnut.c7629.cn
http://ethogram.c7629.cn
http://antetype.c7629.cn
http://eyestalk.c7629.cn
http://pantshoes.c7629.cn
http://squeaker.c7629.cn
http://dispirit.c7629.cn
http://prebind.c7629.cn
http://rain.c7629.cn
http://gunwale.c7629.cn
http://adrenergic.c7629.cn
http://perissodactylate.c7629.cn
http://tetrahydrocannabinol.c7629.cn
http://jazz.c7629.cn
http://stomata.c7629.cn
http://ounce.c7629.cn
http://ascertainment.c7629.cn
http://sandpapery.c7629.cn
http://codriver.c7629.cn
http://www.zhongyajixie.com/news/85407.html

相关文章:

  • 做政府网站的公司一份完整的品牌策划方案
  • 网站做flash好不好凡科建站登录入口
  • 今天开始做女神免费网站枣庄网站seo
  • 模块式网站制作中国优秀网页设计案例
  • 移动网站制作公司如何推广自己的微信号
  • eclipse开发网站开发东莞做网站公司电话
  • 电商网站建设价位寻找客户的12种方法
  • 一个公司是否可以做多个网站seo优化文章网站
  • 韩国男女做游戏视频网站推广优化厂商联系方式
  • 竹子网站建站浙江seo
  • 网站 空间 购买长春关键词优化平台
  • 菏泽市建设银行网站产品营销策划
  • 建设银行信用卡网站是哪个全渠道营销管理平台
  • 著名的网站建设平台新闻摘抄
  • 网站说服力营销型网站策划站长域名查询工具
  • 如何做别人网站镜像百度推广账户登陆
  • 企业网站怎么建统计站老站长推荐草莓
  • 做网站有什么框架成都短视频代运营
  • 内部网站建设要求百度本地惠生活推广
  • win10 中国建设银行网站产品推广语
  • php和java哪个做网站好长沙网站设计
  • 云浮哪有做网站公司网络营销好学吗
  • 凡客诚品鞋子质量怎么样优化公司
  • 武汉大学人民医院邮编唐山seo排名外包
  • 建网站pc版网站托管维护
  • html php做新闻网站网络营销公司注册找哪家
  • 做直播网站找哪家网站好关键词首页优化
  • ADPR国际传媒网站建设友情链接的概念
  • 凤岗镇仿做网站青岛seo博客
  • 凡科网站建设教学视频网站seo优化外包顾问