Outil de code de vérification de génération Java pour votre référence. Le contenu spécifique est le suivant
Package com.demo.utils; importer java.awt.basicStroke; import java.awt.color; import java.awt.font; import java.awt.graphics2d; import java.awt.rederinghints; import java.awt.geom.quadcurve2d; import java.awt.image.buffremedImage; java.io.fileOutputStream; Importer java.io.ioException; import java.io.outputStream; import java.util.random; import javax.imageio.imageio; / ** * générer du code de vérification * @Author dongyangyang * @date 2017/3/13 12:31 * @version 1.0 * / public Class Authcodes {Private Final Random random random = nouveau aléatoire (); // Style de police aléatoire privé final static int [] fontstyle = {font.hanging_baseline, font.italic, font.layout_left_to_right, font.layout_no_limit_contex Font.roman_baseline, font.truetype_font, font.type1_font, font.bold}; chaîne statique finale privée [] font = {"Ravie", "Forte", "Arial", "Courier"}; Final statique privé int [] xarr = {23, 24, 19, 20, 21}; Private Final Static int [] yarr = {20, 21, 22, 23, 24, 25, 16, 17, 18}; private final static int [] font_size = {28, 29, 30, 21, 22, 23, 24, 27, 26, 25}; / ** * Draw Diagram de code aléatoire * @param out * @param width * @param height * @throws ioException * / public static void draw (outputStream out, string value) lève ioException {int width = 100, height = 40; BufferedImage BI = new BufferedImage (largeur, hauteur, bufferedImage.type_int_rgb); Graphics2d g = (graphics2d) bi.getGraphics (); g.setRenderingHint (RenderingHint.Key_antialiasing, RenderingHint.value_antialias_on); G.setColor (Randcolor (240, 255)); G.Fillrect (0, 0, largeur, hauteur); G.Drawrect (1, 1, largeur - 2, hauteur - 2); for (int i = 0; i <10; i ++) {g.setColor (randColor (150, 250)); G.Drawoval (Random.Nextint (110), Random.Nextint (24), 5 + Random.Nextint (10), 5 + Random.Nextint (10)); } g.setfont (getFont ()); g.setColor (randcolor (xarr [(int) (math.random () * 5)], 254)); pour (int i = 0, len = value.length (); i <len; i ++) {string rand = string.valueof (value.charat (i)); Int degré = Random.Nextint (23); if (i% 2 == 0) {degré = degré * (-1); } int x = xarr [(int) (math.random () * 5)] * i, y = yarr [(int) (math.random () * 8)]; g.rotate (math.toradians (degré), x, y); g.setColor (Randcolor (48, 254)); G.Drawstring (Rand, x + 8, y + 10); g.rotate (-math.toradians (degré), x, y); } // La ligne médiane de l'image g.setColor (Randcolor (0, 200)); // La largeur est la largeur de ligne, le type de flotteur BasicStroke BS = new BasicStroke (2); G.SetStroke (BS); // dessine la courbe quadcurve2d.double Curve = new Quadcurve2d.Double (0d, random.nextint (hauteur - 8) + 4, largeur / 2, hauteur / 2, largeur, random.nextint (hauteur - 8) + 4); G.Draw (courbe); // détruire l'image g.dispose (); Imageo.write (bi, "png", out); } Private Static Font getFont () {return new Font (font [(int) (math.random () * 4)], Fontstyle [(int) (math.random () * 12)], font_size [(int) (math.random () * 10)]); } Couleur statique privée RandColor (int fc, int bc) {// Obtenez une couleur aléatoire étant donné une plage si (fc> 255) {fc = 255; } if (bc> 255) {bc = 255; } int r = fc + random.nextint (bc - fc); int g = fc + random.nextint (bc - fc); int b = fc + random.nextint (bc - fc); retourner une nouvelle couleur (r, g, b); } char statique privé [] c = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'm', `` n ',' p ',' '', 'r', 's', ',', ',', ',', ',' x '' «C», «d», «e», «f», «g», «h», «j», «k», «l», «m», «n», «p», «q», «r», «s», «t», `` u »,« »,« 5 », ',', ',', ',', '3', '5' ',' ',' ',' ',', '3', '5 public static String getRandom (int size) {StringBuilder sb = new StringBuilder (); for (int i = 0; i <size; i ++) {sb.append (c [math.abs (random.nextint ())% c.length]); } return sb.toString (); } public static void main (string [] args) lève ioException {for (int i = 1; i <10; i ++) {fileoutputStream out = new FileOutputStream ("d: // bb" + i + ". png"); Dessiner (, getrandom (4)); }}}Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.