圖片驗證碼簡單示例
1、ImageCode.java
package com.example.entity; import java.awt.image.BufferedImage; /** * Description:圖片驗證碼類 * Package:com.example.entity * * @author lightbc * @version 1.0 */ public class ImageCode { private BufferedImage image; public ImageCode(BufferedImage image){ this.image=image; } public BufferedImage getImage() { return image; } public void setImage(BufferedImage image) { this.image = image; } }
2、ImageCodeUtil.java
package com.example.utils; import com.example.entity.ImageCode; import java.awt.*; import java.awt.image.BufferedImage; import java.util.Random; /** * Description:圖片二維碼處理工具類 * Package:com.example.utils * * @author lightbc * @version 1.0 */ public class ImageCodeUtil { //隨機驗證碼取值范圍 private static String CODE_RANDOM_RANGE = "0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm"; //圖片驗證碼的寬 private static int IMAGE_CODE_WIDTH = 100; //圖片驗證碼的高 private static int IMAGE_CODE_HEIGHT = 50; //驗證碼的長度 private static int IMAGE_CODE_LEN = 6; private Random random = new Random(); /** * 創建圖片驗證碼 * @return imagecode對象 */ public ImageCode createCode(){ String sRandom; BufferedImage image = new BufferedImage(IMAGE_CODE_WIDTH,IMAGE_CODE_HEIGHT,BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); //設置圖片驗證碼背景色為白色 g2.setBackground(Color.white); g2.fillRect(0,0,IMAGE_CODE_WIDTH,IMAGE_CODE_HEIGHT); g2.setFont(new Font("宋體",Font.ITALIC,30)); for(int i=0;i<IMAGE_CODE_LEN;i++){ int randomIndex = random.nextInt(CODE_RANDOM_RANGE.length()); sRandom=String.valueOf(CODE_RANDOM_RANGE.charAt(randomIndex)); g2.setColor(createRandomColor(0,100)); g2.drawString(sRandom,15*i+5,35); } //在圖片驗證碼中間划條橫線 g2.drawLine(0,IMAGE_CODE_HEIGHT/2,IMAGE_CODE_WIDTH,IMAGE_CODE_HEIGHT/2); return new ImageCode(image); } /** * 生成隨機RGB顏色 * @param bc 最小值 * @param ac 最大值 * @return color */ public Color createRandomColor(int bc,int ac){ int r = ac+random.nextInt(ac-bc); int g = ac+random.nextInt(ac-bc); int b = ac+random.nextInt(ac-bc); return new Color(r,g,b); } }
3、ImageCodeController.java
package com.example.controller; import com.example.entity.ImageCode; import com.example.utils.ImageCodeUtil; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * Description:圖片驗證碼控制器 * Package:com.example.controller * * @author lightbc * @version 1.0 */ @Controller @RequestMapping("/auth/ic") public class ImageCodeController { @GetMapping(value = "/code/index") public String codeIndex(){ return "code"; } @GetMapping(value = "/get/image/code") @ResponseBody public void getImageCode(HttpServletRequest request, HttpServletResponse response) throws IOException { ImageCode imageCode = new ImageCodeUtil().createCode(); ImageIO.write(imageCode.getImage(),"JPEG",response.getOutputStream()); } }
4、效果展示