ユーザーがウェブサイト情報を登録するとき、基本的にデータ検証コード検証が必要です。では、画像検証コード関数を実装する方法は?
おおよその手順は次のとおりです。
1.メモリにキャッシュされた画像を作成します
2.背景色を設定します
3。境界線を描きます
4。文字を書きます
5。干渉情報を描きます
6。画像出力
これ以上苦労せずに、コードをアップロードするだけです
パッケージcom.lsgjzhuwei.servlet.response; import java.awt.color; Import java.awt.font; Import java.awt.graphics; Import java.awt.image.bufferedimage; Import java.io.ioexception; import java.util.util.util.util.util.util javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; Import javax.servlet.http.httpservletrequest; Import javax.servlet.http.httptservletrelesplet expraminest; */@websverlet(asyncsupported = true、urlpatterns = {"/vidificationCode"})public class visificationCodeはhttpservlet {private static final long serialversionuid = 1l; / ** * @see httpservlet#httpservlet() */ public vilificationcode(){super(); // todo auto-generated constructor stub}/** * @see httpservlet#doget(httpservletrequest request、httpservletresponse応答)高さ= 30; //メモリでキャッシュされた画像を作成するbufferedimage bufferedimage = new bufferedimage(width、height、bufferedimage.type_int_rgb); ///グラフィックスを介して画像を描くグラフィックスグラフィック= bufferedimage.getgraphics(); ///ドローボーダーグラフィックス。セットコロア(color.blue); graphics.drawrect(0、0、width-1、height-1); //書き込み文字列コンテンツ= "フォントカラーグラフィックスセットコロア(color.red); // font and size graphics.setfont(new font( "宋体"、font.bold、20)); int x = 20; int y = 20; for(int i = 0; i <4; i ++){int index = random.nextint(content. content.); x、y); x = x+20;} //干渉線を描画int x1; int x2; int y1; int y2; graphics.setcolor(color.light_gray); for(int i = 0; i i <50; i ++){x1 = random.nextint(width); x2 = random.nextint(width); y1 = andextint(height); y2 = random.nextint(heigh); graphics.drawline(x1、x2、y2);} //画像の出力を出力します。 Image Imageio.write(bufferedimage、 "jpg"、response.getOutputStream());}/** * @See httpservlet#dopost(httpservletrequest request、httpservletresponse応答) {// TODO自動生成方法スタブ}}Java Webで上記の画像検証コード関数の簡単な実装は、私があなたと共有するすべてのコンテンツです。参照を提供できることを願っています。wulin.comをもっとサポートできることを願っています。