分享
 
 
 

JAVA:附加码生成器(图片)

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

〔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>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有