Java image verification code generation tutorial

First, let's look at how to generate a picture verification code locally, and then how to write a verification code output to a web page.

Let's start with the simplest -- the function of the implementation is to turn a string into an image and write it to a file

Implementation code:

result:

The above one is very simple, isn't it? The verification codes we see are not like this. Well, let's add some interference lines, background color, characters and Y coordinates to generate them randomly.

Verification code with interference line and background color - write to file

Demo code:

Presentation results:

Finally, let's look at a verification code that can be rotated and zoomed - written to the local image file

Demo code:

Presentation results:

Now we will begin to demonstrate the image verification technology of the front desk.

Foreground image verification technology

Structure diagram of this project:

index. jsp:

web. xml:

ImageServlet. java

Presentation results:

The following is in index Jsp: if you use this sentence: IMG src=”/myHelloWeb/servlet/ImageServlet”;

You can see the response header:

Look at the response header with this sentence: IMG src=”/myHelloWeb/servlet/ImageServlet?”+ time;

Multiple date responses! Because time is always changing, every time you can't see clearly, you will request it from the server again, instead of not requesting it because of the browser's cache.

The verification code ends here first.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>