Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Extracting Pixel Data From Fonts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Extracting Pixel Data From Fonts" Watch "Extracting Pixel Data From Fonts" New topic
Author

Extracting Pixel Data From Fonts

Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Hi,

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 pseudo-code)
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?

This is an application that is similar to what I am trying to do (scroll down to "Bitmaps and Fonts"):
http://www.reifel.org/PICUserInterface/

Thanks for any input.

Regards

MCU
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41519
    
  53
You could 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.


Ping & DNS - my free Android networking tools app
Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Hi

Could you please give an example of creating a letter (e.g. 'I'), then assigning that to a BufferedImage? I am reading about Fonts and Images, and this will help me in research.

Thanks

MCU
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41519
    
  53
Here's an example that does that: http://radio.javaranch.com/davo/2007/07/20/1184942077153.html I'm sure you can figure out how to remove the 90 degree rotation :-)
Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Hi,

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.


Thanks!

MCU
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41519
    
  53
That code is pretty basic. What do you not understand about it?
Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Hi

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:


It's only printing 0's though...

Thanks MCU
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.

Try out the following code:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41519
    
  53
As an aside, there's no font called "sanserif". See http://java.sun.com/javase/technologies/core/basic/intl/faq.jsp#font-selection for more information.
Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Hi

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?

2)

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?

Thanks again

Regards

MCU
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41519
    
  53
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extracting Pixel Data From Fonts