Results 1 to 9 of 9
  1. #1
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Question 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.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default

    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

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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.

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Both of these are what I was thinking.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  5. #5
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Question how to construct in internal buffered image

    Quote Originally Posted by Darryl.Burke View Post
    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:
    Java 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....
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default setting the font

    Quote Originally Posted by toadaly View Post
    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.

    Quote Originally Posted by toadaly View Post
    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.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  7. #7
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    I found no way to construct a BufferedImage except to get one preconstructed from a graphics object
    Java 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);

  8. #8
    hyz_zsu is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by toadaly View Post
    Java 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?

  9. #9
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Cast the 'null' to an int[].

    ...wr.getPixels(0,0,width,height,(int[]) null);

Similar Threads

  1. Replies: 0
    Last Post: 03-05-2009, 06:18 AM
  2. Replies: 0
    Last Post: 06-22-2008, 11:06 PM
  3. Replies: 0
    Last Post: 01-08-2008, 05:29 PM
  4. Replies: 0
    Last Post: 11-06-2007, 06:58 AM
  5. Replies: 3
    Last Post: 07-16-2007, 11:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •