java圖片驗證碼包括文字和圖片的旋轉
阿新 • • 發佈:2019-02-12
java圖片驗證碼包括文字圖片的旋轉:
此例子演示的是兩位數的加減運算,需要的可以通過自己的修改獲得更多的方式:
或者我上傳的資源中也有其他的兩種方式供選擇:http://download.csdn.net/detail/huitoukest/8043711
package com.utils; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class VerificationCode { private int result2=0; Random rand = new Random(); /** * 隨機產生的加數和被加數 */ private int jiashu=0; private int beijiashu=0; /** * 隨機產生的計算方式,0表示加,1表示減 */ private int js=0; private char[] aa={'零','壹','貳','叄','肆','伍','陸','柒','捌','玖'}; private char[] bb={'0','1','2','3','4','5','6','7','8','9'}; private char[] cc={'〇','一','二','三','四','五','六','七','八','九'}; private char[] action={'加','減','+','-'}; private char[] jieguo={'等','是'}; public static void main(String[] args) { // TODO Auto-generated method stub VerificationImage code=new VerificationImage(); 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=150; int height=70; int degree=0;//繼續一共旋轉的角度,方便最後的時候旋轉回來 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 獲取圖形上下文 Graphics g = image.getGraphics(); // 設定背景色 Color background=getColor(); 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(content.length); int[] ys=getRadomHeights(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()); if(i==2)//運算子號顯示大一些 g.setFont(new Font("Atlantic Inline",Font.PLAIN,24)); else g.setFont(new Font("Atlantic Inline",Font.PLAIN,18)); RotateString(s, xs[i], ys[i], g, degree); } //畫干擾點 CreateRandomPoint(width, height, 80, g); //隨機畫幾條線 CreateRandomLine(width, height,4,g); // 釋放圖形上下文 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.rotate(degree* Math.PI / 180); //特別需要注意的是,這裡的畫筆已經具有了上次指定的一個位置,所以這裡指定的其實是一個相對位置 g2d.drawString(s,0 , 0); } /** * * @param width * @param height * @param many * @param g */ private void CreateRandomPoint(int width,int height,int many,Graphics g) { // 隨機產生干擾點 for (int i=0;i<many;i++) { int x = rand.nextInt(width); int y = rand.nextInt(height); g.setColor(getColor()); g.drawOval(x,y,1,1); } } /** * * @param width * @param height * @param minMany 最少產生的數量 * @param g */ private void CreateRandomLine(int width,int height,int minMany,Graphics g) { // 隨機產生干擾線條 for (int i=0;i<rand.nextInt(minMany)+5;i++) { int x1 = rand.nextInt(width)%15; int y1 = rand.nextInt(height); int x2 = (int) (rand.nextInt(width)%40+width*0.7); int y2 = rand.nextInt(height); g.setColor(getColor()); g.drawLine(x1, y1, x2, y2); } } /*** * @return 隨機返回一種顏色 */ private Color getColor() { int R=(int) (Math.random()*255); int G=(int) (Math.random()*255); int B=(int) (Math.random()*255); return new Color(R,G,B); } /** * * @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); if(k==0) w=aa; else if(k==1) w=bb; else if(k==3) w=cc; 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; /** * 產生加數 */ //從aa\bb\cc中選擇一個字元陣列作為素材 k=(int)(Math.random()*4); if(k==0) w=aa; else if(k==1) w=bb; else if(k==3) w=cc; 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()*4 ); temp[2]=w[k]; js=k%2; //結果 w=jieguo; k=(int)(Math.random()*2); temp[5]=w[k]; //System.out.println(new String(temp)); 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 many){ int[] temp=new int[many]; for(int i=0;i<many;i++){ temp[i]=getRadomHeight(); } return temp; } /** * * @param many * @return 畫圖的時候起始x座標的陣列 */ private int[] getRadonWidths(int many) { int[] temp=new int[many]; for(int i=0;i<many;i++){ if(i==0) temp[i]=getRadonWidth(0); else temp[i]=getRadonWidth(temp[i-1]); } return temp; } private int getRadomHeight() { int fullHeight=70; return (int)(Math.random()*fullHeight)%35+15; } private int getRadonWidth(int minWidth) { int maxWidth=150; int minJianju=maxWidth/9; int maxJianju=maxWidth/6; int temp=maxJianju-minJianju; //在的規定的範圍內產生一個隨機數 return (int)(Math.random()*temp)+minWidth+minJianju; } }
執行結果: