Co-authored-by: sigonasr2 <sigonasr2@gmail.com>main
parent
d3c671a37a
commit
4037757857
@ -0,0 +1,78 @@ |
||||
package readers.fonts; |
||||
|
||||
import java.awt.image.BufferedImage; |
||||
import java.io.IOException; |
||||
import java.nio.file.Files; |
||||
import java.nio.file.Path; |
||||
import java.nio.file.Paths; |
||||
import java.awt.Color; |
||||
|
||||
public class Font { |
||||
Glyph[] data = new Glyph[9]; |
||||
final static int TRANSPARENT = new Color(0,0,0,0).getRGB(); |
||||
|
||||
public static Font LoadFont(String fontName) { |
||||
Path f = Paths.get("readers","fonts",fontName); |
||||
if (Files.exists(f)) { |
||||
//TODO Read the file.
|
||||
return new Font(); |
||||
} |
||||
return new Font(); |
||||
} |
||||
|
||||
public static void TrainFont(String fontName,String expectedGlyphs,BufferedImage data) { |
||||
Path f = Paths.get("readers","fonts",fontName); |
||||
Font font = LoadFont(fontName); |
||||
try { |
||||
Files.createFile(f); |
||||
int startX=-1; |
||||
int endX=-1; |
||||
outer: |
||||
for (int x=0;x<data.getWidth();x++) { |
||||
for (int y=0;y<data.getHeight();y++) { |
||||
if (data.getRGB(x, y)!=TRANSPARENT) { |
||||
if (startX==-1) { |
||||
startX=x; |
||||
} |
||||
continue outer; |
||||
} |
||||
} |
||||
//If we got to here it means we found transparent on everything.
|
||||
if (startX!=-1) { |
||||
endX=x; |
||||
//Create a Glyph out of this.
|
||||
Glyph g = new Glyph(); |
||||
g.width=endX-startX; |
||||
g.height=data.getHeight(); |
||||
int[] arr = data.getRGB(startX,0,g.width,g.height,null,0,g.width); |
||||
//Find the min and max Y.
|
||||
int startY=g.height; |
||||
int endY=0; |
||||
for (int X=0;X<g.width;X++) { |
||||
for (int Y=0;Y<g.height;Y++) { |
||||
if (arr[Y*g.width+X]!=TRANSPARENT&&Y<startY) { |
||||
startY=Y; |
||||
} |
||||
if (arr[Y*g.width+X]!=TRANSPARENT&&Y>endY) { |
||||
endY=Y; |
||||
} |
||||
} |
||||
} |
||||
g.height=endY-startY; |
||||
g.data = new boolean[g.width*g.height]; |
||||
for (int X=0;X<g.width;X++) { |
||||
for (int Y=0;Y<g.height;Y++) { |
||||
g.data[Y*g.width+X]=arr[(Y+startY)*g.width+X]!=TRANSPARENT; |
||||
} |
||||
} |
||||
int index = expectedGlyphs.charAt(0)-'0'; |
||||
font.data[index]=g; |
||||
System.out.println("Glyph for number "+index+" has been saved."); |
||||
expectedGlyphs=expectedGlyphs.substring(1); |
||||
} |
||||
} |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,7 @@ |
||||
package readers.fonts; |
||||
|
||||
public class Glyph { |
||||
int width; |
||||
int height; |
||||
boolean[] data; |
||||
} |
Loading…
Reference in new issue