Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extracting Pixel Data From Fonts

 
Mikpo Siru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mikpo Siru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code is pretty basic. What do you not understand about it?
 
Mikpo Siru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic