当前位置:网站首页>sprintboot验证码kaptcha 自定义图片样式
sprintboot验证码kaptcha 自定义图片样式
2022-08-10 16:10:00 【if...else...】
前言
本文教大家如何自定义kaptcha的图片样式,效果如下:
一、kaptcha默认的图片样式的问题
这里我以鱼眼效果为例,大家看下默认的效果:
从上图大家可以看到,文字变形很严重,不是很好辨认。
下面是xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 生成kaptcha的bean-->
<bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
<property name="config">
<bean class="com.google.code.kaptcha.util.Config">
<constructor-arg type="java.util.Properties">
<!--设置kaptcha属性 -->
<props>
<prop key="kaptcha.border ">yes</prop>
<prop key="kaptcha.border.color">105,179,90</prop>
<prop key="kaptcha.textproducer.font.color">black</prop>
<prop key="kaptcha.image.width">100</prop>
<prop key="kaptcha.image.height">44</prop>
<prop key="kaptcha.textproducer.font.size">27</prop>
<prop key="kaptcha.session.key">code</prop>
<prop key="kaptcha.textproducer.char.length">4</prop>
<prop key="kaptcha.textproducer.font.names">Arial, Courier</prop>
<prop key="kaptcha.textproducer.char.string">0123456789</prop>
<prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.FishEyeGimpy</prop>
<!-- <prop key="kaptcha.noise.color">black</prop>-->
<prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</prop>
<prop key="kaptcha.textproducer.char.space">3</prop>
<prop key="kaptcha.textproducer.impl">com.google.code.kaptcha.text.impl.DefaultTextCreator</prop> </props>
</constructor-arg>
</bean>
</property>
</bean>
</beans>
其中图片效果用的是com.google.code.kaptcha.impl.FishEyeGimpy
二、自定义图片效果的步骤
这里我还是以鱼纹效果为例
1.创建自定义鱼纹效果类
代码如下(示例):
package com.zjyc.qzaft.entity.customs;
import com.google.code.kaptcha.impl.FishEyeGimpy;
import java.awt.*;
import java.awt.image.BufferedImage;
/** * Author: mek * Date: 2022\5\14 0014 * Time: 10:37 * Description: 自定义鱼眼纹效果 */
public class AftFishEyeGimpy extends FishEyeGimpy {
@Override
public BufferedImage getDistortedImage(BufferedImage baseImage) {
Graphics2D graph = (Graphics2D)baseImage.getGraphics();
int imageHeight = baseImage.getHeight();
int imageWidth = baseImage.getWidth();
int horizontalLines = imageHeight / 7;
int verticalLines = imageWidth / 7;
int horizontalGaps = imageHeight / (horizontalLines + 1);
int verticalGaps = imageWidth / (verticalLines + 1);
int i;
for(i = horizontalGaps; i < imageHeight; i += horizontalGaps) {
graph.setColor(Color.blue);
graph.drawLine(0, i, imageWidth, i);
}
for(i = verticalGaps; i < imageWidth; i += verticalGaps) {
graph.setColor(Color.red);
graph.drawLine(i, 0, i, imageHeight);
}
int[] pix = new int[imageHeight * imageWidth];
int j = 0;
for(int j1 = 0; j1 < imageWidth; ++j1) {
for(int k1 = 0; k1 < imageHeight; ++k1) {
pix[j] = baseImage.getRGB(j1, k1);
++j;
}
}
/*这里删除了默认鱼纹效果类的渲染代码*/
return baseImage;
}
}
2.修改kaptcha.xml配置
将图片效果类设置成自己的AftFishEyeGimpy类
代码如下(示例):
<prop key="kaptcha.obscurificator.impl">com.zjyc.qzaft.entity.customs.AftFishEyeGimpy</prop>
总结
自定义图片效果的方法,就是新建类继承默认的效果类,然后重写getDistortedImage函数。
边栏推荐
猜你喜欢
随机推荐
拆分整数为2的幂次项和 → 理解多重背包问题二进制优化的核心思想
北海 Kraken:基于 Flutter 构建的高性能 Web 渲染引擎
功能测试vs.非功能测试:能否非此即彼地进行选择?
Copper lock password library
商业版SSL证书
glut库更新旧程序无法完成编译问题描述
HUAWEI CLOUD DevCloud received the highest-level certification of the first batch of cloud-native technology architecture maturity assessments by the China Academy of Information and Communications Te
app自动化测试webview怎么操作
面了个腾讯25k+出来的,他让我见识到什么基础的天花板
Shanxi: 1 death occurred in a coal mine safety accidents was ordered to halt production
MySQL数据库命令
快速申请代码签名证书方法
常用持续集成工具对比
Spike project harvest
String compression (3) short string compression
并发容器线程安全应对之道
Taurus.MVC WebAPI 入门开发教程4:控制器方法及参数定义、获取及基础校验属性【Require】。
h5开容器,新页面返回遇到的问题
解决mpi4py导入报错ImportError: libmpi.so.40: cannot open shared object file: No such file or directory
cmake tips record