前言
本文教大家如何自定义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函数。