SQnote
永远有多远……
SQnote - java,web优化,python,(www.sqnote.cn)

〔Picture.java〕

package creator.common.attachLogin;

import java.io.*;

public class Picture{

  final int key=1;

  final String error_format_int= "format of color is not rgb.sample \"212|232|0\"";

  final String error_color_input="format of color(num|num|num): num in 0-255";

  /**格式化输出数据**/

  public String manage(String temp){

    String returnStr="";

    temp = encrypt(temp);

    byte[] by =temp.getBytes();

    for(int i=0;i<by.length;i++){

      returnStr=returnStr+(byte)by[i]+"|";

    }

    return returnStr;

  }

  /**格式化输入数据**/

  public byte[] disManage(String temp){

    int len=0,index=0,i=0,first=0;

    while(( i=temp.indexOf("|",first))>-1){

      len++;

      first=i+1;

    }

    byte[] by=new byte[len];

    first=0;

    while(( i=temp.indexOf("|",first))>-1){

     by[index]=Byte.parseByte(temp.substring(first,i));

     index++;

     first=i+1;

    }

    return by;

  }

 /**随机生成四位的附加码**/

 public String getRandom(){

    int i1 = (int)(java.lang.Math.random()*10);

    int i2 = (int)(java.lang.Math.random()*10);

    int i3 = (int)(java.lang.Math.random()*10);

    int i4 = (int)(java.lang.Math.random()*10);

    return String.valueOf(i1)+String.valueOf(i2)+String.valueOf(i3)+String.valueOf(i4);

  }

  /**加密1:错位处理**/

  public String encrypt(String randomStr){

     String para=random()+randomStr.substring(0,1)+random()+random()+randomStr.substring(1,2);

            para= para+random()+randomStr.substring(2);

     return jiaMi(para);

  }

  /**得到随机数0-9之间**/

  private String random(){

    String temp = String.valueOf((int)(java.lang.Math.random()*10));

    return temp;

  }

  /**加密2:加密处理,此方法可以自己修改**/

  private String jiaMi(String str){

    byte[] by = str.getBytes();

    ByteArrayInputStream in = new ByteArrayInputStream(by);

    int ch;

    int index=0;

    byte[] temp = new byte[in.available()];

    while((ch=in.read())!=-1){

      temp[index]=(byte)(ch-key);

      index++;

    }

    ByteArrayInputStream ins = new ByteArrayInputStream(temp);

    BufferedReader fReader = new BufferedReader(new InputStreamReader(ins));


    try{ return  fReader.readLine();}catch(Exception e){return "";}

  }

  /**从给的数字里得到正确的数字**/

  public String discrypt(String temp){

    String para = jieMi(disManage(temp));

    return para.substring(1,2)+para.substring(4,5)+ para.substring(6,8);

  }

  /**解密处理**/

  private String jieMi(byte[] by){

    ByteArrayInputStream in = new ByteArrayInputStream(by);

    int ch;

    int index=0;

    byte[] temp = new byte[in.available()];

    while((ch=in.read())!=-1){

      temp[index]=(byte)(ch+key);

      index++;

    }

    ByteArrayInputStream ins = new ByteArrayInputStream(temp);

    BufferedReader fReader = new BufferedReader(new InputStreamReader(ins));

   try{ return fReader.readLine();}catch(Exception e){return "";}

  }

  /**分解rgb格式的颜色 num|num|num**/

  public int[] masterData(String temp){

    int index_len=0,index=0,next_index=0;

    int[] return_arr=new int[3];

    boolean break_error=false;

    if(getMax(temp,"|")==2){

      while((index_len=temp.indexOf("|",next_index))>-1){

        if(getInt(temp.substring(next_index,index_len))==256){

          break_error = true;

        }else{

          return_arr[index]=getInt(temp.substring(next_index,index_len));

          next_index=index_len+1;

          index++;

        }

        if(break_error) break;

      }

      if(break_error){

        return null;

      }else{

        return_arr[index] = getInt(temp.substring(next_index));

        return return_arr;

      }

    }else{

      System.out.println(error_format_int+":"+temp);

      return null;

    }

  }

  private int getMax(String temp,String temp2){

    int index=0,index_len=0,index_next=0;

    while((index=temp.indexOf(temp2,index_next))>-1){

      index_len++;

      index_next=index+1;

    }

    return index_len;

  }

  private int getInt(String temp){

    try{

       return Integer.parseInt(temp);

    }catch(Exception e){

       System.out.println(error_color_input+":"+temp);

       return 256;

    }

  }

}

〔CreateImage.java〕

package creator.common.attachLogin;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.awt.*;

import java.awt.image.*;

import java.awt.image.BufferedImage;

import com.sun.image.codec.jpeg.*;

import com.sun.image.codec.jpeg.JPEGCodec;

public class CreateImage extends HttpServlet {

  static final private String CONTENT_TYPE = "text/html; charset=gb2312";

  final String input_back_color_error="input rgb backcolor is error";

  final String input_fore_color_error="input rgb forecolor is error";

  private Picture pic = new Picture();


  //Initialize global variables

  public void init() throws ServletException {

  }

  //Process the HTTP Get request

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //生成图片
    try{
      int imageWidth = 60;
      int imageHeight = 20;

      BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
      Graphics graphics = image.getGraphics();
      graphics.setColor(Color.white);
      graphics.fillRect(0,0,imageWidth,imageHeight);
      graphics.setColor(Color.white);


      FileOutputStream fos = new FileOutputStream("attach.jpg");
      BufferedOutputStream bos = new BufferedOutputStream(fos);
      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
      encoder.encode(image);
      bos.close();

    }catch(Exception e){
      System.out.println("生成图片出错!");
    }

    String queryNum   = request.getParameter("Image");

    String queryRgb="";

    if(request.getParameter("Rgb")!=null){

       queryRgb   = request.getParameter("Rgb");

    }

    response.setHeader("Cache-Control","no-store");

    response.setContentType("image/jpeg");

    ServletOutputStream out=response.getOutputStream();

    //jpg格式的背景色图片(于页面风格一样),宽3.6毫米,高1.8毫米

    InputStream imageIn = new FileInputStream(new File("attach.jpg"));

    JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);

    BufferedImage image4 = decoder.decodeAsBufferedImage();

    queryNum = pic.discrypt(queryNum);

    Graphics g = image4.getGraphics();

    if(queryRgb.length()>1){

      if(pic.masterData(queryRgb)!=null){

        int[] arg = pic.masterData(queryRgb);

        g.setColor(new Color(arg[0],arg[1],arg[2]));

      }

    }else{

      g.setColor(new Color(255,0,0));

    }

    g.drawString(queryNum,0,13);

    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

    encoder.encode(image4);

    out.close();

  }

}

〔web.xml〕


  <servlet>
    <servlet-name>CreateImage</servlet-name>
    <servlet-class>javabean.com.CreateImage</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>CreateImage</servlet-name>
    <url-pattern>/servlet/CreateImage</url-pattern>
  </servlet-mapping>

〔index.jsp〕

<%@page contentType="text/html; charset=gb2312" %>
<%@page import="javabean.com.Picture"%>
<%
  Picture pic = new Picture();
  String random =pic.getRandom();
  String encryRandom= pic.manage(random );
%>
<html>
<head>
<title>登陆窗口</title>
</head>
<body>
<div align="center">
      <p align="center">附加码:<input type="attach" size="4">
      <img align=absbottom vspace=2 border=0  src="servlet/CreateImage?Image=<%=encryRandom%>&Rgb=255|0|0" title="验证码图片"></p>
      <input type="hidden" name="get_attach" value="<%=random%>">
</div>
</body>
</html>

posted on 2005-10-26 23:03  SQnote·www.SQnote.cn  阅读(146)  评论(0编辑  收藏  举报