Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing String on a picture in Java

Tags:

java

I want to write a string on an existing picture in java. The pic is of .jpg format. I have used the below code, the only problem is that the final image has a red shade on it..something like the image lost its true color and is light red. Please help me to rectify this problem.

    BufferedImage img = ImageIO.read(new File("pic1.jpg"));
    int width = img.getWidth();
    int height = img.getHeight();
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bufferedImage.createGraphics();
    Font font = new Font("Serif", Font.PLAIN, 96);
    g2d.setFont(font);
    g2d.drawImage(img, 0, 0, null);
    g2d.drawString(text, 100, 250);
    g2d.dispose();

    File file = new File("newimage.jpg");

    ImageIO.write(bufferedImage, "jpg", file);
like image 762
user2626234 Avatar asked Aug 31 '13 08:08

user2626234


People also ask

Can I print image in Java?

The Java Printing API enables applications to: Print all AWT and Java 2D graphics, including composited graphics and images.

How does %D work in Java?

%d: Specifies Decimal integer. %c: Specifies character. %T or %t: Specifies Time and date. %n: Inserts newline character.

Which datatype is used for image in Java?

By default, Java supports only these five formats for images: JPEG, PNG, BMP, WEBMP, GIF. If we attempt to work with an image file in a different format, our application will not be able to read it and will throw a NullPointerException when accessing the BufferedImage variable.


1 Answers

Use INT_RGB instead of INT_ARGB and you'll be fine:

  BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
like image 61
Jk1 Avatar answered Oct 12 '22 09:10

Jk1