import org.apache.commons.codec.binary.Base64; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Random; /** * Created by IntelliJ IDEA * 这是一个神奇的Class * * @author zhz * @date 2020/3/26 13:45 */ public class CheckPicCodeUtil { private static String picCode; public CheckPicCodeUtil() { } public static String getPicCode() { return picCode; } public static void setPicCode(String picCode) { CheckPicCodeUtil.picCode = picCode; } public static String getCheckCode(){ //验证码中的字符由数字和大小写字母组成 String code = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"; Random r = new Random(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < 4; i++) { sb.append(code.charAt(r.nextInt(code.length()))); } return sb.toString(); } public static BufferedImage showPic() throws IOException { String code=getCheckCode(); setPicCode(code); int width=80; int height=30; BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics g=image.getGraphics(); g.setColor(Color.white); g.fillRect(0,0,width,height); Random r=new Random(); for (int i=0;i<3;i++){ g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); g.drawLine(r.nextInt(80),r.nextInt(30),r.nextInt(80),r.nextInt(80)); } g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); g.setFont(new Font("黑体",Font.BOLD,24)); g.drawString(code,15,20); g.dispose();//图像生效 return image; } public static String encodeBase64ImgCode() throws IOException{ BufferedImage codeImg=showPic(); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(codeImg, "JPEG", out); byte[] b = out.toByteArray(); String imgString = Base64.encodeBase64String(b); return "data:image/JPEG;base64," + imgString; } public static void main(String[] args) throws IOException { String base64PicCodeImage = encodeBase64ImgCode(); String checkCode = getPicCode(); System.out.println("验证码==》"+checkCode); System.out.println("base64图片字符串==》"+base64PicCodeImage); } }
验证结果:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <img src="data:image/J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u0aJ9UuLbSJ7ZZi0UjRBtxbG4jBAOFHPQZ6cmuelivdcpO7v917fh5mVPEe63J31/wAju65+71y6g8bWGirHCba4gMjsQd4ID9DnGPlHb1rL8DXUh0STUdUu7pmedo0luJnKbML6naPmBG714z2pdS/5Kto//Xo38pa2lVlKnCa0u1+ZcqjlCMlpdo6+aZYQm4El3CKq9SSf6DJPsDUlVE/0jUJGP3Lb5FH+2QCT+RAB92q3XRFt3ZrGTld9AoooqizG1nQP7X1LSrz7T5X2Cbzdnl7vM5U4zkY+779a2aKKlQim5LdkqKTbXUzJ9DtZ/EFtrTSTC5t4zGigjYQd3UYzn5j39K5vV7C4k8YtPPb39pZvDsS90sMZJCApxJt3HGcgfKCeOoHHb1Tv71tPia5kQPbKBv2/fBzjjseo9MY71lVoxku2tyZUVPRd7mP4dk1d9RvEma5fSYgsds99HsnZgBk9ASp55YZ6d91M1Dw6LfW4NcsZo4Wt4vKjtTCSjM25QBhhtyX7cZ5PU109VJP39+kJ+5ComYf3iSQv1Awx+oU0OHLDlvfX8en3EThaFt309f8AgENnOLSKO1vMRXBPLZOyV2OSVYgDJJPy8HrgYwa0aa6LIjI6hkYEMrDII9DVT7HLbc2Uu1R/ywly0f0U9U7AYyoH8NdKjG1loapcqsj/2Q== "> </body> </html>
参考:https://blog.csdn.net/Solder_Sun/article/details/81363117