Conceptually, how would I convert an arbitrary character into an array of pixel data, so that from that pixel data I would know what pixels need to be set in order to 'visually' get that character (e.g. drawing dots it in MS Paint)?
For example, this is an array of pixel data that could be generated for the Times New Roman character 'I':
In the array of pixel data, each row would be represented by a byte which would hold that bit pattern.
(Basically I want to be able to generate a pixel bitmap for all characters of an arbitrary font).
How doable is this? In general terms, how would it be done?
I've been trying to do this but haven't got far. I've looked at the above link, but I'm finding hard to decode. I need something simpler. If anyone could provide code (complete or psuedo) that does the below I would appreciate it (say for a test character, 'T').
[...]draw the characters (in a font, size and style of your preference) into a BufferedImage, and then examine the pixel values of the raster of that image.
Joined: Mar 22, 2005
That code is pretty basic. What do you not understand about it?
Joined: Apr 09, 2009
I don't understna how to adapt it to my application. I have written the code below that draws a character 'H' in the very top left corner, then tries to capture that corner into a BufferedImage, then reads the pixel values row by row. When it reads the pixel values, it should output '0' for an rgb value of 0, and '1' for everything else:
That's because you create a new BufferedImage first, then throw that away, then create a new empty BufferedImage through g2d.getDeviceConfiguration(). You are indeed painting the H but not on the image.
Thanks both for your input! I now have a test program. I have some questions regarding imageType:
Based on the above program by Rob Prime:
1) If I fill or clear the area using g2d before writing a character to the BufferedImage, then the output is wrong (all 1's). But when I immediately write a character, without doing anything to the background (as in the example above), then the output is correct (1's and 0's). Why is this?
Where can I find the mappings for colours of ARGB for Java?
3)The char's I draw to the BufferedImage are monochromatic, and the output is also monochromatic. Also, I am not displaying the BufferedImage or Graphics2D on a screen. I tried switching the BufferedImage imageType from TYPE_INT_ARGB to TYPE_BYTE_BINARY to try and use the 1-bit mode, but the output became incorrect. Where can I find out about using imageTypes, TYPE_BYTE_BINARY in particular?
Joined: Mar 22, 2005
The java.awt.image.BufferedImage javadocs have some information about the various image types, and how they store data. For the (A)RGB types, the int value generally has the A (alpha) value in bits 24-31, R (red) in bits 16-23, G (green) in bits 8-15 and B (blue) in bits 0-7. So you need to do some shifting and masking on the bit level to get at the individual color values.
The source code of the classes (which comes with the JDK in a file called "src.zip") may also help.