1. 程式人生 > 資訊 >LG 推出 gram 便攜顯示器:16 英寸 2K 解析度, 99% DCI-P3 色域

LG 推出 gram 便攜顯示器:16 英寸 2K 解析度, 99% DCI-P3 色域

驗證碼

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;

//這個類就是一個關於驗證碼的類
public class VerifyCode {
//對外提供的功能有這麼三個,我們把他們分別定義成方法,並且是public
//1.生成驗證碼
//2.輸出驗證碼
//3.對外提供驗證碼的正確答案

private int w=150;//寬度
private int h=70;//高度
private Random r=new Random();//獲取一個隨機數物件
//定義有哪些字型
private String[]fontNames= {"宋體","華文楷體","黑體","微軟雅黑","楷體_GB2312"};
//定義有哪些驗證碼的隨機字元
private String codes="23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
//生成背景色
private Color bgColor =new Color(250,250,250);
//生成驗證碼文字
private String text;
//生成隨機顏色
private Color randomColor() {
int red=r.nextInt(150);
int green=r.nextInt(150);
int blue=r.nextInt(150);
return new Color(red,green,blue);
}
//生成隨機字型
private Font randomFont() {
int index=r.nextInt(fontNames.length);
String fontName=fontNames[index];//字元名稱
int style=r.nextInt(4);//字元樣式(PLAIN=0,BOLD=1,ITALIC=2,BOLDITALIC=3)
int size=r.nextInt(5)+50;//字型大小()50-54之間
return new Font(fontName,style,size);
}
//畫干擾線
private void drawLine(BufferedImage image) {
int num=3;//干擾線數量
Graphics2D g2 = (Graphics2D) image.getGraphics(); // 得到圖片畫筆
for (int i = 0; i < num; i++) {
int x1 = r.nextInt(w); // 起點x座標
int y1 = r.nextInt(h); // 起點y座標
int x2 = r.nextInt(w); // 終點x座標
int y2 = r.nextInt(h); // 終點y座標
g2.setStroke(new BasicStroke(1.5F));// 設定線條特徵,1.5F為線的寬度
g2.setColor(Color.BLUE); // 干擾線顏色
g2.drawLine(x1, y1, x2, y2); // 畫線
}
}
// 得到codes的長度內的隨機數,並使用charAt取得隨機數位置上的codes中的字元
private char randomChar() {
int index = r.nextInt(codes.length());
return codes.charAt(index);
}
// 建立一張驗證碼的圖片
public BufferedImage createImage() {
// 得到一個圖片緩衝區
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) image.getGraphics(); // 得到圖片畫筆
g2.setColor(bgColor); // 給畫筆設定顏色
g2.fillRect(0, 0, w, h); // 使用畫筆填充指定的矩形
g2.setColor(Color.RED); // 給畫筆設定顏色
g2.drawRect(0, 0, w -1, h -1); // 使用畫筆繪製指定的矩形
StringBuilder sb = new StringBuilder();
// 向圖中畫四個字元
for (int i = 0; i < 4; i++) {
String s = randomChar() + "";
sb.append(s);
int x = i * w / 4; // 計算字元x座標位置
g2.setFont(randomFont()); // 設定畫筆字型
g2.setColor(randomColor()); // 設定畫筆顏色
g2.drawString(s, x, h -20); // 在圖片上寫字元
}
text = sb.toString();
drawLine(image); // 繪製干擾線
return image;// 返回圖片
}
// 得到驗證碼的文字,用來驗證使用者輸入的驗證碼
public String getText() {
return text;
}
// 定義輸出的物件和輸出檔案流
//注意第2個引數型別指定為OutputStream,以後可以用於向response中輸出圖片
public void output(BufferedImage bi, OutputStream output)throws IOException {
ImageIO.write(bi, "JPEG", output);
}
}

 

測試類

import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;

public class VerifyCodeTest {
public static void main(String[] args) throws IOException {
VerifyCode code = new VerifyCode();
BufferedImage image = code.createImage();// 獲得驗證碼圖片物件
code.output(image, new FileOutputStream("D:/javaweb/image.jpg"));
System.out.println("驗證碼的真實文字是:"+ code.getText());// 輸出驗證碼文字}
}

}