本篇文章主要介绍一些实用的工具,可能工作中会使用到。一个是验证码的, 一个是关系表达式的,一个是开源的OCR识别图片。
开源的ocr框架 tess4j
<dependency> <groupId>net.sourceforge.tess4j</groupId> <artifactId>tess4j</artifactId> <version>4.5.4</version> </dependency>
使用代码,非常简单,识别个简单的验证码图片
import net.sourceforge.tess4j.ITesseract; import net.sourceforge.tess4j.Tesseract; import net.sourceforge.tess4j.TesseractException; import java.io.File; public class Tess4jDemo { public static void main(String[] args) throws TesseractException { final ITesseract instance = new Tesseract(); instance.setDatapath("D:\\tessdata"); instance.setLanguage("chi_sim"); File imageLocation = new File("D:\\verifyImg"); for(File image : imageLocation.listFiles()){ System.out.println(image.getName()+" -->"+instance.doOCR(image)); } } }
<dependency> <groupId>com.ramostear</groupId> <artifactId>Happy-Captcha</artifactId> <version>1.0.1</version> </dependency>
效果图如下
这种验证码防止机器识别还是可以的,有干扰线。
<dependency> <groupId>com.googlecode.aviator</groupId> <artifactId>aviator</artifactId> <version>5.2.6</version> </dependency>
不过今天我要重点讲下google 的aviator
aviator 支持三种类型计算 :boolean, 字符串,数字类型只支持结果为Long
//数字型计算 System.out.println(AviatorEvaluator.execute("1+2+3")); //字符型计算 String exp1 = "'Hello '+ name"; Map<String,Object> env1 = new HashMap<>(); env1.put("name","tony"); System.out.println(AviatorEvaluator.execute(exp1, env1)); //判断型计算 String exp2 = "a+b>c"; Map<String,Object> env2 = new HashMap<>(); env2.put("a",5); env2.put("b",6); env2.put("c",7); System.out.println(AviatorEvaluator.execute(exp2, env2)); //https://blog.csdn.net/qq_27384769/article/details/80639122
运行结果为
6 Hello tony true
生成验证码工具类
import javax.imageio.ImageIO; import javax.imageio.stream.ImageOutputStream; import java.awt.*; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.Random; public class ImageUtil { private String str ;// 验证码 private ByteArrayInputStream image ;//验证码图片 private int width = 350; //图片宽度 private int height = 46; //图片高度 private ImageUtil(){ init(); } public static ImageUtil getInstance(){ return new ImageUtil(); }; private void init(){ this.str= ""; final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); final Graphics g = image.getGraphics(); Random random = new Random(); g.setColor(getRandColor(200,250)); g.fillRect(0,0,width,height); g.setFont(new Font("Times New Roman",Font.PLAIN,20)); g.setColor(getRandColor(160,200)); for(int i = 0 ; i < 150 ; i ++){ int x = random.nextInt(width); int y = random.nextInt(height); int x1 = random.nextInt(50); int y1 = random.nextInt(50); g.drawLine(x,y,x+x1,y+y1); } int num1 = random.nextInt(10); int num2 = random.nextInt(10); g.setColor(getRandColor(20,110)); g.drawString(String.valueOf(num1),(width/5)*0+6,36); g.drawString("+",(width/5)*1+6,36); g.drawString(String.valueOf(num2),(width/5)*2+6,36); g.drawString("=",(width/5)*3+6,36); g.drawString("?",(width/5)*4+6,36); this.str = String.valueOf(num1+num2); g.dispose(); ByteArrayInputStream inputStream = null; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try{ final ImageOutputStream imageOut = ImageIO.createImageOutputStream(outputStream); ImageIO.write(image,"JPEG",imageOut); imageOut.close(); inputStream = new ByteArrayInputStream(outputStream.toByteArray()); }catch (Exception e){ System.out.println("验证码图片生成失败:"+e); } this.image = inputStream; } private Color getRandColor(int fc, int bc) { Random random = new Random(); if(fc > 255) { fc = 255;} if(bc > 255) { bc = 255;} int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r,g,b); } public String getStr() { return str; } public void setStr(String str) { this.str = str; } public ByteArrayInputStream getImage() { return image; } public void setImage(ByteArrayInputStream image) { this.image = image; } }
至此,生成验证码的功能算是比较简单的