0

我正在尝试根据 Random() 方法的生成显示随机图片(只是尝试像验证码之类的东西)。我正在从数组中生成随机编号,但不知道如何将这些编号与图像映射。有任何想法吗 ?到目前为止我所做的:

导入 java.util.Arrays;导入 java.util.Random;

公共类 Gen_Captcha {

static String[] myCaptchaLetters = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9"};
Gen_Captcha captcha= new Gen_Captcha();
public static void main(String[] args) {
    Random randomGenerator = new Random();
    for(int i=0;i<5;i++){
        int random = randomGenerator.nextInt(62);
        System.out.print((myCaptchaLetters[random]));       
    }

}

}

}

注意:以上代码不包含显示图片的swing框架代码。

4

3 回答 3

2

创建一个List<BufferedImage>, 并用于Collections.shuffle()随机化列表。使用列表的迭代器将随机访问图像而不会重复。

于 2013-06-21T15:03:59.497 回答
1

您可能想看看Graphics.drawString()

您可以通过创建BufferedImage来获取 graphics2D 对象 注意也有更改字体的方法。对于更高级的验证码,您可以使用相同的 api 扭曲图像并在其上绘制随机内容。

于 2013-06-21T14:58:49.927 回答
1

但不知道如何用图像映射这些不

不要映射数字,映射字母:

Map<String, Image> images = new HashMap<String, Image>();
images.put("a", imageA);
images.put("b", imageB);
于 2013-06-21T14:52:50.557 回答