java生成验证码SWT_Java SWT Lable框中显示图片验证码
public static String testcode="";//驗(yàn)證碼,保存驗(yàn)證碼,取這個(gè)值于用戶輸入的值作比較
public static void image(Label label) throws IOException{
//BufferedImage 的構(gòu)造函數(shù)可以設(shè)置圖片的大小
BufferedImage image = new BufferedImage(label.getSize().x, label.getSize().y,BufferedImage.TYPE_INT_RGB);//這里設(shè)置圖片的大小
//這里需要使用到j(luò)ava.awt.Graphics來(lái)繪制圖片
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++){//驗(yàn)證碼長(zhǎng)度
index = random.nextInt(content.length);
testcode+=String.valueOf(content[index]);//testcode是驗(yàn)證碼
//圖片中文字的顏色
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,驗(yàn)證碼文字,2文字距離上邊的距離3,距離下部分的距離,可以更改這后面的兩個(gè)數(shù)據(jù),來(lái)改變圖片的,驗(yàn)證碼顯示位置
}
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)));
}
總結(jié)
以上是生活随笔為你收集整理的java生成验证码SWT_Java SWT Lable框中显示图片验证码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CUDA从入门到精通(三):必备资料
- 下一篇: CUDA从入门到精通(四):加深对设备的