From 67409112a73912a308bdb967001e037f9f08f9b3 Mon Sep 17 00:00:00 2001 From: synchronized Date: Fri, 27 Jul 2018 15:11:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=AA=8C=E8=AF=81=E7=A0=81?= =?UTF-8?q?=E7=9A=84=E6=95=88=E6=9E=9C=E5=92=8C=E5=B9=B2=E6=89=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 158 +++++++++--------- src/main/java/com/wf/captcha/Captcha.java | 6 +- src/main/java/com/wf/captcha/Encoder.java | 50 ++++++ src/main/java/com/wf/captcha/GifCaptcha.java | 23 ++- src/main/java/com/wf/captcha/GifEncoder.java | 25 ++- src/main/java/com/wf/captcha/SpecCaptcha.java | 36 ++-- .../com/wf/captcha/utils/CaptchaUtil.java | 30 ++-- src/test/java/com/wf/captcha/CaptchaTest.java | 4 +- 8 files changed, 204 insertions(+), 128 deletions(-) diff --git a/pom.xml b/pom.xml index bb293b9..4b72ef7 100644 --- a/pom.xml +++ b/pom.xml @@ -20,12 +20,6 @@ - - org.sonatype.oss - oss-parent - 7 - - @@ -50,88 +44,86 @@ 6 6 + UTF-8 + - - org.apache.maven.plugins - maven-source-plugin - 2.2.1 - - - package - - jar-no-fork - - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - - package - - jar - - - - - - org.apache.maven.plugins - maven-gpg-plugin - 1.5 - - - sign-artifacts - verify - - sign - - - - - - - - + + + release + + + <!– Source –> + + org.apache.maven.plugins + maven-source-plugin + 2.2.1 + + + attach-sources + + jar-no-fork + + + + + <!– Javadoc –> + + org.apache.maven.plugins + maven-javadoc-plugin + 2.9.1 + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.5 + + + sign-artifacts + verify + + sign + + + + + + + + --> \ No newline at end of file diff --git a/src/main/java/com/wf/captcha/Captcha.java b/src/main/java/com/wf/captcha/Captcha.java index 8618e16..fd30d31 100644 --- a/src/main/java/com/wf/captcha/Captcha.java +++ b/src/main/java/com/wf/captcha/Captcha.java @@ -9,10 +9,10 @@ import java.io.OutputStream; * Created by 王帆 on 2018-07-27 上午 10:08. */ public abstract class Captcha extends Randoms { - protected Font font = new Font("Verdana", Font.ITALIC | Font.BOLD, 28); // 字体 + protected Font font = new Font("Verdana", Font.PLAIN, 32); // 字体 protected int len = 5; // 验证码随机字符长度 - protected int width = 150; // 验证码显示宽度 - protected int height = 40; // 验证码显示高度 + protected int width = 130; // 验证码显示宽度 + protected int height = 48; // 验证码显示高度 private String chars = null; // 当前验证码 /** diff --git a/src/main/java/com/wf/captcha/Encoder.java b/src/main/java/com/wf/captcha/Encoder.java index 70cf4bd..177f167 100644 --- a/src/main/java/com/wf/captcha/Encoder.java +++ b/src/main/java/com/wf/captcha/Encoder.java @@ -97,6 +97,13 @@ public class Encoder { byte[] accum = new byte[256]; //---------------------------------------------------------------------------- + + /** + * @param width 宽度 + * @param height 高度 + * @param pixels 像素 + * @param color_depth 颜色 + */ Encoder(int width, int height, byte[] pixels, int color_depth) { imgW = width; imgH = height; @@ -106,6 +113,12 @@ public class Encoder { // Add a character to the end of the current packet, and if it is 254 // characters, flush the packet to disk. + + /** + * @param c 字节 + * @param outs 输出流 + * @throws IOException IO异常 + */ void char_out(byte c, OutputStream outs) throws IOException { accum[a_count++] = c; if (a_count >= 254) @@ -115,6 +128,11 @@ public class Encoder { // Clear out the hash table // table clear for block compress + + /** + * @param outs 输出流 + * @throws IOException IO异常 + */ void cl_block(OutputStream outs) throws IOException { cl_hash(hsize); free_ent = ClearCode + 2; @@ -124,11 +142,20 @@ public class Encoder { } // reset code table + + /** + * @param hsize int + */ void cl_hash(int hsize) { for (int i = 0; i < hsize; ++i) htab[i] = -1; } + /** + * @param init_bits int + * @param outs 输出流 + * @throws IOException IO异常 + */ void compress(int init_bits, OutputStream outs) throws IOException { int fcode; int i /* = 0 */; @@ -201,6 +228,11 @@ public class Encoder { } //---------------------------------------------------------------------------- + + /** + * @param os 输出流 + * @throws IOException IO异常 + */ void encode(OutputStream os) throws IOException { os.write(initCodeSize); // write "initial code size" byte @@ -213,6 +245,11 @@ public class Encoder { } // Flush the packet to disk, and reset the accumulator + + /** + * @param outs 输出流 + * @throws IOException IO异常 + */ void flush_char(OutputStream outs) throws IOException { if (a_count > 0) { outs.write(a_count); @@ -221,6 +258,10 @@ public class Encoder { } } + /** + * @param n_bits int + * @return int + */ final int MAXCODE(int n_bits) { return (1 << n_bits) - 1; } @@ -228,6 +269,10 @@ public class Encoder { //---------------------------------------------------------------------------- // Return the next pixel from the image //---------------------------------------------------------------------------- + + /** + * @return int + */ private int nextPixel() { if (remaining == 0) return EOF; @@ -239,6 +284,11 @@ public class Encoder { return pix & 0xff; } + /** + * @param code int + * @param outs 输出流 + * @throws IOException IO异常 + */ void output(int code, OutputStream outs) throws IOException { cur_accum &= masks[cur_bits]; diff --git a/src/main/java/com/wf/captcha/GifCaptcha.java b/src/main/java/com/wf/captcha/GifCaptcha.java index 4962194..c9673d2 100644 --- a/src/main/java/com/wf/captcha/GifCaptcha.java +++ b/src/main/java/com/wf/captcha/GifCaptcha.java @@ -1,9 +1,6 @@ package com.wf.captcha; -import java.awt.AlphaComposite; -import java.awt.Color; -import java.awt.Font; -import java.awt.Graphics2D; +import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; @@ -82,12 +79,26 @@ public class GifCaptcha extends Captcha { int h = height - ((height - font.getSize()) >> 1); int w = width / len; g2d.setFont(font); + // 随机画干扰线 + for (int i = 0; i < 8; i++) { + int x1 = num(-10, width - 10); + int y1 = num(5, height - 5); + int x2 = num(10, width + 10); + int y2 = num(2, height - 2); + g2d.setColor(color(150, 250)); + g2d.setStroke(new BasicStroke(1.3f)); + g2d.drawLine(x1, y1, x2, y2); + // 画干扰圆圈 + g2d.setColor(color(100, 250)); + g2d.setStroke(new BasicStroke(1.0f)); + g2d.drawOval(num(width), num(height), 5 + num(10), 5 + num(10)); + } + // 画验证码 for (int i = 0; i < len; i++) { ac3 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getAlpha(flag, i)); g2d.setComposite(ac3); g2d.setColor(fontcolor[i]); - g2d.drawOval(num(width), num(height), 5 + num(10), 5 + num(10)); - g2d.drawString(String.valueOf(strs[i]), (width - (len - i) * w) + (w - font.getSize()) + 1, h - 4); + g2d.drawString(String.valueOf(strs[i]), (width - (len - i) * w) + (w - font.getSize()) + num(7, 11), h - num(2, 6)); } g2d.dispose(); return image; diff --git a/src/main/java/com/wf/captcha/GifEncoder.java b/src/main/java/com/wf/captcha/GifEncoder.java index 8a1f4c9..5636e33 100644 --- a/src/main/java/com/wf/captcha/GifEncoder.java +++ b/src/main/java/com/wf/captcha/GifEncoder.java @@ -79,7 +79,6 @@ public class GifEncoder { * image is added. * * @param iter int number of iterations. - * @return */ public void setRepeat(int iter) { if (iter >= 0) { @@ -168,6 +167,8 @@ public class GifEncoder { * Flushes any pending data and closes output file. * If writing to an OutputStream, the stream is not * closed. + * + * @return boolean */ public boolean finish() { if (!started) return false; @@ -219,7 +220,6 @@ public class GifEncoder { * than 20 do not yield significant improvements in speed. * * @param quality int greater than 0. - * @return */ public void setQuality(int quality) { if (quality < 1) quality = 1; @@ -319,6 +319,9 @@ public class GifEncoder { /** * Returns index of palette color closest to c + * + * @param c color + * @return int */ protected int findClosest(Color c) { if (colorTab == null) return -1; @@ -365,6 +368,8 @@ public class GifEncoder { /** * Writes Graphic Control Extension + * + * @throws IOException IO异常 */ protected void writeGraphicCtrlExt() throws IOException { out.write(0x21); // extension introducer @@ -396,6 +401,8 @@ public class GifEncoder { /** * Writes Image Descriptor + * + * @throws IOException IO异常 */ protected void writeImageDesc() throws IOException { out.write(0x2c); // image separator @@ -419,6 +426,8 @@ public class GifEncoder { /** * Writes Logical Screen Descriptor + * + * @throws IOException IO异常 */ protected void writeLSD() throws IOException { // logical screen size @@ -437,6 +446,8 @@ public class GifEncoder { /** * Writes Netscape application extension to define * repeat count. + * + * @throws IOException IO异常 */ protected void writeNetscapeExt() throws IOException { out.write(0x21); // extension introducer @@ -451,6 +462,8 @@ public class GifEncoder { /** * Writes color table + * + * @throws IOException IO异常 */ protected void writePalette() throws IOException { out.write(colorTab, 0, colorTab.length); @@ -462,6 +475,8 @@ public class GifEncoder { /** * Encodes and writes pixel data + * + * @throws IOException IO异常 */ protected void writePixels() throws IOException { Encoder encoder = new Encoder(width, height, indexedPixels, colorDepth); @@ -470,6 +485,9 @@ public class GifEncoder { /** * Write 16-bit value to output stream, LSB first + * + * @param value int + * @throws IOException IO异常 */ protected void writeShort(int value) throws IOException { out.write(value & 0xff); @@ -478,6 +496,9 @@ public class GifEncoder { /** * Writes string to output stream + * + * @param s string + * @throws IOException IO异常 */ protected void writeString(String s) throws IOException { for (int i = 0; i < s.length(); i++) { diff --git a/src/main/java/com/wf/captcha/SpecCaptcha.java b/src/main/java/com/wf/captcha/SpecCaptcha.java index 4899dad..eb78941 100644 --- a/src/main/java/com/wf/captcha/SpecCaptcha.java +++ b/src/main/java/com/wf/captcha/SpecCaptcha.java @@ -1,9 +1,6 @@ package com.wf.captcha; -import java.awt.AlphaComposite; -import java.awt.Color; -import java.awt.Font; -import java.awt.Graphics2D; +import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; @@ -59,30 +56,31 @@ public class SpecCaptcha extends Captcha { BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = (Graphics2D) bi.getGraphics(); AlphaComposite ac3; - Color color; int len = strs.length; g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); - // 随机画干扰的圆圈 - for (int i = 0; i < 15; i++) { - color = color(150, 250); - g.setColor(color); - g.drawOval(num(width), num(height), 5 + num(10), 5 + num(10)); - color = null; + // 随机画干扰线 + for (int i = 0; i < 12; i++) { + int x1 = num(-10, width - 10); + int y1 = num(5, height - 5); + int x2 = num(10, width + 10); + int y2 = num(2, height - 2); + g.setColor(color(150, 250)); + g.setStroke(new BasicStroke(1.3f)); + g.drawLine(x1, y1, x2, y2); + // 画干扰圆圈 + g.setColor(color(100, 250)); + g.drawOval(num(width), num(height), 5 + num(25), 5 + num(25)); } - g.setFont(font); + g.setFont(new Font(font.getFontName(), Font.ITALIC, font.getSize())); int h = height - ((height - font.getSize()) >> 1); int w = width / len; - int size = w - font.getSize() + 1; // 画字符串 for (int i = 0; i < len; i++) { - ac3 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f);// 指定透明度 + ac3 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.75f);// 指定透明度 g.setComposite(ac3); - color = new Color(20 + num(110), 20 + num(110), 20 + num(110));// 对每个字符都用随机颜色 - g.setColor(color); - g.drawString(String.valueOf(strs[i]), (width - (len - i) * w) + size, h - 4); - color = null; - ac3 = null; + g.setColor(new Color(20 + num(110), 20 + num(110), 20 + num(110))); + g.drawString(String.valueOf(strs[i]), (width - (len - i) * w) + (w - font.getSize()) + num(7, 11), h - num(2, 6)); } ImageIO.write(bi, "png", out); out.flush(); diff --git a/src/main/java/com/wf/captcha/utils/CaptchaUtil.java b/src/main/java/com/wf/captcha/utils/CaptchaUtil.java index 23cb243..118da9e 100644 --- a/src/main/java/com/wf/captcha/utils/CaptchaUtil.java +++ b/src/main/java/com/wf/captcha/utils/CaptchaUtil.java @@ -17,6 +17,10 @@ public class CaptchaUtil { /** * 验证验证码 + * + * @param code 用户输入的验证码 + * @param request HttpServletRequest + * @return 是否正确 */ public static boolean ver(String code, HttpServletRequest request) { if (code != null && !code.trim().isEmpty()) { @@ -29,26 +33,26 @@ public class CaptchaUtil { /** * 输出验证码 * - * @param request - * @param response - * @throws IOException + * @param request HttpServletRequest + * @param response HttpServletResponse + * @throws IOException IO异常 */ public static void out(HttpServletRequest request, HttpServletResponse response) throws IOException { - out(130, 38, 5, request, response); + out(5, request, response); } /** * 输出验证码 * * @param len 长度 - * @param request - * @param response - * @throws IOException + * @param request HttpServletRequest + * @param response HttpServletResponse + * @throws IOException IO异常 */ public static void out(int len, HttpServletRequest request, HttpServletResponse response) throws IOException { - out(130, 38, len, request, response); + out(130, 48, len, request, response); } /** @@ -57,14 +61,14 @@ public class CaptchaUtil { * @param width 宽度 * @param height 高度 * @param len 长度 - * @param request - * @param response - * @throws IOException + * @param request HttpServletRequest + * @param response HttpServletResponse + * @throws IOException IO异常 */ public static void out(int width, int height, int len, HttpServletRequest request, HttpServletResponse response) throws IOException { setHeader(response); - Captcha captcha = new GifCaptcha(130, 38, 5); + Captcha captcha = new GifCaptcha(width, height, len); request.getSession().setAttribute(SESSION_KEY, captcha.text().toLowerCase()); captcha.out(response.getOutputStream()); } @@ -72,7 +76,7 @@ public class CaptchaUtil { /** * 设置相应头 * - * @param response + * @param response HttpServletResponse */ private static void setHeader(HttpServletResponse response) { response.setContentType("image/gif"); diff --git a/src/test/java/com/wf/captcha/CaptchaTest.java b/src/test/java/com/wf/captcha/CaptchaTest.java index 1a25a47..071a398 100644 --- a/src/test/java/com/wf/captcha/CaptchaTest.java +++ b/src/test/java/com/wf/captcha/CaptchaTest.java @@ -13,14 +13,14 @@ public class CaptchaTest { @Test public void test() throws Exception { - SpecCaptcha specCaptcha = new SpecCaptcha(150, 40, 4); + SpecCaptcha specCaptcha = new SpecCaptcha(); System.out.println(specCaptcha.text()); specCaptcha.out(new FileOutputStream(new File("D:/a/aa.png"))); } @Test public void testGIf() throws Exception { - GifCaptcha specCaptcha = new GifCaptcha(150, 40, 4); + GifCaptcha specCaptcha = new GifCaptcha(130, 48, 5); System.out.println(specCaptcha.text()); specCaptcha.out(new FileOutputStream(new File("D:/a/aa.gif"))); }