php-captcha翻译到java的测试
php源文件地址:https://github.com/lifei6671/php-captcha/blob/master/src/CaptchaBuilder.php
port到java后的测试结果示例:
翻译后的java版本代码:
class CaptchaBuilder { /** * @var resource 验证码图片 */ protected Mat image; /** * @var string 验证码文字 */ protected String text ; /** * @var string 随机字符 */ protected char[] characters = "2346789abcdefghjmnpqrtuxyzABCDEFGHJMNPQRTUXYZ".toCharArray(); /** * @var int 图片宽度 */ protected int width = 150; /** * @var int 图片高度 */ protected int height = 40; private String[] fonts = new String[5]; /** * @var int 验证码字符的个数 */ private int number = 4; /** * @var int 字体大小 */ private int fontSize = 24; /** * @var string 验证码字体 */ private String textFont; private int noiseLevel = 30; private Scalar backColor; /** * @var bool 是否添加干扰线 */ private boolean isDrawLine = false; /** * @var bool 是否启用曲线 */ private boolean isDrawCurve = true; /** * @var bool 是否启用背景噪音 */ private boolean isDrawNoise = true; public CaptchaBuilder() { this.initialize(new HashMap<>()); } public void initialize(Map<String,String> config){ if(config.containsKey("width")) { this.width = Integer.valueOf(config.get("width")); } this.height = config.containsKey("height") ? Integer.valueOf(config.get("height")) : 40; this.number = config.containsKey("number") ? Integer.valueOf(config.get("number")) : 4; this.fontSize = (int)(this.width / (float)(this.number*1.5)); if(config.containsKey("line")) { this.isDrawLine = Boolean.parseBoolean(config.get("line")); } if(config.containsKey("curve")) { this.isDrawCurve = Boolean.parseBoolean(config.get("curve")); } if(config.containsKey("noise")) { this.isDrawNoise = Boolean.parseBoolean(config.get("noise")); } if(config.containsKey("fonts") && config.get("fonts").isEmpty() == false){ this.fonts = config.get("fonts").split(","); }else { // } this.noiseLevel = this.width*10 / this.height; } private void putRotateText(Mat img, String text, Point org,double angle,int fontFace, double fontScale, Scalar color, int thickness ){ Mat text_image_color = Mat.zeros((int)(img.rows() * 1.2), (int)(img.cols()*1.2), img.type()); Mat text_image_white = Mat.zeros((int)(img.rows() * 1.2), (int)(img.cols()*1.2), img.type()); Core.putText(text_image_color , text, org, fontFace, fontScale, color,thickness); Core.putText(text_image_white, text, org, fontFace, fontScale, new Scalar(255,255,255),thickness); Mat rotationMatrix = Imgproc.getRotationMatrix2D(org, angle, 1.0); Imgproc.warpAffine(text_image_color, text_image_color, rotationMatrix,new Size(img.width(),img.height())); Imgproc.warpAffine(text_image_white, text_image_white, rotationMatrix,new Size(img.width(),img.height())); Core.subtract(img,text_image_white,img); Core.add(img, text_image_color,img); } public static <T extends Object> int array_rand(T[] array) { int rnd = ThreadLocalRandom.current().nextInt(array.length); return rnd; } public CaptchaBuilder create() throws Exception { this.image = new Mat(this.height, this.width, CvType.CV_32FC3); this.backColor = this.getLightColor(); this.image.setTo(this.backColor); if (0==this.fonts.length) { throw new Exception("字体不存在"); } this.textFont = this.fonts[array_rand(this.fonts)]; if(this.isDrawNoise) this.drawNoise(); if(this.isDrawLine){ int square = this.width * this.height; double effects = mt_rand(square/3000, square/2000); for (int e = 0; e < effects; e++) { this.drawLine(this.image, this.width, this.height); } } if(this.isDrawCurve==true) this.drawSineLine(); double codeNX = 0; char[] code = new char[this.number]; for (int i = 0; i < this.number; i++) { code[i] = this.characters[mt_rand(0, this.characters.length - 1)]; codeNX += mt_rand(this.fontSize * 1, this.fontSize * 1.3); Scalar color = this.getDeepColor(); putRotateText(this.image,Character.toString(code[i]),new Point((int)codeNX, (int)(this.fontSize * 1.2)),mt_rand(-40, 40),Core.FONT_HERSHEY_SIMPLEX,1,color,4); } this.text = (new String(code)).toLowerCase(); return this; } public void save(String filename, int quality) { Highgui.imwrite(filename, this.image); } public String getText() { return this.text; } private Scalar getFontColor() { Scalar dc = this.getDeepColor(); return dc; } protected void drawSineLine() { double px = 0,py = 0; double A = mt_rand(1, this.height/2); double b = mt_rand(-this.height/4, this.height/4); double f = mt_rand(-this.height/4, this.height/4); double T = mt_rand(this.height, this.width*2); double w = ((2* Math.PI)/T); double px1 = 0; double px2 = mt_rand(this.width/2, this.width * 0.8); Scalar color = new Scalar(mt_rand(1, 150), mt_rand(1, 150), mt_rand(1, 150)); for (px=px1; px<=px2; px = px + 1) { if (w!=0) { py = (A * Math.sin(w*px + f)+ b + this.height/2); float i = (this.fontSize/5); while (i > 0) { Core.circle(this.image,new Point((int)(px + i) , (int)(py + i)),1,color,1); i--; } } } A = mt_rand(1, this.height/2); f = mt_rand(-this.height/4, this.height/4); T = mt_rand(this.height, this.width*2); w = ((2* Math.PI)/T); b = (py - A * Math.sin(w*px + f) - this.height/2); px1 = px2; px2 = this.width; for (px=px1; px<=px2; px=px+ 1) { if (w!=0) { py = (A * Math.sin(w*px + f)+ b + this.height/2); float i = (this.fontSize/5); while (i > 0) { Core.circle(this.image,new Point((int)(px + i), (int)(py + i)),1,color,1); i--; } } } } protected void drawLine(Mat image,int width,int height) { drawLine(image, width, height,null); } protected void drawLine(Mat image,int width,int height,Scalar tcol) { if (tcol == null) { tcol = new Scalar(mt_rand(100, 255), mt_rand(100, 255), mt_rand(100, 255)); } int Xa,Ya,Xb,Yb; if (mt_rand(0, 1)!=0) { Xa = mt_rand(0, width/2); Ya = mt_rand(0, height); Xb = mt_rand(width/2, width); Yb = mt_rand(0, height); } else { Xa = mt_rand(0, width); Ya = mt_rand(0, height/2); Xb = mt_rand(0, width); Yb = mt_rand(height/2, height); } Core.line(image,new Point(Xa,Ya),new Point(Xb,Yb),tcol,mt_rand(1, 3)); } private void drawNoise() { String codeSet = "2345678abcdefhijkmnpqrstuvwxyz"; for(int i = 0; i < this.noiseLevel; i++){ Scalar noiseColor = this.getLightColor(); for(int j = 0; j < 5; j++) { Core.putText (this.image,Character.toString(codeSet.charAt(mt_rand(0, 29))),new Point(mt_rand(-10, this.width), mt_rand(-10, this.height)),Core.FONT_HERSHEY_SIMPLEX ,1,noiseColor,2); } } } private Scalar getLightColor() { return new Scalar(200 + mt_rand(1,55),200 + mt_rand(1,55),200 + mt_rand(1,55)); } private Scalar getRandColor() { int red = mt_rand(1,254); int green = mt_rand(1,254); int blue; if(red + green > 400){ blue = 0; }else{ blue = 400 -green - red; } return new Scalar(red,green,blue); } private int mt_rand(int min,int max) { int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1); return randomNum; } private double mt_rand(double min,double max) { double randomNum = ThreadLocalRandom.current().nextDouble(min, max + 1); return randomNum; } private Scalar getDeepColor() { Scalar rd = this.getRandColor(); int increase = 30 + mt_rand(1,254); int red = Math.abs(Math.min(255,(int)rd.val[0] - increase)); int green = Math.abs(Math.min(255,(int)rd.val[1] - increase)); int blue = Math.abs(Math.min(255,(int)rd.val[2] - increase)); return new Scalar(red,green,blue); } public static void main(String[] argv) { //load();--->Load Opencv nactive dll CaptchaBuilder ca = new CaptchaBuilder(); try { ca.create(); } catch (Exception e) { e.printStackTrace(); } ca.save("test.png", 1); } }
参考:
java - Convert OpenCV Mat object to BufferedImage - Stack Overflow
How to put rotated text in OpenCV ? - OpenCV Q&A Forum
Drawing a Circle - Tutorialspoint
php笔记---画图 - LUAOHAN的专栏 - CSDN博客
OpenCV - Adding Text - Tutorialspoint
OpenCV - Drawing a Line - Tutorialspoint
PHP 分配、取消图像颜色 imagecolorallocate 与 imagecolordeallocate 函数(六) - Sunny - CSDN博客
Generating Random Numbers In Different Programming Languages | Go4Expert
How do I generate random integers within a specific range in Java? - Stack Overflow
java - How to randomly pick an element from an array - Stack Overflow
- 点赞
- 收藏
- 关注作者
评论(0)