图片验证码java代码 图片验证码java代码怎么写( 二 )


下面简单的介绍图片验证码java代码他们的功能和用途 , 执行效率等 。每个都有各自的优缺点看你是做甚什么方面的研究开发用 。.net,是网站编程,现在很多都用这个,但是这个语言编程都有统一思路 , 很好掌握 。窒息那个效率不是很高;php 支持跨平台,很容易学会,执行的效率很高;asp是ASP.net的前身,它比较稳定,比.net要弱一点 。但是比.net好学 。jsp 是网页编程 , 这个学习大约一周就能搞定,不过这个得多实践,不然的话,时间长了,就容易忘记 。
我自己做的系统里面用作验证码的JSP的%@page contentType="image/jpeg;charset=utf-8"%%@page import="java.util.*,java.awt.*,java.awt.image.*,javax.imageio.*" %%@ page import="java.io.OutputStream" %html body%!Color getRandColor(int fc,int bc){Random rd=new Random();if(fc255) fc=255;if(bc255) bc=255;int red=fc+rd.nextInt(bc-fc);int green=fc+rd.nextInt(bc-fc);int blue=fc+rd.nextInt(bc-fc);return new Color(red,green,blue);}%%Random r=new Random();response.addHeader("Pragma","No-cache");response.addHeader("Cache-Control","no-cache");response.addDateHeader("expires",0);int width=90;int height=23;BufferedImage pic=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);Graphics gc=pic.getGraphics();gc.setColor(getRandColor(200,250));gc.fillRect(0,0,width,height);String[] rNum ={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};int[] style = {Font.PLAIN,Font.BOLD,Font.ITALIC,Font.PLAIN+Font.BOLD,Font.BOLD+Font.ITALIC,Font.PLAIN+Font.ITALIC,Font.PLAIN+Font.BOLD+Font.ITALIC};gc.setColor(Color.WHITE);gc.drawLine(0,30,90,10);gc.setColor(getRandColor(160,200));for (int i=0;i50;i++){int x = r.nextInt(width);int y = r.nextInt(height);int xl = r.nextInt(10);int yl = r.nextInt(10);gc.drawLine(x,y,x+xl,y+yl);}gc.setColor(getRandColor(60,150));String rt = "";for(int i=0;i4;i++){String temp = rNum[r.nextInt(62)];rt = rt+temp;gc.setFont(new Font("Times New Roman",style[r.nextInt(7)],15));gc.drawString(temp,5+i*15+r.nextInt(10),10+r.nextInt(10));}gc.dispose();session.setAttribute("randNum",rt);OutputStream os=response.getOutputStream();ImageIO.write(pic,"JPEG",os);System.out.println("当前验证码为:"+session.getAttribute("randNum"));os.flush();os.close();os=null;response.flushBuffer();out.clear();out = pageContext.pushBody();% /body/html
JAVA识别图片验证码package com.he;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
public class CodeFact
extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
//设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 在内存中创建图象
int width = 60, height = 20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
//生成随机类
Random random = new Random();
// 设定背景色
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
//设定字体
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
//画边框
g.setColor(new Color(33,66,99));
g.drawRect(0,0,width-1,height-1);
// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160, 200));
for (int i = 0; i155; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);

推荐阅读