Cuando los usuarios registran la información del sitio web, básicamente necesitan verificación del código de verificación de datos. Entonces, ¿cómo implementar la función del código de verificación de imagen?
Los pasos aproximados son:
1. Cree imágenes en caché en la memoria
2. Establezca el color de fondo
3. Dibuja las fronteras
4. Escribe cartas
5. Dibujar información de interferencia
6. Salida de imagen
Sin más preámbulos, simplemente cargue el código
paquete com.lsgjzhuwei.servlet.response; import java.awt.color; import java.awt.font; import java.awt.graphics; import java.awt.image.bufferedImage; import java.io.ioxception; import java.util.random; importar javax .imageio; javax.servlet.servletException; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.htttpservletRequest; import javax.servlet.http.httpservletResponse;/*** *** CASA DE LA SERVETLET; */@WebServlet (asyncsupported = true, urlPatterns = {"/verificationCode"}) public class VerificationCode extiende httpservlet {private static final long serialVersionUid = 1l; / ** * @see httpservlet#httpservlet () */ public verificationCode () {super (); // TODO STUB de constructor generado automático}/** * @see httpservlet#doget (httpservletRequest request, httpServletReSponse respuesta) */protegido void doget (httpservletRequest solicitud, httpServletResponse respuesta) tira serveTextException, iOexception {// toDO AUTOTO METOTOTT 120; int altura = 30; // Crear una imagen en caché en la memoria BufferedImage BufferedImage = new BufferedImage (Width, Height, BufferedImage.Type_int_rgb); /// Background Color // Dibuja imagen a través de gráficos Gráficos GRÁFICOS = BuffEdimage.getGraphics (); // establecer color gráfico. width, height);///Draw border graphics.setColor(Color.blue);graphics.drawRect(0, 0, width-1, height-1);//Write letter String content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxyz1234567890";Random random = new Random();//Set font color graphics.setColor(Color.red);//Set 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.length());char letter = content.charAt(index);graphics.drawString(letter+" "", x, y); x = x+20;} // Dibuja la línea de interferencia int x1; int x2; int y1; int y2; gráfico.setColor (color.light_gray); para (int i = 0; i <50; i ++) {x1 = random.nextint (ancho); x2 = random.nextint (ancho); y1 = random.nextint (altura); y2 = random.nextint (altura); gráfico.drawline (x1, y1, x2, y2);} // Salir la imagen a la imagen // emitir la imagen en la imagen de la imagen JPG a través de la imagen de la imagen JPG a la salida de Browser Browser. ImageIO.Write (BufferedImage, "JPG", Response.getOutputStream ());}/** * @see httpservlet#dopost (httpservletRequest request, httpServletResponse Respuesta) */Protected void dopost (httpServRequest solicitud, httpSeServeSponse Respuesta) ServletException {/////la solicitud de ServletException, i/////la solicitud de ServletException, IT///////////la solicitud HttTpSeT. TODO Auto Generado Método STUB}}La simple implementación de la función del código de verificación de imagen mencionada anteriormente en la web Java es todo el contenido que comparto con usted. Espero que pueda darle una referencia y espero que pueda apoyar más a Wulin.com.