最近、Java画像検証コードに関する多くの記事があり、Java検証コード生成テクノロジーを全員が習得するのに役立ちます。以下は、参照用のJava画像検証コードを生成する最も簡単な方法です。
現在、すべての業界は、システムをカスタマイズする際に機械登録を検討します。現在最も効果的な方法は、検証を入力することです。今確認する方法はたくさんあります:
1。問題の検証は、実際には画像検証でもあります。写真に質問を生成し、ボックスを入力して回答を入力します。
2。画像の確認、写真に表示されているテキスト情報を入力します。
3。SMS検証は非常に複雑であり、ユーザーはそれをあまり好まない。
4. Baiduの最新の検証方法もあります。テキストは画像に生成され、テキストクリックボックスが表示され、検証画像に表示されているテキストを選択します。
次に、Javaのコードを共有して、基本コードである検証コードを生成しましょう。学習に直接使用できます。より複雑な検証が必要な場合は、論理的検証を自分で追加できます。
@controllerpublic class imgverifycode httpservlet { / ** * * * / private static final long serialversionuid = 1l; /***検証コード画像の幅。 */ private int width = 70; /***検証コード画像の高さ。 */ private int height = 30; / ***検証コード文字の数*/ private int codecount = 5; / ** * xx */ private int xx = 0; / *** font height*/ private int fontheight; / ** * codey */ private int codey; / ** * codesequence */ string [] codesequence = {"1"、 "2"、 "3"、 "4"、 "5" 6 "、" 7 "、" 8 "、" 9 "、" a "、" a "、" b "、b"、 "c" "c" 、 "d"、 "d"、 "e"、 "e"、 "f"、 "f"、 "g"、 "z"、 "x"、 "q"、 "v"}; / ***検証画像属性の初期化*/ public void init()throws servletexception {// web.xml // width string strwidth = width+""から初期情報を取得します。 // height string strheight = height+""; //文字列文字列strcodecount = codecount+""; //構成された情報を数値に変換します{if(strwidth!= null && strwidth.length()!= 0){width = integer.parseint(strwidth); } if(strheight!= null && strheight.length()!= 0){height = integer.parseint(strheight); } if(strcodecount!= null && strcodecount.length()!= 0){codecount = integer.parseint(strcodecount); }} catch(numberformatexception e){e.printstacktrace(); } xx = width /(codecount + 2); //乱数を生成する水平距離fontheight = height -12; //乱数を生成する数値codey = height -8; //乱数を生成する垂直距離//画像バッファーbufferedimage buffimg = new bufferedimage(幅、height、bufferedimage.type_int_rgb); graphics2d gd = buffimg.creategraphics(); //画像バッファーbufferedimage buffimg = new bufferedimage(幅、height、bufferedimage.type_int_rgb); graphics2d gd = buffimg.creategraphics(); //画像バッファーbufferedimage buffimg = new bufferedimage(幅、height、bufferedimage.type_int_rgb); graphics2d gd = buffimg.creategraphics(); //ランダム数ジェネレータークラスの作成ランダム= new Random(); //イメージを白いgd.setcolor(color.white)として記入します。 gd.fillrect(0、0、幅、高さ); //フォントを作成するには、フォントのサイズを画像の高さに応じて決定する必要があります。 font font = new font( "sixedsys"、font.plain、fontheight); //フォントを設定します。 gd.setfont(font); //境界線を描きます。 gd.setcolor(color.black); gd.drawrect(0、0、width -1、height -1); // 4つの干渉線をランダムに生成し、画像内の認証コードを他のプログラムで検出する可能性が低くなります。 gd.setcolor(color.black); for(int i = 0; i <4; 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; // CodeCount番号の検証コードをランダムに生成します。 for(int i = 0; i <codecount; i ++){//ランダムに生成された検証コード番号を取得します。 string strrand = string.valueof(codesequence [random.nextint(27)]); //ランダムな色のコンポーネントを生成して、各数字出力の色値が異なるように色値を構築します。 red = random.nextint(125); green = random.nextint(255); blue = random.nextint(200); //ランダムに生成された色を使用して、検証コードを画像に描画します。 gd.setcolor(新しい色(赤、緑、青)); gd.drawstring(strrand、(i + 1) * xx、codey); //生成された4つの乱数を一緒に結合します。 randomcode.append(strrand); } //セッションに4桁の検証コードを保存します。 httpsession session = req.getsession(); session.setattribute( "validatecode"、randomcode.tostring()); //画像キャッシュは禁止されています。 resp.setheader( "pragma"、 "no-cache"); resp.setheader( "Cache-Control"、 "No-Cache"); resp.setdateheader( "expires"、0); resp.setContentType( "image/jpeg"); //画像をサーブレット出力ストリームに出力します。 servletoutputStream sos = resp.getOutputStream(); Imageio.write(buffimg、 "jpeg"、sos); sos.close(); nullを返します。 }}このコードは、検証画像を生成するための基本的な方法です。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。詳細な調査については、以前の記事をご覧ください。