この記事では、参照のためにJava検証コードによって生成されたサンプルコードを共有しています。特定のコンテンツは次のとおりです
パッケージcom.gonvan.component.captcha; java.awt。*;インポートjava.awt.image.bufferedimage; Import java.io.ioexception; import java.util.hashmap; import java.util.map; import java.util.random; javax.imageio.imageio;インポートjavax.servlet.servletoutputStream; Import javax.servlet.http.httpservletrequest; Import javax.servlet.http.httpservletresponse; Import javax.servlet.http.httpsession; /*** 2016/3/14にYuerzmによって作成されました。 */public class captchafactory {private static final char [] code_sequence = "abcdefghijklmnopqrstuvwxyz0123456789" .tochararray();プライベート静的最終int default_width = 60;プライベート静的最終int default_height = 20;プライベート静的最終int default_code_len = 4;プライベート静的最終int default_code_x = 13;プライベート静的最終int default_code_y = 16;プライベート静的最終int default_font_size = 18;プライベート静的最終文字列default_font_family = "Times New Roman"; Private Static CaptChaFactoryインスタンス= new CaptChaFactory(); private int width = default_width; //幅private height = default_heightを定義します。 //高さを定義しますプライベートint length = default_code_len; //画像に表示されている検証コードの数を定義しますprivate int xx = default_code_x; //検証コードx座標プライベートint yy = default_code_y; // Picture Private int fontsize = default_font_size; //画像に表示される検証コードのフォントサイズを定義しますprivate string fontfamily = default_font_family; //画像に表示される検証コードの数を定義private captchafactory(){} public static captChaFactory getInstance(){return instance; } / ** * width and heightを構成 * * @param w * @param h * @return * / public captchafactory configwidthandheight(int w、int h){instance.width = w; instance.height = h;インスタンスを返す; } / ** * configure coordinates * * @param x * @param y * @return * / public captchafactory configxy(int x、int y){instance.xx = x; instance.yy = y;インスタンスを返す; } / ** * font size * * @param fontsize * @return * / public captchafactory configfontsize(int fontsize){instance.fontsize = fontsize;インスタンスを返す; } / ** * font * * @param fontfamily * @return * / public captchafactory configfontsize(string fontfamily){instance.fontfamily = fontfamily;インスタンスを返す; } public void write(httpservletrequest request、httpservletresponse応答)ioexception {// 4桁の検証コードをセッションに保存します。マップcaptcha = generate(); string randomcode =(string)captcha.get( "captchacode"); bufferedimage buffimg =(bufferedimage)captcha.get( "captchaimg"); httpsession session = request.getSession(); session.setattribute( "code"、randomcode); //画像キャッシュは禁止されています。 Response.setheader( "Pragma"、 "no-cache"); Response.setheader( "Cache-Control"、 "no-cache"); Response.setDateHeader( "Expires"、0); Response.setContentType( "Image/jpeg"); //画像をサーブレット出力ストリームに出力します。 servletoutputStream outputStream = respons.getOutputStream(); Imageio.write(buffimg、 "jpeg"、outputstream); outputStream.close(); } public Map <string、object> generate()throws ioexception {// image buffer bufferedimage buffimg = new bufferedimage(width、height、bufferedimage.type_int_rgb);グラフィックスGd = buffimg.getgraphics(); //背景色GD.SetColor(GetRandColor(200、250))を設定します。 gd.fillrect(0、0、幅、高さ); //フォントを設定すると、フォントのサイズを画像の高さに応じて決定する必要があります。 gd.setfont(new Font(fontfamily、font.plain、fontsize)); //ランダム数ジェネレータークラスの作成ランダム= new Random(); //ランダムは40の干渉線を生成し、画像内の認証コードを他のプログラムで検出する可能性が低くなります。 gd.setcolor(getRandColor(160、200)); for(int i = 0; i <155; i ++){int x = random.nextint(width); int y = random.nextint(height); int xl = random.nextint(12); int yl = random.nextint(12); gd.drawline(x、y、x + xl、y + yl); } //ランダムコードは、ランダムに生成された検証コードを保存するために使用され、ユーザーがログインした後に確認できるようにします。StringBufferRandomCode= new StringBuffer(); int red = 0、緑= 0、青= 0; //長さ検証コードをランダムに生成します。 for(int i = 0; i <length; i ++){//ランダムに生成された検証コード番号を取得します。 string code = string.valueof(code_sequence [random.nextint(36)]); //ランダムな色のコンポーネントを生成して、各数字出力の色値が異なるように色値を構築します。 red = random.nextint(110); green = random.nextint(110); blue = random.nextint(110); //ランダムに生成された色で検証コードを画像に描画します。 gd.setcolor(新しい色(赤 + 20、緑 + 20、青 + 20)); gd.drawstring(code、i * xx + 6、yy); //生成された乱数を一緒に結合します。 randomcode.append(code); } map <string、object> retval = new Hashmap <>(); retval.put( "captchacode"、randomcode.toString()); retval.put( "captchaimg"、buffimg);返品; } / ** *範囲が与えられたランダムな色を取得 * * @param fc * min * @param bc * @return color * / private color getRandColor(int fc、int bc){random random = new Random(); if(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);新しい色(r、g、b)を返します。 }}上記はこの記事に関するものです。すべての人の学習に役立つことを願っています。