この例では、参照用にランダムに生成された検証コードの特定のコードを共有します。特定のコンテンツは次のとおりです
java.awt.color; Import java.awt.font; Import java.awt.graphics2d; Import java.awt.image.bufferedimage; Import java.util.random; Import javax.imageio.imageio; Import.Servletletelet.Servletletelet.Serveletelet.Serveletelet.Serveletelet.Servax.Serveleteletelet.Servax.Serveletextextectio javax.servlet.servletoutputStream; Import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; Import.servlet.http.httpservletResponse; Import javax.havellet.httptess; {/ ***オブジェクトのコンストラクター。*/ public Randimage(){super(); } private int imgwidth = 0; //画像幅private int imgheight = 0; //画像の高さプライベートint codecount = 0; //画像の文字の数Private int x = 0;プライベートインテスト; //フォントプライベートInt Codeyの高さ。プライベートストリングフォントスタイル; //フォントスタイル//シリアル化IDは、プライベート静的な最終long serialversionuid = 128554012633034503Lを重複させないようにします。 /***初期化構成パラメーター*/public void init()throws servletexception {// width string strwidth = "200"; // height string strheight = "80"; //文字列文字列strcodecount = "5"; // font fontstyle = "times new Roman"; //構成された情報を数値に変換します{if(strwidth!= null && strwidth.length()!= 0){imgwidth = integer.parseint(strwidth); } if(strheight!= null && strheight.length()!= 0){imgheight = integer.parseint(strheight); } if(strcodecount!= null && strcodecount.length()!= 0){codecount = integer.parseint(strcodecount); }} catch(numberformatexception e){e.printstacktrace(); } x = imgwidth /(codecount + 1); //文字間隔fontheight = imgheight -2; //フォント高さcodey = imgheight -12; // code height}保護されたvoid processRequest(httpservletrequest request、httpservletresponse応答)servletexception、ioexception {//出力ストリームset response.setContentType( "image/jpeg"); //出力形式Response.setheader( "Pragma"、 "No-Cache"); //キャッシュなしResponse.setheader( "Cache-Control"、 "No-Cache"); //キャッシュがrespress.setDateHeaderを再生していません( "expires"、0); //キャッシュの無効化もキャッシュされていませんhttpsession session = request.getSession(); //セッションセッションを取得//メモリで画像を作成するbufferedimage画像= new bufferedimage(imgwidth、imgheight、bufferedimage.type_int_rgb); //グラフィカルコンテキストGraphics2d g = image.creategraphics()を取得します。 //ランダムランダム= new Random()を生成します。 //ランダムクラス//長方形G.SetColor(color.white)の背景色を設定します。 //長方形の長方を白いg.fillrect(0、0、imgwidth、imgheight)に記入します。 //ボーダーフォントg.setfont(new Font(fontstyle、font.plain + font.italic、fontheight))を設定します); //ボーダーカラーG.SetColor(新しい色(55、55、12))を設定します。 //境界G.drawrect(0、0、imgwidth -1、imgheight -1)を描画します。 // 160の干渉線をランダムに生成し、画像内の認証コードを他のプログラムで検出するのは簡単ではないG.SetColor(GetRandColor(160、200)); for(int i = 0; i <160; i ++){int x = random.nextint(imgwidth); int y = random.nextint(imgheight); int xl = random.nextint(12); int yl = random.nextint(12); G.drawline(x、y、x + xl、y + yl); } //ランダムに生成された認証コード(4桁の番号)文字列srand = ""; int red = 0、緑= 0、青= 0; for(int i = 0; i <codecount; i ++){// CodeCountランダム文字を生成するために変更// rgb 3色の新しい色red = random.nextint(255); green = random.nextint(255); blue = random.nextint(255); //ストーリーは、多くの0 1 2 int wordtype = random.nextint(3); // 0-2の間に3つの数字を取得するストーリーを取得するためのストーリーchar retword = 0; // 0番号1小文字2キャピタルレタースイッチ(WordType){case 0:retword = this.getSingLeNumberChar(); // 0-9のchar-typeブレークを取得します。ケース1:retword = this.getlowerorupperchar(0); //小文字のchar-typeブレークを取得します。ケース2:retword = this.getlowerorupperchar(1); //大文字のchar-typeブレークを取得します。 } srand += string.valueof(retword); //取得したランダム文字を取得しますg.setColor(新しい色(赤、緑、青)); //色G.DrawString(String.ValueOf(RetWord)、2+(I) * X、Codey)を設定します。 //画像の対応する位置に文字を書き込み} //セッションセッションで認証コードを保存します。SetAttribute( "rand"、srand); //取得したランダムな文字をセッションの返信に保存すると、//画像がg.dispose()を有効にすることができます。 // GオブジェクトServletOutputStream ResponseTputStream = Response.GetOutputStream()をリリースします。 //出力ストリーム//ページImageio.writeへの出力画像(画像、 "jpeg"、ResponseOutputStream); // jpeg形式の出力//下の入力ストリームを閉じます! ResponseOutputStream.flush(); // fresh and close of close responseutputStream.close(); } color getRandColor(int fc、int bc){//ランダムカラーランダム= 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)を返します。 }保護されたvoid doget(httpservletrequest request、httpservletresponse応答)servletexception、ioexception {processRequest(request、response); }保護されたvoid dopost(httpservletrequest request、httpservletresponse応答)servletexception、ioexception {processRequest(request、response); } //整数の乱数をcharに変換して、private private char getSingLenumberChar(){random = new Random(); int numberResult = random.nextint(10); int ret = numberResult + 48; //文字 '0'をAscallコードに変換する場合、48 return(char)ret; } // 26文字を取得するプライベートチャーgetlowerorupperchar(int upper){random random = new Random(); int numberresult = random.nextint(26); int ret = 0; if(upper == 0){// lowercase ret = numberResult + 97; } else if(upper == 1){// Capital Ret = numberResult + 65; } return(char)ret; }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。