Wednesday, May 6, 2009

How to ceate a Security Code

package com.ozview;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

import javax.imageio.ImageIO;

public class Test
{
/** The image font. * */
private static Font imageFont = new Font("Arial Bold", Font.BOLD, 24);



protected String generateImageCode(ByteArrayOutputStream output)
{
int width = 150, height = 30;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = image.getGraphics();
Random random = new Random();
//g.setColor(getRandColor(200, 250));
g.setColor(new Color(230, 241, 241));
g.fillRect(1, 1, width - 1, height - 1);
g.setColor(new Color(102, 102, 102));
g.drawRect(0, 0, width - 1, height - 1);
g.setFont(imageFont);
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++)
{
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(6) + 1;
int yl = random.nextInt(12) + 1;
g.drawLine(x, y, x + xl, y + yl);
}
for (int i = 0; i < 70; i++)
{
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(12) + 1;
int yl = random.nextInt(6) + 1;
g.drawLine(x, y, x - xl, y - yl);
}

String sRand = "";
for (int i = 0; i < 8; i++)
{
String tmp = getRandomChars();
sRand += tmp;
//g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
g.setColor(new Color(0, 0, 255));
g.drawString(tmp, 17 * i + 9, 22);
}
g.dispose();
try
{
File imagefile = new File("securitycode.jpg");
ImageIO.write(image, "jpg", imagefile);
} catch (IOException e)
{
System.err.print("Generating the security image code error, " + e.getMessage());
}
return sRand;
}

/**
* Get random color.
*
* @param fc
* a int fc.
* @param bc
* a int bc.
* @return a Random color.
*/
protected Color getRandColor(int fc, int bc)
{
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}

/**
* Get a random characters.
*
* @return a random characters.
*/
protected String getRandomChars()
{
int rand = (int) Math.round(Math.random() * 2);
long itmp = 0;
char ctmp = '\u0000';
switch (rand)
{
case 1:
itmp = Math.round(Math.random() * 25 + 65);
ctmp = (char) itmp;
return String.valueOf(ctmp);
case 2:
itmp = Math.round(Math.random() * 25 + 97);
ctmp = (char) itmp;
return String.valueOf(ctmp);
default:
itmp = Math.round(Math.random() * 9);
return String.valueOf(itmp);
}
}

public static void main(String [] args) {
Test test = new Test();
ByteArrayOutputStream output = new ByteArrayOutputStream();
String randomCode = test.generateImageCode(output);

System.out.println(randomCode);


}

}

1 comment:

  1. Casino & Hotel Montgomery, MD Jobs - JM Hub
    Find all Casino 경주 출장안마 & Hotel Montgomery, MD 문경 출장안마 jobs in one convenient location. Get 양주 출장안마 the inside scoop on 군산 출장샵 jobs, salaries, 세종특별자치 출장샵 top office locations and more.

    ReplyDelete