图形验证码工具EasyCaptcha,生成的图形验证码都默认带干扰线和圈
在生成的时候,看源码里面是写的默认生成一条线两个圈,但是不想要这个怎么办
看文档和SpecCaptcha类的api都没有去掉干扰线的方法
新建一个类继承SpecCaptcha类,重写out和toBase64方法
out方法里面的代码就是从父类的graphicsImage方法复制过来的,把添加干扰的两行代码删了
再加一个构造方法
import com.wf.captcha.SpecCaptcha; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; /** * @author vhukze * @date 2021/10/12 13:53 */ public class CaptchaUtil extends SpecCaptcha { public CaptchaUtil(int width, int height, int len) { super(width, height, len); } @Override public boolean out(OutputStream out) { char[] strs = super.textChar(); try { BufferedImage bi = new BufferedImage(this.width, this.height, 1); Graphics2D g2d = (Graphics2D) bi.getGraphics(); g2d.setColor(Color.WHITE); g2d.fillRect(0, 0, this.width, this.height); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // this.drawOval(2, g2d); g2d.setStroke(new BasicStroke(2.0F, 0, 2)); // this.drawBesselLine(1, g2d); g2d.setFont(this.getFont()); FontMetrics fontMetrics = g2d.getFontMetrics(); int fW = this.width / strs.length; int fSp = (fW - (int) fontMetrics.getStringBounds("W", g2d).getWidth()) / 2; for (int i = 0; i < strs.length; ++i) { g2d.setColor(this.color()); int fY = this.height - (this.height - (int) fontMetrics.getStringBounds(String.valueOf(strs[i]), g2d).getHeight() >> 1); g2d.drawString(String.valueOf(strs[i]), i * fW + fSp + 3, fY - 3); } g2d.dispose(); ImageIO.write(bi, "png", out); out.flush(); return true; } catch (IOException var18) { var18.printStackTrace(); } finally { try { out.close(); } catch (IOException var17) { var17.printStackTrace(); } } return false; } @Override public String toBase64() { return super.toBase64(); } }
然后还是跟原来一样使用就行