?
Solved

How to add a line and text to a buffered image?

Posted on 2006-04-28
11
Medium Priority
?
399 Views
Last Modified: 2012-05-05
I have a buffered image that I am going to print.  

Given the buffered image object, what is the easiest way to:

add a line of text at the top
add a line going accross the width of the page
center the original buffered image underneath that?
0
Comment
Question by:jjacksn
9 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 16565675
Create a new image. Draw the texts as you wish. Finally draw the old image as desired. Print the new image!
0
 
LVL 5

Author Comment

by:jjacksn
ID: 16565719
Any sample code on drawing fonts?  (I don't know how).
0
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 500 total points
ID: 16565766
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:jjacksn
ID: 16565814
yeah, actually, turns out I do have acces to a graphics object where I need it, making this much easier.  happen to know how to center text?
0
 
LVL 15

Assisted Solution

by:Javatm
Javatm earned 500 total points
ID: 16565972
> happen to know how to center text?

Use : drawString(String s, float x, float y) - Float X and Y is the possition use to place where on the screen you would like it to be. :)
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 16565987
>> happen to know how to center text?
You would have to calculate the length of the text, using the font information, and the use the length to position the text appropriately.
0
 
LVL 5

Assisted Solution

by:maclema
maclema earned 500 total points
ID: 16567259
Here is a working sample application...

Let me know if you have any questions..

Matt

______________________________________________

import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.image.*;

public class TextOnImage
{
      
      public BufferedImage loadImage(String path) throws IOException
      {
            Image img = ImageIO.read(new File(path));
            return (BufferedImage)img;
      }
      
      public void saveImage(String path, BufferedImage img) throws IOException
      {
            ImageIO.write(img, "PNG", new File(path));
      }
      
      public int getStringWidth(String str, Font f)
      {
            JTextField txt = new JTextField();
            txt.setFont(f);
            FontMetrics fm = txt.getFontMetrics(txt.getFont());
            return fm.stringWidth(str);
      }
      
      public void drawText(String text, BufferedImage img)
      {
            //Create a graphics object
            Graphics2D g2d = (Graphics2D)img.createGraphics();
            
            //create a font
            Font f = new Font("Arial", Font.BOLD, 14);
            
            //determine the total width of the string to be written
            double stringWidth = getStringWidth(text, f);
            
            //turn on anitaliasing (smooth edges)
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            
            //determine the x coordinate to center the string
            double imageWidth = img.getWidth();
            int x = (int)Math.floor((imageWidth-stringWidth)/(double)2);
            int y = 20;
            
            //create a color
            Color c = Color.BLACK;
            
            g2d.setColor(c);
            g2d.setFont(f);
            g2d.drawString(text, x, y);
      }
      
      public static void main(String[] args) throws IOException
      {
            TextOnImage toi = new TextOnImage();
            
            BufferedImage img = toi.loadImage("C:/sample.jpg");
            toi.drawText("Hello World!", img);
            toi.saveImage("C:/sample.png",img);
      }

}
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 500 total points
ID: 16568876
This might help if you adapted it:

      class OverlayIcon extends ImageIcon {
            private String text;

            private boolean saved;

            /**
             * @param image
             */
            public OverlayIcon(Image image, String text) {
                  super(image);
                  this.text = text;
            }

            /*
             * (non-Javadoc)
             *
             * @see javax.swing.ImageIcon#paintIcon(java.awt.Component,
             *      java.awt.Graphics, int, int)
             */
            public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
                  super.paintIcon(c, g, x, y);
                  g.drawString(text, 10, 10);
            }

            public void saveOverlay() {
                  Graphics g = null;
                  try {
                        BufferedImage bi = new BufferedImage(getIconWidth(), getIconHeight(),
                                    BufferedImage.TYPE_INT_RGB);
                        g = bi.getGraphics();
                        paintIcon(component, g, 0, 0);
                        ImageIO.write(bi, "bmp", new File("overlay.bmp"));
                  } catch (IOException e) {
                        e.printStackTrace();
                  } finally {
                        g.dispose();
                  }
            }

      }
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question