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);
The Java Printing API enables applications to: Print all AWT and Java 2D graphics, including composited graphics and images.
%d: Specifies Decimal integer. %c: Specifies character. %T or %t: Specifies Time and date. %n: Inserts newline character.
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.
Use INT_RGB instead of INT_ARGB and you'll be fine:
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With