How to write about an image

Asked

Viewed 566 times

0

I found this code on the Internet and made some changes, but I’m not sure what’s going wrong.

Code:

public class main 
{
    public static void main(String[] args) throws IOException
    {
        String key = "Lucas Caresia";
        BufferedImage bufferedImage = ImageIO.read(new File("recibo.png"));
        Graphics graphics = bufferedImage.getGraphics();
        graphics.setColor(Color.BLACK);
        graphics.setFont(new Font("Arial Black", Font.PLAIN, 15));
        graphics.drawString(key, 420, 560);
        ImageIO.write(bufferedImage, "png", new File("recibo.jpg"));
        System.out.println("Image Created");
    }
}

Image before:

inserir a descrição da imagem aqui

Image after (visualized by eclipse):

inserir a descrição da imagem aqui

  • 2

    The second image is not showing

  • Exactly this the problem, I tried with several different image viewers, however it continues not appearing.

  • Are you sure the image recibo.png exists and it’s about the image you want?

  • Although you are set to PNG format and saving with extension ". jpg", your code does not seem to have anything wrong. I tested it here and it works "correctly" (it’s just writing the text in the wrong coordinate, but the image generates normally). I will vote as not reproduced.

  • Even if I define as PGN it saves as JPG, however I do not define it in the code, I do not know how to solve

1 answer

1

I was able to solve just by correcting

ImageIO.write(bufferedImage, "png", new File("recibo.jpg"));

To

ImageIO.write(bufferedImage, "png", new File("recibo.png"));
  • The content was PNG, only the extension that was wrong, just to explain, the extensions are just to facilitate, but still the image will be PNG, this is defined by the contents of the file ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.