Created loading and saving of glyphs (#1)
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
4037757857
commit
8300b450e7
@ -5,17 +5,40 @@ import java.io.IOException;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.file.StandardOpenOption;
|
||||||
|
import java.util.List;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
|
|
||||||
public class Font {
|
public class Font {
|
||||||
Glyph[] data = new Glyph[9];
|
Glyph[] data = new Glyph[10];
|
||||||
final static int TRANSPARENT = new Color(0,0,0,0).getRGB();
|
final static int TRANSPARENT = new Color(0,0,0,0).getRGB();
|
||||||
|
|
||||||
public static Font LoadFont(String fontName) {
|
public static Font LoadFont(String fontName) {
|
||||||
Path f = Paths.get("readers","fonts",fontName);
|
Path f = Paths.get("readers","fonts",fontName);
|
||||||
if (Files.exists(f)) {
|
if (Files.exists(f)) {
|
||||||
//TODO Read the file.
|
Font font = new Font();
|
||||||
return new Font();
|
try {
|
||||||
|
List<String> data = Files.readAllLines(f);
|
||||||
|
for (int i=0;i<data.size();i+=3) {
|
||||||
|
Glyph g = new Glyph();
|
||||||
|
g.width=Integer.parseInt(data.get(i));
|
||||||
|
g.height=Integer.parseInt(data.get(i+1));
|
||||||
|
String dataArr = data.get(i+2);
|
||||||
|
g.data=new boolean[dataArr.length()];
|
||||||
|
for (int j=0;j<dataArr.length();j++) {
|
||||||
|
g.data[j]=(dataArr.charAt(j)-'0')=='1'?true:false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return font;
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
Files.createFile(f);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return new Font();
|
return new Font();
|
||||||
}
|
}
|
||||||
@ -23,8 +46,6 @@ public class Font {
|
|||||||
public static void TrainFont(String fontName,String expectedGlyphs,BufferedImage data) {
|
public static void TrainFont(String fontName,String expectedGlyphs,BufferedImage data) {
|
||||||
Path f = Paths.get("readers","fonts",fontName);
|
Path f = Paths.get("readers","fonts",fontName);
|
||||||
Font font = LoadFont(fontName);
|
Font font = LoadFont(fontName);
|
||||||
try {
|
|
||||||
Files.createFile(f);
|
|
||||||
int startX=-1;
|
int startX=-1;
|
||||||
int endX=-1;
|
int endX=-1;
|
||||||
outer:
|
outer:
|
||||||
@ -69,8 +90,32 @@ public class Font {
|
|||||||
font.data[index]=g;
|
font.data[index]=g;
|
||||||
System.out.println("Glyph for number "+index+" has been saved.");
|
System.out.println("Glyph for number "+index+" has been saved.");
|
||||||
expectedGlyphs=expectedGlyphs.substring(1);
|
expectedGlyphs=expectedGlyphs.substring(1);
|
||||||
|
startX=-1;
|
||||||
|
endX=-1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
saveFont(font,fontName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void saveFont(Font f,String name) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (int i=0;i<f.data.length;i++) {
|
||||||
|
Glyph g = f.data[i];
|
||||||
|
if (g==null) {
|
||||||
|
g=new Glyph();
|
||||||
|
}
|
||||||
|
sb
|
||||||
|
.append(g.width)
|
||||||
|
.append('\n')
|
||||||
|
.append(g.height)
|
||||||
|
.append('\n');
|
||||||
|
for (int j=0;j<g.data.length;j++) {
|
||||||
|
sb.append(g.data[j]?"1":"0");
|
||||||
|
}
|
||||||
|
sb.append('\n');
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Files.write(Paths.get("readers","fonts",name),sb.toString().getBytes(),StandardOpenOption.TRUNCATE_EXISTING);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package readers.fonts;
|
package readers.fonts;
|
||||||
|
|
||||||
public class Glyph {
|
public class Glyph {
|
||||||
int width;
|
int width=0;
|
||||||
int height;
|
int height=0;
|
||||||
boolean[] data;
|
boolean[] data = new boolean[]{};
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
54
|
||||||
|
51
|
||||||
|
000000000000000000001111111111111111111111111110000000000000000000000000111111111111111111111111111100000000000000000000000001111111111111111111111111111111111100000000000000000011111111111111111111111111111111111100000000000000000111111111111111111111111111111111111100000000000000001111111111111111111111111111111111111100000000000000011111111111111111111111111111111111111100000000000000111111111111111111111111111111111111111100000000000000111111111111111111111111111111111111111110000000000000111111111111111111111111111111111111111110000000000111111111111111111111111111111111111111111110000000000111111111111111111111111111111111111111111110000000001111111111111111111111111111111111111111111111000000011111111111111111111111111111111111111111111111000000011111111111111111111111111111111111111111111111000000111111100000000000000000000000000111111111111111000001111111100000000000000000000000000111111111111111000011111111100000000000000000000000000111111111111111000111111111100000000000000000000000000111111111111111000111111111000000000000000000000000000111111111111111001111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111111111111111111000000000000000000000000000111111111111110111111111111000000000000000000000000000111111111111110111111111111000000000000000000000000000111111111111100111111111111000000000000000000000000000111111111111000111111111111000000000000000000000000000111111111110000111111111111000000000000000000000000000111111111100000111111111111000000000000000000000000000111111111000000111111111111000000000000000000000111111111111110000000111111111111111111111111111111111111111111111100000000111111111111111111111111111111111111111111111000000000111111111111111111111111111111111111111111110000000000111111111111111111111111111111111111111111100000000000111111111111111111111111111111111111111100000000000000111111111111111111111111111111111111111100000000000000111111111111111111111111111111111111110000000000000000111111111111111111111111111111111111100000000000000000111111111111111111111111111111111111000000000000000000111111111111111111111111111111111110000000000000000000111111111111111111111111111111111100000000000000000000
|
||||||
|
0
|
||||||
|
0
|
||||||
|
|
||||||
|
0
|
||||||
|
0
|
||||||
|
|
||||||
|
51
|
||||||
|
51
|
||||||
|
011111111111111111111111111111111111100000000000000111111111111111111111111111111111111111000000000000111111111111111111111111111111111111111000000000000111111111111111111111111111111111111111000000000000111111111111111111111111111111111111111000000000000111111111111111111111111111111111111111000000000000111111111111111111111111111111111111111000000000000011111111111111111111111111111111111111000000000000011111111111111111111111111111111111111000000000000011111111111111111111111111111111111111000000000000011111111111111111111111111111111111111000000000000011111111111111111111111111111111111111000000000000011111111111111111111111111111111111111100000000000011111111111111111111111111111111111111110000000000000000001111111111111111111111111111111111100000000000000000000000000000000000000000000111111111000000000000000000000000000000000000000000111111111110000000000000000000000000000000000000000111111111111000000000000000000000000000000000000000111111111111110000000000000000001111111111111111001111111111111110000000000000001111111111111111111111111111111111111000000000000001111111111111111111111111111111111111000000000000011111111111111111111111111111111111111000000000000111111111111111111111111111111111111111000000000001111111111111111111111111111111111111111000000000011111111111111111111111111111111111111111000000000111111111111111111111111110111111111111111000000001111111111111111111111111100111111111111111000000011111111111111111111111111000111111111111111000000111111111111111111111111110000111111111111111000001111111111111111111111111100000111111111111111000011111111111111111111111111000000111111111111111000011111111111111111111111110000000111111111111111000000000000000000000000000000000000111111111111111000000000000000000000000000000000000111111111111111000000000000000000000000000000000000111111111111111000000000000000000000000000000000000111111111111111000000000000000000000000000000000000111111111111111000000000000000000000000000000000000111111111111111000000000000000000000000000000000111111111111111111011111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111110011111111111111111111111111111111111110011111111000011111111111111111111111111111111110000000000000000011111111111111111111111111111111100000000000000000011111111111111111111111111111111000000000000000000011111111111111111111111111111110000000000000000000011111111111111111111111111111100000000000000000000011111111111111111111111111111000000000000000000000011111111111111111111111111110000000000000000000000011111111111111111111111111100000000000000000000000
|
||||||
|
0
|
||||||
|
0
|
||||||
|
|
||||||
|
0
|
||||||
|
0
|
||||||
|
|
||||||
|
0
|
||||||
|
0
|
||||||
|
|
||||||
|
0
|
||||||
|
0
|
||||||
|
|
||||||
|
0
|
||||||
|
0
|
||||||
|
|
||||||
|
54
|
||||||
|
51
|
||||||
|
000000000000000000000011111111111111111110000000000000000000000000000000000111111111111111111111100000000000000000000000000000011111111111111111111111110000000000000000000000000000111111111111111111111111111000000000000000000000000001111111111111111111111111111100000000000000000000000011111111111111111111111111111110000000000000000000000111111111111111111111111111111111000000000000000000001111111111111111111111111111111111100000000000000000011111111111111111111111111111111111110000000000000000111111111111111111111111111111111111111000000000000001111111111111111111111111111111111111111100000000000011111111111111111111111111111111111111111110000000000011111111111111111111111111111111111111111110000000000111111111111111111111111111111111111111111111000000001111111111111111111111111111111111111111111111000000001111100000000000000000000000000111111111111111000000011111100000000000000000000000000111111111111111000000011111100000000000000000000000000111111111111111000000111111100000000000000000000000000111111111111111000001111111000000000000000000000000000111111111111111000011111111000000000000000000000000000111111111111111000111111111000000000000000000000000000111111111111111001111111111000000000000000000000000000111111111111111011111111111000001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111100111111111111111111111111111111111111111111111111111000111111111111111111111111111111111111111111111111110000111111111111111111111111111111111111111111111111100000111111111111111111111111111111111111111111111110000000111111111111111000000000000000000000000000000100000000111111111111111000000000000000000000000000000000000000111111111111111000000000000000000000000000000000000000111111111111111000000000000000000000000000000000111111111111111111111000000000001111111111111111111111111111111111111111111000000000001111111111111111111111111111111111111111111000000000001111111111111111111111111111111111111111110000000000001111111111111111111111111111111111111111100000000000001111111111111111111111111111111111111111000000000000001111111111111111111111111111111111111110000000000000001111111111111111111111111111111111111100000000000000001111111111111111111111111111111111111000000000000000001111111111111111111111111111111111100000000000000000001111111111111111111111111111111111000000000000000000001111111111111111111111111111111100000000000
|
@ -1,3 +1,4 @@
|
|||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.file.CopyOption;
|
import java.nio.file.CopyOption;
|
||||||
@ -88,8 +89,10 @@ public class sigPlace {
|
|||||||
|
|
||||||
int[] arr = img.getRGB(scoreBox.x, scoreBox.y, scoreBox.w, scoreBox.h, null, 0, scoreBox.w);
|
int[] arr = img.getRGB(scoreBox.x, scoreBox.y, scoreBox.w, scoreBox.h, null, 0, scoreBox.w);
|
||||||
|
|
||||||
final ColorRange TARGETCOLOR = new ColorRange(240,255,240,255,240,255);
|
BufferedImage newImg = new BufferedImage(scoreBox.w,scoreBox.h,BufferedImage.TYPE_INT_ARGB);
|
||||||
final ColorRange SEEKINGCOLOR = new ColorRange(100,255,100,255,100,255);
|
|
||||||
|
final ColorRange TARGETCOLOR = new ColorRange(160,255,170,255,190,255);
|
||||||
|
final ColorRange SEEKINGCOLOR = new ColorRange(26,255,53,255,80,255);
|
||||||
final Color FINALCOLOR = Color.MAGENTA;
|
final Color FINALCOLOR = Color.MAGENTA;
|
||||||
for (int i=0;i<arr.length;i++) {
|
for (int i=0;i<arr.length;i++) {
|
||||||
Color col = new Color(arr[i],true);
|
Color col = new Color(arr[i],true);
|
||||||
@ -103,8 +106,9 @@ public class sigPlace {
|
|||||||
arr[i]=TRANSPARENT;
|
arr[i]=TRANSPARENT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
img.setRGB(0,0,scoreBox.w,scoreBox.h,arr,0,scoreBox.w);
|
newImg.setRGB(0,0,scoreBox.w,scoreBox.h,arr,0,scoreBox.w);
|
||||||
Font.TrainFont("testFont","930",img);
|
ImageIO.write(newImg,"png",new File("test.png"));
|
||||||
|
Font.TrainFont("testFont","930",newImg);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user