My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.sioti.batch;

import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import javax.imageio.ImageIO;

public class MapCreator {

/**
* @param args
* @throws InterruptedException
* @throws IOException
*/
public static void main(String[] args) throws InterruptedException,
IOException {
// This method ensures that all pixels have been loaded before returning
// Image image = new ImageIcon("image.gif").getImage();
BufferedImage image = ImageIO.read(new File("docs/mapa.bmp"));

// Get the dimensions of the image; these will be non-negative
int width = image.getWidth(null);
int height = image.getHeight(null);

// identify the colors
char node = 'A';
Map colors = new TreeMap();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {

String hex = getColor(image, x, y);
if (colors.get(hex) == null) {
colors.put(hex, Character.valueOf(node++));
}
}
}

// show the colors
Iterator iter = colors.keySet().iterator();
while (iter.hasNext()) {
Object key = iter.next();
System.out.println(key);
System.out.println(colors.get(key));
}

BufferedWriter writer = new BufferedWriter(new FileWriter(
"docs/mapa.txt"));
// write the file
for (int y = 0; y < height; y++) {
StringBuffer line = new StringBuffer(width);
for (int x = 0; x < width; x++) {
String hex = getColor(image, x, y);
char toWrite = ((Character) colors.get(hex)).charValue();
line.append(toWrite);
}
writer.write(line.toString());
writer.write(Character.LINE_SEPARATOR);
}

writer.flush();
writer.close();
}

private static String getColor(BufferedImage image, int x, int y) {
int c = image.getRGB(x, y);
int red = (c & 0x00ff0000) >> 16;
int green = (c & 0x0000ff00) >> 8;
int blue = c & 0x000000ff;
String hex = Integer.toHexString(red) + Integer.toHexString(green)
+ Integer.toHexString(blue);
hex = hex.toUpperCase();
return hex;
}

}

Change log

r2 by hamilton.lima on Jun 23, 2008   Diff
first commit
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 2209 bytes, 81 lines
Powered by Google Project Hosting