简体   繁体   English

使用Java显示随机图片

[英]display random pictures using java

I am trying to display random pictures based on Random() method's generation(just trying something like captcha). 我试图显示基于Random()方法的生成的随机图片(只是尝试像验证码)。 I am generating random no from an array, but dont know how to map these no's with images. 我正在从数组中生成随机否,但不知道如何用图像映射这些否。 Any ideas ? 有任何想法吗 ? What I've done till now: 到目前为止,我所做的是:

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

public class Gen_Captcha { 公共课程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]));       
    }

}

} }

} }

Note: The above code does not include the swing framework code to display image. 注意:上面的代码不包括显示图像的swing框架代码。

Create a List<BufferedImage> , and use Collections.shuffle() to randomize the list. 创建一个List<BufferedImage> ,然后使用Collections.shuffle()将列表随机化。 Using the list's iterator will visit the images randomly without duplicates. 使用列表的迭代器将随机访问图像,而不会重复。

but dont know how to map these no's with images 但不知道如何用图像映射这些否

Don't map the numbers, map the letters: 不要映射数字,请映射字母:

Map<String, Image> images = new HashMap<String, Image>();
images.put("a", imageA);
images.put("b", imageB);

You might want to take a look at Graphics.drawString() 您可能想看看Graphics.drawString()

You can get a graphics2D object by creating a BufferedImage Note there are also methods to change the font. 您可以通过创建BufferedImage来获取graphics2D对象。请注意,还有一些更改字体的方法。 for more advanced captcha's you can then warp the image and draw random stuff on it as well using the same api's. 对于更高级的验证码,您可以使用相同的api扭曲图像并在其上绘制随机内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM