0

There was a PHP application that's purpose was to print text over a number of pre-defined PNG images. It worked without issues and printed antialiased text in any given font and color. What are the special considerations when printing over the existing images in Java?

I tired to use Graphics2D calls to print over the same images, but it is not working with the code below.

String imName = Config.getBlanketImage();
File file = new File(imName);
// BufferedImage im = new BufferedImage(400, 600, BufferedImage.TYPE_INT_ARGB); 
// graphics.setColor(new Color(bColor.getRed(), bColor.getGreen(), bColor.getBlue()));
// graphics.fillRect(0, 0, im.getWidth(), im.getHeight());
BufferedImage im = ImageIO.read(file);

graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
graphics.setFont(drawFont);
graphics.setColor(new Color(aColor.getRed(), aColor.getGreen(), a.getBlue()));
graphics.drawString(Config.getText1(), x, y);

The text comes across in a completely wrong color - the shades of background, sometimes surrounded by thin line of black pixels. I cannot post images and only 2 links, so screw the samples. But if I commented out the first three lines and uncommented the commented out lines and printed over a brand new image, the colors would be correct and anti-aliasing would be on.

I tried with and without the rendering hints and it did not make any difference whatsoever, just showing all that I tried.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
ajeh
  • 2,652
  • 2
  • 34
  • 65
  • 1
    Is your test image truecolor RGB(A) or does it use a palette? – Jongware Sep 10 '13 at 14:23
  • Good catch. Saving images as 24bit fixed everything. Sounds like PHP does not care about 8 bit color depth, but AWT does. – ajeh Sep 10 '13 at 17:21
  • Other than the fact that the files are now 10x times larger, it works. Is there any way to print over 8bit color depth just like PHP did? Moving to the truecolor images will bloat our traffic and memory requirements. – ajeh Sep 10 '13 at 17:33
  • You'll have to disable antialiasing and transparency, at least. Or convert to 24 bits, add text, convert back to 8-bit. – Jongware Sep 10 '13 at 18:08
  • *"pre-defined PNG images."* Link to one (that is preferably small in bytes). – Andrew Thompson Sep 10 '13 at 22:36
  • @Jongware You'll get the best results by going to true color and then add a new color quantization/dithering step, but it's also possible to keep the `IndexColorModel` from the original image if the color you use to paint text is already in the palette. – Harald K Sep 11 '13 at 08:54
  • Harald, did you mean it is possible to keep 8bit images and temporarily convert them to 24bit to perform drawing, then convert back to 8? Could you provide a sample? – ajeh Sep 11 '13 at 14:01

0 Answers0