How can I load an image and write text to it using Java? -


i've image located @ images/image.png in java project. want write method signature follow

byte[] mergeimageandtext(string imagefilepath, string text, point textposition);

this method load image located @ imagefilepath , @ position textposition of image (left upper) want write text, want return byte[] represents new image merged text.

try 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();     } } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -