Reputation: 91
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);
Upvotes: 5
Views: 709
Reputation: 9
Write code like this. There is no need to use BufferedImage class to get image graphic object. This just will do what you want.
BufferedImage img = ImageIO.read(new File("pic1.jpg"));
int width = img.getWidth();
int height = img.getHeight();
Graphics g = img.getGraphics();
Font font = new Font("Serif", Font.PLAIN, 96);
g.setFont(font);
g.setColor(Color.WHITE);
g.drawString(text, 100, 250);
g.dispose();
File file = new File("newimage.jpg");
ImageIO.write(bufferedImage, "jpg", file);
Upvotes: 1
Reputation: 11443
Use INT_RGB instead of INT_ARGB and you'll be fine:
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Upvotes: 6