Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ava;
- import java.awt.Color;
- import java.awt.image.BufferedImage;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Random;
- import javax.imageio.ImageIO;
- public class AvatarGenerator extends Thread {
- private ArrayList<String> userNames;
- private long seed;
- public static final int AVATAR_XSIZE = 10;
- public static final int AVATAR_YSIZE = 10;
- public AvatarGenerator(String s1, String seedString, boolean multipleFiles) {
- userNames = new ArrayList<String>();
- seed = djb2(seedString);
- if(!multipleFiles) {
- userNames.add(s1);
- return;
- }
- try{
- BufferedReader br = new BufferedReader(new FileReader(s1));
- String currentUserName = null;
- while((currentUserName = br.readLine()) != null) {
- userNames.add(currentUserName);
- }
- br.close();
- }catch(IOException e) {
- System.out.println("Error: Could not read usernames file.");
- System.exit(1);
- }
- }
- public void run() {
- for(int i=0; i<userNames.size(); i++) {
- String userName = userNames.get(i);
- String outputFileName = userName + ".bmp";
- Random random = new Random(seed + djb2(userName));
- float H = Math.abs(random.nextFloat())%1f;
- float S = (Math.abs(random.nextFloat())%0.5f)+0.5f;
- float B = (Math.abs(random.nextFloat())%0.3f)+0.7f;
- int forgroundColor = (new Color(50,50,50)).getRGB();
- int[][] avatar = new int[AVATAR_XSIZE][AVATAR_YSIZE];
- for(int x=0; x<AVATAR_XSIZE/2; x++) {
- for(int y=0; y<AVATAR_YSIZE; y++) {
- boolean choice = random.nextBoolean();
- if(choice) {
- float mod1 = ((Math.abs(random.nextFloat()))%0.1f)-0.05f;
- avatar[x][y] = Color.HSBtoRGB(H+mod1, S, Math.min(Math.max(B+mod1,0.0f),1.0f));
- float mod2 = ((Math.abs(random.nextFloat()))%0.1f)-0.05f;
- avatar[AVATAR_XSIZE-x-1][y] = Color.HSBtoRGB(H+mod2, S, Math.min(Math.max(B+mod2,0.0f),1.0f));
- }else{
- avatar[x][y] = forgroundColor;
- avatar[AVATAR_XSIZE-x-1][y] = forgroundColor;
- }
- }
- }
- writeImage(outputFileName, avatar);
- }
- }
- private void writeImage(String outputFilename, int[][] avatar) {
- File outputFile = new File(outputFilename);
- int PIXELX = 5;
- int PIXELY = 5;
- BufferedImage theImage = new BufferedImage(AVATAR_XSIZE*PIXELX, AVATAR_YSIZE*PIXELY, BufferedImage.TYPE_INT_RGB);
- for(int x=0; x<AVATAR_XSIZE; x++) {
- for(int y=0; y<AVATAR_YSIZE; y++) {
- for(int px=0; px<PIXELX; px++) {
- for(int py=0; py<PIXELY; py++) {
- theImage.setRGB(PIXELX*x + px, PIXELY*y + py, avatar[x][y]);
- }
- }
- }
- }
- try{
- ImageIO.write(theImage, "BMP", outputFile);
- }catch(IOException e) {
- System.out.println("Error: Could not write file.");
- System.exit(1);
- }
- }
- private long djb2(String stringToHash) {
- long hash = 5381;
- int c;
- for(int i=0; i<Math.max(stringToHash.length(),8); i++) {
- c = stringToHash.charAt(i % stringToHash.length());
- hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
- }
- return hash;
- }
- public static void main(String[] args) {
- if(args.length == 2) {
- AvatarGenerator instance = new AvatarGenerator(args[0], args[1], false);
- instance.start();
- }else if(args.length == 3) {
- if("-m".equals(args[2])) {
- AvatarGenerator instance = new AvatarGenerator(args[0], args[1], true);
- instance.start();
- return;
- }else{
- System.out.println("Error: Unknown flag.");
- }
- }else{
- System.out.println("Usage:\n\t$ AvatarGenerator <username> <seed>");
- System.out.println("OR\n\t$ AvatarGenerator <file with usernames> <seed> -m");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment