Package com.ucf.staging.utils;
Import Sun.misc.BASE64Decoder;
Import Sun.misc.BASE64Encoder;
Import Javax.imageio.ImageIO;
Import Javax.imageio.stream.FileImageOutputStream;
Import java.awt.*;
Import Java.awt.image.BufferedImage;
Import java.io.*;
Import Java.util.Random;
/** * Generates random numbers or alphabetic strings, which are displayed in an image format for manual recognition, making the program difficult to identify.
* Reduce the likelihood that the system is automatically attacked by the program.
* Generated graphics color from red, black, blue, violet 4 randomly combined, the number or letter vertical position in the * within a certain range is also random, reduce the chance of automatic recognition by the program.
* Because of the 0,1,2 of the numbers and the o,l,z of the letters, the human eye is difficult to recognize, so it does not generate a mixed string of numbers and letters.
* Generated string letters are unified in lowercase, the maximum length of the string is 16.
* */public class Randomgraphic {//character height and width, in pixels private int wordheight = 10;
private int wordwidth = 15;
Character size private int fontsize = 16;
Maximum number of strings private static final int max_charcount = 16;
Vertical starting position private final int initypos = 5;
The number of characters to be generated by the factory method gets private int charcount = 0;
Color array, when drawing a string randomly select a private static final color[] Char_color = {Color.red,color.blue,color.magenta,color.blue};
Random number generator private Random r = new Random ();
/** * Generate the format constants of the image, JPEG format, when the file is the extension of. jpg; * When outputting to a page, you set the MIME type to Image/jpeg/public static String GRAPHIC
_jpeg = "JPEG"; /** * Generates the format constants of the image, PNG format, when the file is generated with the extension. png; * You need to set the MIME type to image/png/public static String when outputting to the page grap
Hic_png = "PNG";
Create an object with the factory method protected randomgraphic (int charcount) {this.charcount = charcount; /** * Create object's Factory method * @param charcount the number of characters to be generated, between 1 and 16 * @return Returns the Randomgraphic object instance * @th
The rows Exception parameter charcount when the error is thrown */public static randomgraphic CreateInstance (int charcount) throws exception{ if (CharCount < 1 | | charcount > Max_charcount) {throw new Exception ("Invalid parameter CharCo
Unt,charcount should between in 1 and 16 ");
Return to New Randomgraphic (charcount);
/** * Randomly generates a number string and draws it as an image, rendering the result output to the stream out * @param graphicformat sets the image format that is generated, the value is Graphic_jpeg or graphic_png * @param out image result lossOut stream * @return The value of a randomly generated string * @throws IOException/public string Drawnumber (string graphicformat,output
Stream out) throws ioexception{//randomly generated string value string charvalue = "";
/*charvalue = Randnumber (); */charvalue = RANDALPHASTR (4);
return Draw (Charvalue,graphicformat,out);
/** * Randomly generates an alphabetic string and draws it as an image, drawing the result output into the stream out * @param graphicformat sets the image format that is generated, the value is Graphic_jpeg or graphic_png * @param out image result output stream * @return The value of the randomly generated string * @throws IOException/public string Drawalpha (string
Graphicformat,outputstream out) throws ioexception{//randomly generated string of values string charvalue = "";
Charvalue = Randalphastr (4);
return Draw (Charvalue,graphicformat,out);
///given range to get random colors 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); /** * Draws the string graphically, draws the result output to the stream out * @param charvalue the string to draw @param Graphicformat sets the image format that is generated, the value is Grap Hic_jpeg or Graphic_png * @param out image output stream * @return The value of the randomly generated string * @throws IOException/Protect Ed String Draw (String charvalue,string graphicformat,outputstream out) throws ioexception{int width = (charcount+
2) * WORDWIDTH;
int height = wordheight * 3;
BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb);
Create a random number generator class.
Random Random = new Random ();
Gets the graphics context Graphics g = image.getgraphics ();
Set Background color G.setcolor (getcolor (100));
G.fillrect (0, 0, width, height);
Set font g.setfont (new font ("Song Body", Font.Bold, 18));
Randomly generated 155 lines of interference, so that the image of the authentication code is not easily detected by other programs G.setcolor (Getrandcolor (160, 200));
for (int i = 0; i < i++) {int x = random.nextint (width);
int y = random.nextint (height);
int xl = Random.nextint (12);
int yl = Random.nextint (12);
G.setcolor (GetColor (25));
G.drawline (x, y, X + xl, y + yl); ///Draw charvalue, each character color random for (int i = 0; i < charcount; i++) {String c = Charvalue.substrin
g (i,i+1);
Color color = Char_color[randomint (0,char_color.length)];
G.setcolor (color);
int xpos = (i+1) * wordwidth;
random int ypos = Randomint (initypos+wordheight,initypos+wordheight*2) in vertical direction;
g.DrawString (C,xpos,ypos);
} g.dispose ();
Image.flush ();
Output to stream Imageio.write (image,graphicformat,out);
return charvalue; /*** random Return of a color, transparency 0~255 0 means full through * @return randomly return a color * @param alpha transparency 0~255 0 means full
* * Private Color getcolor (int alpha) {int r= (int) (Math.random () *255);
int g= (int) (Math.random () *255);
int b= (int) (Math.random () *255);
return new Color (R,g,b,alpha); public string drawinputstr (int num,string graphicformat,outputstream out) throws ioexception{string Charval
UE = RANDALPHASTR (num);
int width = (charcount+2) * wordwidth;
int height = wordheight * 3;
BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb);
Bytearrayoutputstream byteout = new Bytearrayoutputstream ();
Create a random number generator class.
Random Random = new Random ();
Gets the graphics context Graphics g = image.getgraphics ();
Set Background color G.setcolor (GetColor (80));
G.fillrect (0, 0, width, height);
Set the jamming point createrandompoint (width, height,50,g,255);
Set font g.setfont (new font ("Song Body", Font.Bold, 18));
Randomly generated 155 lines of interference, so that the image of the authentication code is not easily detected by other programs G.setcolor (Getrandcolor (160, 200));
for (int i = 0; i < 135 i++) {int x = random.nextint (width);
int y = random.nextint (height);
int xl = Random.nextint (12);
int yl = Random.nextint (12);
G.setcolor (GetColor (200));
G.drawline (x, y, X + xl, y + yl); ///Draw charvalue, each character color random for (int i = 0; i < charcount; i++) {String c = Charvalue.substrin
g (i,i+1); Color color = Char_color[random