Hallo an alle,
Ich schreibe gerade ein Tool, das mir einen Zeichensatz mit einer
bestimmten Font in ein "Pixel-Array" umwandelt, um es für ein LCD zu
nutzen.
Dazu habe ich eine Klasse, die mir genau ein Zeichen Umwandelt. Ich gebe
das konvertierte Zeichen per Ascii-Art auf der Konsole aus. Leider
werden manche Zeichen unvollständig erzeugt:
H:
1 |
|
2 | #
|
3 | #
|
4 | #
|
5 | #
|
6 | #######
|
7 | ## #
|
8 | #
|
9 | #
|
10 | #
|
11 | #
|
N:
1 |
|
2 | ##
|
3 | ##
|
4 | ###
|
5 | # ##
|
6 | # #
|
7 | # #
|
8 | # ##
|
9 | # ##
|
10 | # #
|
11 | # #
|
Die beiden Zeichen sind Arial, 14.
Hierzu der Konstruktor der Klasse:
1 | public LcdGlyph(String c, Font f)
|
2 | {
|
3 | this.c = c;
|
4 | this.f = f;
|
5 |
|
6 | FontRenderContext fontRendContext = new FontRenderContext(null, true, true);
|
7 | GlyphVector glyphVector = f.createGlyphVector(fontRendContext, c);
|
8 |
|
9 | Rectangle2D bounds = glyphVector.getPixelBounds(fontRendContext, 0, 0);
|
10 | int width = (int)bounds.getWidth();
|
11 | int height = (int)bounds.getHeight();
|
12 |
|
13 | BufferedImage bn = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
|
14 | Graphics2D g2d = bn.createGraphics();
|
15 | g2d.drawGlyphVector(glyphVector, 0,(float)height);
|
16 |
|
17 | WritableRaster raster = bn.getRaster();
|
18 | DataBuffer buffer = raster.getDataBuffer();
|
19 | SampleModel model = bn.getSampleModel();
|
20 |
|
21 | for(int i=0; i<raster.getHeight(); i++)
|
22 | {
|
23 | for(int j=0; j<raster.getWidth(); j++)
|
24 | {
|
25 | int pixel[] = model.getPixel(j, i, (int[])null, buffer);
|
26 |
|
27 | if(pixel[0]>0)
|
28 | {
|
29 | System.out.print("#");
|
30 | }
|
31 | else
|
32 | {
|
33 | System.out.print(" ");
|
34 | }
|
35 |
|
36 | }
|
37 | System.out.println();
|
38 | }
|
39 | }
|
Andere Zeichen werden richtig erzeugt.
Kann mir jemand Tipps dazu geben?
lg Robert