この記事の例は、参照のためのJava Generation Letter Verification Codeの特定のコードを共有しています。特定のコンテンツは次のとおりです
java.awt.basicstroke; import java.awt.color; import java.awt.font; import java.awt.graphics2d; Import java.awt.image.bufferedimage; Import java.io.ioexception; Impont java.utputstream; import java.util.util.util.util.util.util.util.util.util.util.util.util.util.util.util.utiliio.utlage.utputstream; class verifyCode {//画像幅と高さプライベートint w = 70; private int h = 35;プライベートランダムr = new Random(); private string [] fontnames = {"song font"、 "chishing kai font"、 "bold font"、 "microsoft elegant black"、 "kai font_gb2312"};プライベート文字列codes = "234567890qwertyuipasdfghjkzxcvbnmqwertyuipasdfghjkzxcvnm";プライベートカラーbgcolor = new Color(255,255,255);プライベート文字列テキスト; //ランダムな色を生成プライベートカラーランダムカラー(){int red = r.nextint(150); int green = r.nextint(150); int Blue = R.Nextint(150);新しい色(赤、緑、青)を返します。 } //ランダムフォントプライベートフォントランダムフォント(){int index = r.nextint(fontnames.length); string fontname = fontnames [index]; int style = r.nextint(4); // 0 none 1 bold 2 italic 3 bold + obleque int size = r.nextint(5)+24; //ランダムフォントサイズを生成24〜28 return new font(fontname、style、size); } //干渉線のプライベートボイドドローライン(bufferedimage画像){// 4つの干渉線を生成int num = 4; graphics2d bi =(graphics2d)image.getGraphics(); for(int i = 0; i <num; i ++){int x1 = r.nextint(w); int x2 = r.nextint(w); int y1 = r.nextint(h); int y2 = r.nextint(h); bi.setstroke(新しいBasicStroke(1.5F)); bi.setcolor(color.blue); //干渉線色bi.drawline(x1、y1、x2、y2); }} //文字のランダム生成private char randomChar(){int index = r.nextint(codes.length()); return codes.charat(index); } //画像バッファーprivate bufferedimage createimage(){bufferedimage image = new bufferedimage(w、h、bufferedimage.type_int_bgr); graphics2d bi =(graphics2d)image.getGraphics(); bi.setcolor(this.bgcolor); bi.fillrect(0、0、w、h);画像を返します。 } //画像の生成public bufferedimage getImage(){bufferedimage image = createimage(); graphics2d bi =(graphics2d)image.getGraphics(); stringbuilder sb = new StringBuilder(); for(int i = 0; i <4; i ++){string string = randomchar()+""; sb.append(string); //各文字は、画像の1/4幅を説明しますfloat x = i*1.0f*w/4; //ランダムフォントフォーマットbi.setfont(randomfont()); bi.setcolor(randomcolor()); //画像の適切な場所に文字を書きます(H-6は、画像が下から6つの高さであることを意味します)bi.drawstring(string、x、h-6); } this.text = sb.toString();ドローライン(画像);画像を返します。 } //取得したフォントを返すpublic string getText(){return text; } //指定された場所に画像を書き込みますpublic static void output(bufferedimage image、outputStream out){try {imageio.write(image、 "jpg"、out); } catch(ioexception e){e.printstacktrace(); }}}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。