Results 1 to 7 of 7
  1. #1
    elcapi is offline Member
    Join Date
    Jul 2007
    Location
    Mexico City
    Posts
    2
    Rep Power
    0

    Default Writing text into an image and save it

    Hi,

    I need to insert/write text into an existing image and I would like to know if someone has some experience with this. The goal is to open an image then write some text on it and then save it. The saved image should be displayed with the text wrote before.

    At this moment I can open the image:

    Java Code:
     
    
    FileSeekableStream stream = new FileSeekableStream(imageFilename);
    TIFFDecodeParam decodeParam = new TIFFDecodeParam();
    decodeParam.setDecodePaletteAsShorts(true);
    
    ParameterBlock params = new ParameterBlock();
    params.add(stream);
    RenderedOp image1 = JAI.create("tiff", params);
    BufferedImage img = image1.getAsBufferedImage();
    With the BufferedImage object, I can display the image with a graphics object and display some text too.

    Java Code:
    public void paint(Graphics g) {
       Dimension size = getSize();
       g.drawImage(img, 
    	0, 0, size.width, size.height,
    	0, 0, img.getWidth(null), img.getHeight(null),
    	null);
    
       Font font = new Font("Serif", Font.PLAIN, 18);
       g.setFont(font);
       g.setColor(Color.black);
       g.setFont(font);
       g.drawString("Some Text...", 10,10);
    }
    The idea is to do something similar without displaying the image and without using a graphics object or, someone know if is possible to save the objects displayed from the graphics object into an image?

    I have been looking for some class in JAI that let me do that, but I haven't found the way to do it.

    Any ideas would be appreciated

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    Check createGraphics() method of BufferedImage class:

    Creates a Graphics2D, which can be used to draw into this BufferedImage.
    BufferedImage (Java 2 Platform SE v1.4.2)

  3. #3
    elcapi is offline Member
    Join Date
    Jul 2007
    Location
    Mexico City
    Posts
    2
    Rep Power
    0

    Default

    Hi,

    thanks a lot for the tip.

    Now I can insert text with the Graphics2D object and save it with the ImageIO class

    Java Code:
    Graphics2D g2 = img.createGraphics();
    		
    g2.setColor(Color.black);
    g2.setFont(font);
    g2.drawString(text, 80,1440);
    
    File outputfile = new File(f"imagewithtext.png");
    ImageIO.write(img, "png", outputfile);

  4. #4
    scorpio is offline Member
    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    Post java code to write text into bmp file

    can any one tell me how to write text into bmp file

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    bmp is largely worthless. Why not simply use png, or jpg? Both are supported by java, and as a bonus, its not a BMP :D

  6. #6
    scorpio is offline Member
    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    Thumbs down hi

    thanks for your kind information but what i was talking about was bmp[bitmap] image...hopefully now you can get it....

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    I understood you BMP file format - Wikipedia, the free encyclopedia. I was stating that bmp being a windows-centric format which also has poor compression makes it not a very good format to use for anything. All images which are not vector are bitmap by their very nature, but bmp is a file format. Png, Jpg, Gif, etc... are all bitmap. But .bmp refers the windows image format from yesteryear.

    If you were referring to bitmap images in general (anything with pixels) then your question is mostly answered by the above posters.

    To write text on an image an save it, first load it into java with ImageIO and store it as a BufferedImage, get the Graphics object of the image with
    Java Code:
    myBufferedImage.getGraphics()
    set a color:
    Java Code:
    Graphics g = myBufferedImage.getGraphics();
    g.setColor(Color.RED);
    and then draw text on the image
    Java Code:
    g.drawString("Some Text", 10, 50);
    where 10 and 50 are the x and y pixel coord's for the string on the image.
    Then save the image in a useful format like png or jpg with ImageIO.

    Hope this helps

Similar Threads

  1. Replies: 7
    Last Post: 08-17-2010, 07:00 PM
  2. how to save image as jpg?
    By katie in forum New To Java
    Replies: 4
    Last Post: 04-15-2010, 11:07 AM
  3. how to save image to disk after using pixelgrabber
    By shishirg in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 03:45 PM

Posting Permissions

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