
03-05-2009, 03:52 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
|
|
how to get raw image data for fonts
As part of a bigger project I am working on, I need to be alble to get the images of fonts brought to an array of pixel values, which would be used as a training set in some advanced work on artificial intelligence. The only thing I can think of is to do graphics.getGrapics() the draw string in each of the fonts on that graphic object but everyting I can find necessitates having a graphics object come in from the system.
I need to list fonts, then have each of those fonts draw ascii 0x20 through 0x7e and be able to get the [][] or [] as one would normally view [][] on the screen or that same data as a flat dataspace that I can do offsets to make a Matrix. I have searched the code but have not gotten any starting places located.
The data can be float or int or 1/0 valued, it does not matter - I just need work the raw values from code rather than tediously, manually generating data by looking at the displayed rendering.
|
|

03-05-2009, 10:41 PM
|
 |
Senior Member
|
|
Join Date: Sep 2008
Location: Madgaon, Goa, India
Posts: 726
Rep Power: 2
|
|
|
I guess you can drawString to a BufferedImage of TYPE_BYTE_BINARY and use the form of getRGB that returns an int[] (which will be in RGB color space [000000] and [FFFFFF], but you can reduce it to 0/1 binary easily enough)
HTH, db
|
|

03-08-2009, 02:07 PM
|
|
Senior Member
|
|
Join Date: Jan 2009
Posts: 359
Rep Power: 2
|
|
|
Oddly, I've had to do this before as well. What I did (for better or worse), was to construct a BufferedImage large enough for every character in the font (the size can be obtained from FontMetrics), then drew the character to the graphics, grabbed the resulting pixels, and made a 2d array containing a 1 everywhere the associated pixel was nonzero.
There's probably a better way to do it by directly calculating pixels from the font, but this approach is simple and works.
|
|

03-08-2009, 04:49 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
|
|
|
Both of these are what I was thinking.
|
|

03-10-2009, 04:10 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
|
|
how to construct in internal buffered image
Originally Posted by Darryl.Burke
|
|
I guess you can drawString to a BufferedImage of TYPE_BYTE_BINARY and use the form of getRGB that returns an int[] (which will be in RGB color space [000000] and [FFFFFF], but you can reduce it to 0/1 binary easily enough)
|
I found no way to construct a BufferedImage except to get one preconstructed from a graphics object supplied in a paint() .... the best I can come up with is:
|
Code:
|
this.notAnImage=new int[12];
notAnImage[0] =0x128;
notAnImage[1] =0x128;
notAnImage[2] =0x128;
notAnImage[3] =0x128;
notAnImage[4] =0x128;
notAnImage[5] =0x128;
notAnImage[6] =0x128;
notAnImage[7] =0x128;
notAnImage[8] =0x128;
notAnImage[9] =0x128;
notAnImage[10]=0x128;
notAnImage[11]=0x128;//zarfOCR.dll
// = new java.awt.image.BufferedImage(java.awt.image.ColorModel.getRGBdefault(),java.awt.image.Raster.createPackedRaster(java.awt.image.DataBuffer.TYPE_BYTE,4,3,notAnImage, new java.awt.Point()),false, new java.util.Hashtable<java.lang.String,java.lang.String>()); |
obviously the [] could be made bigger and I would not unroll the loop for a default internal Buffered Image initialization ... the data will be coming in ( from the JNI work in the static linking post ) as 8-bit grayscale default dib with no compression or any standard cs ops deriving from known optimizations, that's the license spike provides - anything requiring any brains such as will be coming in from known commercial traffic gets passed to him for commercial use.
I worked on this as deep as I could grasp what is going on, the comment marker is to remove the code during prototyping - the small array should actually be grapics big enough to do object.drawString() for ascii 0x20 through 0x7e inclusive, at any font size that I can get constructed.
The instructor ( a double degreed board member of IEEE ) suggests using the smallest size that will work, for me what is practical for that is "any size that will work" as I can let the machine run for a hundred hours building the learning sets. One of the things I will get to today is g5 - which has an abundance of powerful optimizations for building the learning sets.
What is hanging the plan here is constructing a default ImageProducer of sufficient correctness that I can call draw string on it, listing the fonts and reducing the selection set to a sensible set is within my skills, though will involve significant manual methods such looking at the system font tool and so on....
|
|

03-10-2009, 04:15 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
|
|
setting the font
Originally Posted by toadaly
|
|
Oddly, I've had to do this before as well. What I did (for better or worse), was to construct a BufferedImage large enough for every character in the font (the size can be obtained from FontMetrics), then drew the character to the graphics, grabbed the resulting pixels, and made a 2d array containing a 1 everywhere the associated pixel was nonzero.
|
Call setFont(...) on the graphics object then call getMetrics() ?... seems to involve an extra step,... tried to do this once before and drilled in on exactly this issue.
Originally Posted by toadaly
|
|
There's probably a better way to do it by directly calculating pixels from the font, but this approach is simple and works.
|
The tradional, known matter of getting the [][] from the font, has to be done with code - then efforts fail on how to get the information as the original coder was coding for routine use of the information appliance, not for developers.
|
|

03-11-2009, 05:03 AM
|
|
Senior Member
|
|
Join Date: Jan 2009
Posts: 359
Rep Power: 2
|
|
Originally Posted by Nicholas Jordan
|
|
I found no way to construct a BufferedImage except to get one preconstructed from a graphics object
|
|
Code:
|
...supposing you found the width and height from FontMetrics...
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g = (Graphics2D) image.getGraphics();
g.setColor(Color.white);
g.fillRect(0,0,width,height);
g.setColor(Color.black);
g.drawString("A",0,0);
g.dispose();
WritableRaster wr = image.getRaster();
int[] pixels = wr.getPixels(0,0,width,height,null); |
|
|

04-05-2009, 05:45 AM
|
|
Member
|
|
Join Date: Apr 2009
Posts: 4
Rep Power: 0
|
|
Originally Posted by toadaly
|
|
Code:
|
...supposing you found the width and height from FontMetrics...
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g = (Graphics2D) image.getGraphics();
g.setColor(Color.white);
g.fillRect(0,0,width,height);
g.setColor(Color.black);
g.drawString("A",0,0);
g.dispose();
WritableRaster wr = image.getRaster();
int[] pixels = wr.getPixels(0,0,width,height,null); |
|
Hi, I try to get the raw data of the image by your method, but the complier said that "The method getPixels(int, int, int, int, int[]) is ambiguous for the type WritableRaster", any suggestion?
|
|

04-05-2009, 07:15 AM
|
|
Senior Member
|
|
Join Date: Jan 2009
Posts: 359
Rep Power: 2
|
|
|
Cast the 'null' to an int[].
...wr.getPixels(0,0,width,height,(int[]) null);
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 06:03 AM.
|
|