検証コード画像生成ステップ
BuffereDimageオブジェクトを作成します。
BufferedImageのブラシを取得します。つまり、GetGraphics()メソッドを呼び出してグラフィックスオブジェクトを取得します。
グラフィックオブジェクトのsetColor()メソッドとfillRect()メソッドを呼び出して、画像の背景色を設定します。
グラフィックオブジェクトのsetColor()メソッドとdrawline()メソッドを呼び出して、画像干渉線を設定します。
BufferedImagedオブジェクトのSetRGB()メソッドを呼び出して、画像のノイズを設定します。
GraphicsオブジェクトのsetColor()メソッド、setFont()メソッド、およびdrawString()メソッドを呼び出して、画像検証コードを設定します。
検証コードの画像の幅と高さはWebサイトのスタイルに従って決定する必要があるため、フォントのサイズは画像の幅と高さに応じて決定する必要があり、小さなスキルが使用されます。
パッケージUtil; Import java.awt.color; Import java.awt.font; Import java.awt.graphics; Import java.awt.image; Import java.awt.image.Bufferedimage; Import java.io.io.io.io.io.io.io.io.ioexception; Import java.util.random; javax.imageio.imageio; public class visification {private static final string alphabet = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz1234567890"; / ***幅として幅、高さとしての高さ、および検証コードのコードとしての画像を生成* @param width画像の幅* @param heightime画像の高さ* @paramコード検証コード文字列* @return return画像検証コード*/ public static bufferedimage getimage(int width、int height、string code) } /** *幅を幅、高さとして生成し、高さとして、検証コードはコード、写真の干渉線の数はlinecnt * @param width of picture * @param heightの高さ * @paramコード検証コード文字列 * @param linecnn public static bufferedimage getImage(int width、int height、string code、int linecnt){return createimage(width、height、code、linecnt、0.01); } /***幅、高さ、高さ、検証コードの絵を生成するコードはコード、写真の干渉線の数はlinecnt*ノイズ比がノイゼレートです。つまり、絵の幅の幅の幅* @param width* @param height of the quite of the quiphififiriferiation code string intecident結果に応じて適切に* @paramは、写真のノイズピクセルの割合を合計ピクセルに拡張します* @return画像検証コードを返します*/ public static bufferedimage getImage(int width、int height、string code、int linecnt、double noisate) } /** * *幅、高さ、検証コードの画像を生成します。画像内の干渉線の数はlinecnt*ノイズ比がノイズレートです。つまり、画像のノイズピクセルの割合* @param width画像の幅* @param height height of the image* @param code verification code string* @param linecnt @param linecnt干渉ラインの数は、noised emanced apperative nois no no no no secationed a emacterty no spary no spary no spary linecn合計ピクセルへ* @return画像検証コードを返します* / private static bufferedimage createimage(int width、int height、string code、int linecnt、double noiserate){int fontwidth =((int)(wid)(wid)(width* 0.8)) / codelengt.length(); int fontheight =(int)(height * 0.7); //幅と高さの下で適切な検証コードを生成するために、// fontwdithのサイズは、fontheightで小さい、int fontsize = math.min(fontwidth、fontheight); // int paddingx =(int)(width * 0.1); int paddingy = height-(height -fontsize) / 2; //画像を作成するbufferedimage buffimg = new bufferedimage(幅、高さ、bufferedimage.type_int_rgb); //ブラシグラフィックスG = buffimg.getgraphics(); //ブラシの色を設定しますg.setcolor(getRandColor(200、255)); //次に、長方形を埋める、つまり、背景色G.FillRect(0、0、幅、高さ)を設定します。 //(int i = 0; i <linecnt; i ++)の干渉線を設定します{// interference line int xs =(int)(math.random() * width)の開始点とエンドポイントを取得するには保存します。 int ys =(int)(math.random() * height); int xe =(int)(math.random() * width); int ye =(int)(math.random() * height); G.SetColor(GetRandColor(1、255)); G.drawline(xs、ys、xe、ye); } //ノイズintエリア=(int)(noiserate * width * height); for(int i = 0; i <area; ++ i){int x =(int)(math.random() * width); int y =(int)(math.random() * height); buffimg.setrgb(x、y、(int)(math.random() * 255)); } //フォントフォントフォントを設定= new Font( "Ravie"、font.plain、fontsize); G.SetFont(Font); for(int i = 0; i <code.length(); ++ i){string ch = code.substring(i、i+1); G.SetColor(GetRandColor(1、199)); G.DrawString(CH、PADDINGX + FONTWIDTH * I、PADDINGY); } buftimgを返します。 } / ***ランダムな色を取得し、r、g、bの値はlとrの間にあります* @param l left interval* @param r右インターバル* @returnランダムカラー値を返します* / private static color getRandcolor(int l、int r){if(l> 255)l = 255; if(r> 255)r = 255; if(l <0)l = 0; if(r <0)r = 0; int interval = r -l; int r = l +(int)(math.random() * interval); int g = l +(int)(math.random() * interval); int b = l +(int)(math.random() * interval);新しい色(r、g、b)を返します。 } / ***大文字と小文字と数字で構成されるいくつかの文字列をランダムに生成します* @param lenはレン文字をランダムに生成します* @returnは大文字と小文字と数字で構成されるランダムに生成された文字列を返します* / public static string getRandCode(int len){string code = ""; for(int i = 0; i <len; ++ i){int index =(int)(math.random() * alphabet.length()); code = code + alphabet.charat(index); } return code; } / ***画像をバイト配列に変換* @param image画像* @returnバイト配列を返します* @throws ioexception* / public static byte [] getbytearray(bufferedimage画像)ioexceptionをスローするImageio.write(image、 "png"、baos); return baos.tobytearray(); // bytearrayoutputStreamは閉じる必要はありません}}検証コード画像を使用します
確認コード.javaサーブレットで、上記のクラスを呼び出して確認コード画像を生成し、写真をクライアントに返します。
保護されたvoid doget(httpservletrequest request、httpservletresponse応答)servletexception、ioexception {httpsession session = request.getsession(); //文字列をランダムに生成し、セッション文字列code = verification.getRandCode(4); session.setattribute( "verification"、code); bufferedimage image = util.verification.getimage(100,30、code、5); Response.setContentType( "Image/PNG"); outputStream out = response.getOutputStream(); out.write(util.verification.getBytearray(画像)); out.flush(); out.close(); }index.jspで確認コードを設定します。ユーザーが検証コードをクリックすると、JSコードを呼び出してサーバーに要求して新しい検証コードを取得します。検証コードを生成する上記のサーブレットはブラウザによってキャッシュされるため、JSコードはサーブレットにランダムパラメーターを与える必要があるため、ブラウザはサーバーにリクエストを送信して、キャッシュで読み取るのではなく、新しい検証コードを取得します。
<%@ページインポート= "util.verification"%> <%@page言語= "java" contentType = "text/html; charset = utf-8" pageencoding = "utf-8"%> <!doctype html public "-// w3c // dtd html 4.01 mransitional // en en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv =" content-type "content =" text/html; charset = utf-8 "> <title>挿入タイトルdocument.getElementById( "verification")。src = "/verificationCode/visificationCode?hehe ="+math.random(); } </script> </head> <body> <form action = "<%= request.getContextPath()+"/checkverification "%>" method = "post">検証コード:<入力タイプ= "テキスト" name = "dubvisverification"> <img id = "verification" src = "<%= request.getContextpath()+" <入力型= "submit" name = "submit" value = "submit"> </form> </body> </html>
最後に、CheckVerification.javaサーブレットで、ユーザーが入力した検証コードが正しいかどうかを判断します。ユーザーを容易にするために、検証コードは通常、ケースに依存しないように設定されるため、比較前に小文字に変換する必要があります。
保護されたvoid doget(httpservletrequest request、httpservletresponse応答)servletexception、ioexception {httpsession session = request.getsession();文字列検証=(string)session.getattribute( "verification"); string dubvidverification = request.getParameter( "dubvisverification"); printwriter out = response.getWriter(); if(検証!= null && submitverification!= null){if(vilifisionification.tolowercase()。equals(submitverification.tolowercase())){out.println( "yes !!!"); } else {out.println( "no !!!"); }} else {out.println( "no !!!"); } session.RemoveAttribute( "Verification"); //ユーザーが繰り返しフォームを送信しないように}/** * @see httpservlet#dopost(httpservletrequest request、httpservletresponse応答) TODO自動生成メソッドスタブドーゲット(リクエスト、応答); }最終的なランニングレンダリングは次のとおりです
上記はこの記事に関するものです。すべての人の学習に役立つことを願っています。