Java tutorial
/** * Copyright (C) 2014-2017, Hrfax and/or its affiliates. All rights reserved. * Hrfax PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * */ package costumetrade.common.verify; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import org.apache.commons.lang3.RandomUtils; import akka.japi.Pair; /** * ??? * @author zhouyq * @Date 2017316 */ public class ImageVerification { // private int width = 160; // private int height = 40; // ?? private int codeCount = 4; // ?? private int lineCount = 150; private static final char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; public ImageVerification() { } /** * * @param width * @param height */ public ImageVerification(int width, int height) { this.width = width; this.height = height; } /** * * @param width * @param height * @param codeCount * @param lineCount ? */ public ImageVerification(int width, int height, int codeCount, int lineCount) { this.width = width; this.height = height; this.codeCount = codeCount; this.lineCount = lineCount; } public Pair<String, BufferedImage> create() { int x = 0, fontHeight = 0, codeY = 0; int red = 0, green = 0, blue = 0; x = width / (codeCount + 2);//? fontHeight = height - 2;// codeY = height - 4; // ?buffer BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = buffImg.createGraphics(); // ? g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); // g.setFont(new Font("Arial", Font.PLAIN, fontHeight)); for (int i = 0; i < lineCount; i++) { int xs = RandomUtils.nextInt(0, width); int ys = RandomUtils.nextInt(0, height); int xe = xs + RandomUtils.nextInt(0, width / 8); int ye = ys + RandomUtils.nextInt(0, height / 8); red = RandomUtils.nextInt(0, 255); green = RandomUtils.nextInt(0, 255); blue = RandomUtils.nextInt(0, 255); g.setColor(new Color(red, green, blue)); g.drawLine(xs, ys, xe, ye); } // randomCode??? StringBuilder randomCode = new StringBuilder(); // ?codeCount?? for (int i = 0; i < codeCount; i++) { String strRand = String.valueOf(codeSequence[RandomUtils.nextInt(0, codeSequence.length)]); // ???? red = RandomUtils.nextInt(0, 255); green = RandomUtils.nextInt(0, 255); blue = RandomUtils.nextInt(0, 255); g.setColor(new Color(red, green, blue)); g.drawString(strRand, (i + 1) * x, codeY); // ?? randomCode.append(strRand); } // ????Session return new Pair<String, BufferedImage>(randomCode.toString(), buffImg); } }