|
@@ -0,0 +1,117 @@
|
|
|
|
+package cn.com.qmth.examcloud.core.basic.base.util;
|
|
|
|
+
|
|
|
|
+import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
|
+
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
|
+import javax.script.ScriptEngine;
|
|
|
|
+import javax.script.ScriptEngineManager;
|
|
|
|
+import java.awt.*;
|
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.Serializable;
|
|
|
|
+import java.util.Random;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 数字公式验证码
|
|
|
|
+ */
|
|
|
|
+public class VerifyCode {
|
|
|
|
+
|
|
|
|
+ private static final char[] OPS = new char[]{'+', '-', '*'};
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 生成验证码图片
|
|
|
|
+ */
|
|
|
|
+ public static Result generateVerifyCode() {
|
|
|
|
+ int width = 80;
|
|
|
|
+ int height = 32;
|
|
|
|
+
|
|
|
|
+ BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
|
+
|
|
|
|
+ Graphics graphics = image.getGraphics();// 画笔
|
|
|
|
+ graphics.setColor(new Color(0xDCDCDC));// 背景色
|
|
|
|
+ graphics.fillRect(0, 0, width, height);
|
|
|
|
+
|
|
|
|
+ graphics.setColor(Color.black);// 边框色
|
|
|
|
+ graphics.drawRect(0, 0, width - 1, height - 1);
|
|
|
|
+
|
|
|
|
+ // 生成N个干扰点
|
|
|
|
+ Random random = new Random();
|
|
|
|
+ for (int i = 0; i < 50; i++) {
|
|
|
|
+ int x = random.nextInt(width);
|
|
|
|
+ int y = random.nextInt(height);
|
|
|
|
+ graphics.drawOval(x, y, 0, 0);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 随机生成三个数字、并组合成一个数学公式
|
|
|
|
+ String verifyCode = new StringBuilder()
|
|
|
|
+ .append(random.nextInt(10))
|
|
|
|
+ .append(OPS[random.nextInt(3)])
|
|
|
|
+ .append(random.nextInt(10))
|
|
|
|
+ .append(OPS[random.nextInt(3)])
|
|
|
|
+ .append(random.nextInt(10))
|
|
|
|
+ .toString();
|
|
|
|
+
|
|
|
|
+ graphics.setColor(new Color(0, 100, 0));
|
|
|
|
+ graphics.setFont(new Font("Candara", Font.BOLD, 24));
|
|
|
|
+ graphics.drawString(verifyCode, 8, 24);
|
|
|
|
+ graphics.dispose();
|
|
|
|
+
|
|
|
|
+ Integer result;
|
|
|
|
+ try {
|
|
|
|
+ // 将验证码的字符串算出结果
|
|
|
|
+ ScriptEngineManager manager = new ScriptEngineManager();
|
|
|
|
+ ScriptEngine engine = manager.getEngineByName("JavaScript");
|
|
|
|
+ result = (Integer) engine.eval(verifyCode);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new StatusException("500", "验证码图片生成失败!", e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return new Result(image, result);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static byte[] imageToBytes(BufferedImage image) {
|
|
|
|
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
|
+ try {
|
|
|
|
+ ImageIO.write(image, "JPEG", out);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new StatusException("500", "验证码图片处理失败!", e);
|
|
|
|
+ }
|
|
|
|
+ return out.toByteArray();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static class Result implements Serializable {
|
|
|
|
+
|
|
|
|
+ private static final long serialVersionUID = 1L;
|
|
|
|
+
|
|
|
|
+ private BufferedImage image;// 验证码图片
|
|
|
|
+
|
|
|
|
+ private Integer result;// 验证码结果
|
|
|
|
+
|
|
|
|
+ public Result(BufferedImage image, Integer result) {
|
|
|
|
+ this.image = image;
|
|
|
|
+ this.result = result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Result() {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public BufferedImage getImage() {
|
|
|
|
+ return image;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void setImage(BufferedImage image) {
|
|
|
|
+ this.image = image;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Integer getResult() {
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void setResult(Integer result) {
|
|
|
|
+ this.result = result;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|