java生成验证码SWT_Java SWT Lable框中显示图片验证码
public static String testcode="";//驗證碼,保存驗證碼,取這個值于用戶輸入的值作比較
public static void image(Label label) throws IOException{
//BufferedImage 的構造函數可以設置圖片的大小
BufferedImage image = new BufferedImage(label.getSize().x, label.getSize().y,BufferedImage.TYPE_INT_RGB);//這里設置圖片的大小
//這里需要使用到java.awt.Graphics來繪制圖片
java.awt.Graphics graphics = image.getGraphics();
Color color = new Color(245, 245, 220);
graphics.setColor(color);//為圖片添加的底色
graphics.fillRect(0,0,label.getSize().x,label.getSize().y);
char[] content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
Random random = new Random();
int index;
for(int i=0;i<4;i++){//驗證碼長度
index = random.nextInt(content.length);
testcode+=String.valueOf(content[index]);//testcode是驗證碼
//圖片中文字的顏色
graphics.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
//圖片中文字的位置
graphics.drawString(content[index]+"",10+20*i,10+3*random.nextInt(4));
//1,驗證碼文字,2文字距離上邊的距離3,距離下部分的距離,可以更改這后面的兩個數據,來改變圖片的,驗證碼顯示位置
}
ByteArrayOutputStream stream=new ByteArrayOutputStream();
ImageIO.write(image, "jpg", stream);
InputStream inputStream=new ByteArrayInputStream(stream.toByteArray());
label.setImage(new Image(null, new ImageData(inputStream).scaledTo(label.getSize().x, label.getSize().y)));
}
總結
以上是生活随笔為你收集整理的java生成验证码SWT_Java SWT Lable框中显示图片验证码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CUDA从入门到精通(三):必备资料
- 下一篇: java web高级编程 百度网盘_[有