javaimageimgscalr

how to add text to image using imgscalr


I want to add some text to image. and I'm using imgscalr.

I'm looking for some examples or tutorials.

in case you know some better java based image library, please do let me know.

thanks!


Solution

  • Hmm, interesting suggestion. I never anticipated that people would want to render text to their images using imgscalr -- but with the popularity of meme images, maybe I should have thought of that :)

    Since imgscalr is processing and returning standard BufferedImages in Java, you are free to use all the existing Java2D rendering libs to paint text to the image.

    To do that you want to call BufferedImage.getGraphics() on the image you are processing, cast that to a Graphics2D object like so:

    Graphics2D g2d = (Graphics2D)myImage.getGraphics();
    

    Then you can use the standard Graphics2D font rendering methods to "draw" text onto your image. For example, something like:

    g2d.drawString("I haz a hat!", 50, 50);
    

    Also you can change how the font looks by using the Graphics.setFont(...) call.

    In the future I may add convenience methods to imgscalr to do this for you now that you have given me this idea ;)