工具類方法如下
public class VerificationCode { Random rand = new Random(); /** * 隨機產生的加數和被加數 */ private int jiashu=0; private int beijiashu=0; /** * 隨機產生的計算方式,0表示加,1表示減 */ private int js=0; private char[] aa={'零','壹','貳','叄','肆','伍','陸','柒','捌','玖'}; private char[] action={'加','減'}; private char[] jieguo={'等','是'}; public static void main(String[] args) { VerificationCode code=new VerificationCode(); JFrame jFrame=new JFrame(); jFrame.setBounds(400, 400, 250, 250); ImageIcon img = new ImageIcon(code.getVerificationCode2()); JLabel background = new JLabel(img); jFrame.add(background); jFrame.setVisible(true); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /** * 第二種驗證碼的計算方式,兩位數的加減法 * @return 一個新的驗證碼圖片 */ public BufferedImage getVerificationCode2() { int width=200; int height=100; int degree=0;//繼續一共旋轉的角度,方便最后的時候旋轉回來 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR); // 獲取圖形上下文 Graphics g = image.getGraphics(); //畫隨機干擾框 setSquareBackGround(g,width,height,5); //畫干擾點 //CreateRandomPoint(width, height,50,g,255); //隨機畫幾條線 // CreateRandomLine(width, height,2,g,255); // 設定背景色 Color background=getColor(180); g.setColor(background); g.fillRect(0, 0, width, height); //畫邊框 g.setColor(background); g.drawRect(0,0,width-1,height-1); // 將認證碼顯示到圖像中,如果要生成更多位的認證碼 char[] content=getDrawContent2(); int[] xs=getRadonWidths(width,content.length); int[] ys=getRadomHeights(height,content.length); for(int i=0;i<content.length;i++) { String s=content[i]+""; if(content[i]=='!') s=""; //如果在畫字之前旋轉圖片 if(i!=2){ int maxDegree=rand.nextInt(2); if(maxDegree==0) maxDegree=0; else maxDegree=305; degree=rand.nextInt(45)+maxDegree; } else degree=0; g.setColor(getColor(background,230)); if(i==2)//運算符號顯示大一些 g.setFont(new Font("Atlantic Inline",Font.PLAIN,getIntRandom(22,25))); else g.setFont(new Font("Atlantic Inline",Font.PLAIN,getIntRandom(20,23))); RotateString(s, xs[i], ys[i], g, degree); } //畫隨機干擾框 setSquareBackGround(g,width,height,3); //畫干擾點 CreateRandomPoint(width, height,100,g,100); //隨機畫幾條線 CreateRandomLine(width, height,8,g,100); // 釋放圖形上下文 g.dispose(); System.out.println("計算的結果是="+getResult2()); return image; } /** * 旋轉並且畫出指定字符串 * @param s 需要旋轉的字符串 * @param x 字符串的x坐標 * @param y 字符串的Y坐標 * @param g 畫筆g * @param degree 旋轉的角度 */ private void RotateString(String s,int x,int y,Graphics g,int degree) { Graphics2D g2d = (Graphics2D) g.create(); // 平移原點到圖形環境的中心 ,這個方法的作用實際上就是將字符串移動到某一個位置 //g2d.translate(x-1, y+3); g2d.translate(getIntRandom(x, x+2), getIntRandom(y,y+2)); // 旋轉文本 g2d.rotate(degree* Math.PI / 180); //特別需要注意的是,這里的畫筆已經具有了上次指定的一個位置,所以這里指定的其實是一個相對位置 g2d.drawString(s,0 , 0); } /** * 隨機產生干擾點 * @param width * @param height * @param many * @param g * @param alpha 透明度0~255 0表示全透 */ private void CreateRandomPoint(int width,int height,int many,Graphics g,int alpha) { // 隨機產生干擾點 for (int i=0;i<many;i++) { int x = rand.nextInt(width); int y = rand.nextInt(height); g.setColor(getColor(alpha)); g.drawOval(x,y,rand.nextInt(3),rand.nextInt(3)); } } /** * 隨機產生干擾線條 * @param width * @param height * @param minMany 最少產生的數量 * @param g * @param alpha 透明度0~255 0表示全透 */ private void CreateRandomLine(int width,int height,int minMany,Graphics g,int alpha) { // 隨機產生干擾線條 for (int i=0;i<getIntRandom(minMany, minMany+6);i++) { int x1 =getIntRandom(0,(int)(width*0.6)); int y1 =getIntRandom(0,(int)(height*0.6)); int x2 =getIntRandom((int)(width*0.4),width); int y2 =getIntRandom((int)(height*0.2),height); g.setColor(getColor(alpha)); g.drawLine(x1, y1, x2, y2); } } /** * 由隨機產生的方塊來作為干擾背景 */ private void setSquareBackGround(Graphics g,int width,int height,int count){ // 隨機產生干擾線條 for (int i=0;i<getIntRandom(count, count+2);i++) { int x1 =getIntRandom(0,(int)(width*0.3)); int y1 =getIntRandom(0,(int)(height*0.3)); int x2 =getIntRandom((int)(width*0.5),width); int y2 =getIntRandom((int)(height*0.55),height); g.setColor(getColor(100)); int w=x2-x1; int h=y2-y1; if(w<0) w=-w; if(h<0) h=-h; g.drawRect(x1, y1, w, h); g.setColor(getColor(25)); g.fillRect(x1, y1, w, h); } } /*** * @return 隨機返一個指定區間的數字 */ private int getIntRandom(int start,int end) { if(end<start) { int t=end; end=start; start=t; } int i=start+(int) (Math.random()*(end-start)); return i; } @SuppressWarnings("unused") private int getIntRandom(double start,double end) { if(end<start) { double t=end; end=start; start=t; } double i=start+(int) (Math.random()*(end-start)); return (int)i; } /*** 隨機返回一種顏色,透明度0~255 0表示全透 * @return 隨機返回一種顏色 * @param alpha 透明度0~255 0表示全透 */ 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); } /*** * @return 隨機返回一種顏色,與給定顏色相類似 * @param alpha 透明度0~255 0表示全透 */ private Color getColor(Color c,int alpha) { int R=getIntRandom(-140,140); int G=getIntRandom(-140,140); int B=getIntRandom(-140,140); R=getCloserRandom(c.getRed(),R); B=getCloserRandom(c.getBlue(),B); G=getCloserRandom(c.getGreen(),G); return new Color(R,G,B,alpha); } /** * 在顏色值和給定的隨機數之間返回一個隨機顏色值0~255 * @param colorValue * @param randomValue * @param deep,默認為0 * @return */ @SuppressWarnings("unused") private int getCloserRandom(int colorValue,int randomValue){ if(colorValue+randomValue>255) { return getCloserRandom(colorValue,randomValue-getIntRandom(1, randomValue+20)); }else if(colorValue+randomValue<0){ return getCloserRandom(colorValue,randomValue+getIntRandom(1, randomValue+20)); }else if(Math.abs(randomValue)<60) { return getCloserRandom(colorValue,getIntRandom(-255,255)); }else{ return colorValue+randomValue; } } /** * * @return 返回getVerificationCode2需要畫出的內容:兩位數加減法字符數組 */ private char[] getDrawContent2() { beijiashu=0; jiashu=0; char[] temp=new char[6]; char[] w =aa; int k=0; /** * 產生被加數 */ //從aa\bb\cc中選擇一個字符數組作為素材 k=(int)(Math.random()*4); w=aa; k=(int)(Math.random()*10); temp[0]=w[k]; if(k==0) temp[0]='!'; beijiashu+=k*10; k=(int)(Math.random()*10); temp[1]=w[k]; beijiashu+=k; /** * 產生加數 */ k=(int)(Math.random()*2); w=aa; k=(int)(Math.random()*10); temp[3]=w[k]; if(k==0) temp[3]='!'; jiashu=k*10+jiashu; k=(int)(Math.random()*10); temp[4]=w[k]; jiashu+=k; //選擇加減乘除 w=action; k=(int)(Math.random()*2 ); temp[2]=w[k]; js=k%2; //結果 w=jieguo; k=(int)(Math.random()*2); temp[5]=w[k]; return temp; } /** * 對圖片選擇,這里保留以方便以后使用 * @param bufferedimage * @param degree * @return 一張旋轉后的圖片 */ public BufferedImage rolateImage(BufferedImage bufferedimage,int degree,Color backGround) { BufferedImage img; int w = bufferedimage.getWidth(); int h = bufferedimage.getHeight(); int type = BufferedImage.TYPE_INT_RGB; Graphics2D graphics2d; graphics2d = (img = new BufferedImage(w, h, type)).createGraphics(); graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2); graphics2d.drawImage(bufferedimage,null, null); return img; } /** * 得到驗證碼getVerificationCode2,計算出來的結果 */ public int getResult2() { if(js==0) return(beijiashu+jiashu); else if(js==1) return (beijiashu-jiashu); return 0; } /** * * @param many * @return 畫圖的時候隨機的高度的數組 */ private int[] getRadomHeights(int height,int many){ int[] temp=new int[many]; for(int i=0;i<many;i++){ temp[i]=getRadomHeight(height); } return temp; } /** * * @param many * @return 畫圖的時候起始x坐標的數組 */ private int[] getRadonWidths(int width,int many) { int[] temp=new int[many]; for(int i=0;i<many;i++){ if(i==0) temp[i]=getRadonWidth(many,0,width); else temp[i]=getRadonWidth(many,temp[i-1],width); } return temp; } private int getRadomHeight(int fullHeight) { return getIntRandom((int)(fullHeight*0.2), (int)(fullHeight*0.75)); } private int getRadonWidth(int many,int minWidth, int maxWidth) { int minJianju=maxWidth/(many+2); int maxJianju=maxWidth/(many); int temp=maxJianju-minJianju; //在的規定的范圍內產生一個隨機數 return (int)(Math.random()*temp)+minWidth+minJianju; } }
說明,干擾信息,如干擾點,干擾框等待在文字和背景的前后都加入理論效果更高;
通過設置圖片大小,顏色透明度,干擾數量,文字大小,旋轉角度等等,可以自行下載定制;
效果圖:
干擾點:
干擾框/點/線圖:
不加入干擾的效果:
干擾框的圖:
調整驗證碼大小和字體大小之后的圖: