2

I've an image located at images/image.png in my java project. I want to write a method its signature is as follow

byte[] mergeImageAndText(String imageFilePath, String text, Point textPosition);

This method will load the image located at imageFilePath and at position textPosition of the image (left upper) I want to write the text, then I want to return a byte[] that represents the new image merged with text.

mohamede1945
  • 7,092
  • 6
  • 47
  • 61
  • If you don't want to go with "plain old" Java, you could try to make use of an existing image processing library. I've used ImageMagick lightly in the past, and it seemed pretty good. Its website links to an open-source Java interface called [JMagick](http://www.jmagick.org/index.html;jsessionid=62FE2FA6029938F4372483DB9F301A9C). – Pops Jun 30 '11 at 20:09
  • To be honest, Java has some good support exactly for this kind of tasks. – Rekin Jun 30 '11 at 20:17

3 Answers3

6

Use ImageIO to read the image into a BufferedImage.

Use the getGraphics() method of BufferedImage to get the Graphics object.

Then you can use the drawString() method of the Graphics object.

You can use ImageIO to save the image.

NoNaMe
  • 6,020
  • 30
  • 82
  • 110
camickr
  • 321,443
  • 19
  • 166
  • 288
  • That's exactly what I wanted. BTW, javadocs says it's better to use createGraphics This method returns a Graphics2D, but is here for backwards compatibility. createGraphics is more convenient, since it is declared to return a Graphics2D. – mohamede1945 Jun 30 '11 at 20:17
6

Try this way:

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;

public class ImagingTest {

    public static void main(String[] args) throws IOException {
        String url = "http://icomix.eu/gr/images/non-batman-t-shirt-gross.jpg";
        String text = "Hello Java Imaging!";
        byte[] b = mergeImageAndText(url, text, new Point(200, 200));
        FileOutputStream fos = new FileOutputStream("so2.png");
        fos.write(b);
        fos.close();
    }

    public static byte[] mergeImageAndText(String imageFilePath,
            String text, Point textPosition) throws IOException {
        BufferedImage im = ImageIO.read(new URL(imageFilePath));
        Graphics2D g2 = im.createGraphics();
        g2.drawString(text, textPosition.x, textPosition.y);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(im, "png", baos);
        return baos.toByteArray();
    }
}
Rekin
  • 9,731
  • 2
  • 24
  • 38
2

I'm just going to point you in the general direction of image manipulation in Java.

To load images you can use ImageIO. You can also use ImageIO to output images to different formats.

The easiest way to create an image is to use BufferedImage and then paint on it via Graphics2D. You can use Graphics2D to paint your loaded image and then paint your text on top of it.

When you're done you use ImageIO to output the image in a suitable format (PNG, JPG, etc).

Andreas Holstenson
  • 1,428
  • 9
  • 7