| | |
| | | package com.yeshi.fanli.util; |
| | | |
| | | import java.awt.Color; |
| | | import java.awt.Font; |
| | | import java.awt.Graphics; |
| | | import java.awt.Graphics2D; |
| | | import java.awt.RenderingHints; |
| | | import java.awt.geom.AffineTransform; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.OutputStream; |
| | | import java.util.Arrays; |
| | | import java.util.Random; |
| | | import sun.misc.BASE64Encoder; |
| | | |
| | | import javax.imageio.ImageIO; |
| | | import java.awt.*; |
| | | import java.awt.geom.AffineTransform; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.*; |
| | | import java.util.Arrays; |
| | | import java.util.Random; |
| | | |
| | | public class VerifyCodeUtil { |
| | | // 使用到Algerian字体,系统里没有的话安装字体,字体只显示大写,去掉了1,0,i,o几个容易混淆的字 |
| | |
| | | /** |
| | | * 使用系统默认字符源生成验证码 |
| | | * |
| | | * @param verifySize |
| | | * 验证码长度 |
| | | * @param verifySize 验证码长度 |
| | | * @return |
| | | */ |
| | | public static String generateVerifyCode(int verifySize) { |
| | |
| | | /** |
| | | * 使用指定源生成验证码 |
| | | * |
| | | * @param verifySize |
| | | * 验证码长度 |
| | | * @param sources |
| | | * 验证码字符源 |
| | | * @param verifySize 验证码长度 |
| | | * @param sources 验证码字符源 |
| | | * @return |
| | | */ |
| | | public static String generateVerifyCode(int verifySize, String sources) { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 输出指定验证码图片流 |
| | | * |
| | | * @param w |
| | | * @param h |
| | | * @param os |
| | | * @param code |
| | | * @throws IOException |
| | | */ |
| | | public static void outputImage(int w, int h, OutputStream os, String code) throws IOException { |
| | | private static BufferedImage getVerifyImages(int w, int h, String code) { |
| | | |
| | | int verifySize = code.length(); |
| | | BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); |
| | | Random rand = new Random(); |
| | |
| | | } |
| | | |
| | | g2.dispose(); |
| | | |
| | | return image; |
| | | } |
| | | |
| | | /** |
| | | * 输出指定验证码图片流 |
| | | * |
| | | * @param w |
| | | * @param h |
| | | * @param os |
| | | * @param code |
| | | * @throws IOException |
| | | */ |
| | | public static void outputImage(int w, int h, OutputStream os, String code) throws IOException { |
| | | BufferedImage image = getVerifyImages(w, h, code); |
| | | ImageIO.write(image, "JPEG", os); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取Base64的验证码 |
| | | * |
| | | * @param w |
| | | * @param h |
| | | * @param code |
| | | * @return |
| | | */ |
| | | public static String getVerifyImagesBase64(int w, int h, String code) { |
| | | BufferedImage image = getVerifyImages(w, h, code); |
| | | //输出流 |
| | | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| | | try { |
| | | ImageIO.write(image, "png", stream); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | byte[] bytes = stream.toByteArray();//转换成字节 |
| | | BASE64Encoder encoder = new BASE64Encoder(); |
| | | String base64 = encoder.encodeBuffer(bytes).trim();//转换成base64串 |
| | | base64 = base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n |
| | | return base64; |
| | | } |
| | | |
| | | |
| | | private static Color getRandColor(int fc, int bc) { |
| | | if (fc > 255) |
| | | fc = 255; |