1

I am Trying to put watermark on images using java. Currently I put watermark on images with extension with png, jpeg but the code is not working on tiff image. Previously it was giving null pointer error but after including twelvemonkey core and tiff plugin jars null pointer error was gone and code is executing line by line. But the output tiff image dosn't have the watermark.

            BufferedImage sourceImage = ImageIO.read(...);
            Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();

            AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f);
            g2d.setComposite(alphaChannel);
            g2d.setColor(Color.BLACK);
            g2d.setFont(new java.awt.Font("Times New Roman", Font.BOLD, 64));
            FontMetrics fontMetrics = g2d.getFontMetrics();
            Rectangle2D rect = fontMetrics.getStringBounds(watermarkText, g2d);

            int centerX = (sourceImage.getWidth() - (int) rect.getWidth()) / 2;
            int centerY = sourceImage.getHeight() / 2;

            FontMetrics fm = g2d.getFontMetrics();

            g2d.drawString(watermarkText, centerX, centerY);

            ImageIO.write(sourceImage,extension, outFile);

Can anyone gives a way to put watermark on tiff image?

Sameesh
  • 315
  • 8
  • 18
  • I have no idea how to do this in java but if you want to add a watermark you need to access the image bitmap and update that with your watermark and then save it back as a tiff image. – Joakim Danielson Jun 20 '19 at 12:46
  • The above code works for me. I added a `g2d.dispose()` for good measure, but it shouldn't matter. Between `ImageIO.read(..)` and `ImageIO.write(..)` there is no difference between a TIFF, PNG or other file types. It's all just `BufferedImage` instances. Perhaps it doesn't work with your particular image or your watermark text. If so, you should include them, to make your problem reproducible. – Harald K Jun 21 '19 at 08:18
  • 1
    Provide some details about the image you're trying to process. Specifically the original color depth. You may have to convert the image data to an appropriate representation before adding your watermark. – JimmyB Jun 21 '19 at 10:14

1 Answers1

3

As already stated in the comments to the question, the problem is likely not that your code is "wrong" or that there's anything specific to the TIFF format that prevents watermarking.

However, TIFF is a very flexible format, and may contain anything from RGB, CMYK, "deep color" or float samples, to simple indexed or bilevel image data. I would suspect that the problem is related to the latter (and if so, you would have the same problem with some palette PNGs), as in these cases there may not be colors that can represent your transparent black. But it's impossible to say for sure, without further clarification.

It's easy to fix such problems by creating a full RGB in-memory image, drawing your original onto that and then adding the watermark. But this would drastically change the output file and size, and I don't know if this is acceptable for your use case.


Anyway, here's a full working example, based on your code above and with verifiable input/output files:

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class WatermarkTest {

    private static String watermarkText = "WaterMark!";

    public static void main(String[] args) throws IOException {
        File input = new File(args[0]);
        String extension = getExtension(input);

        File output = args.length >= 2 
                ? new File(args[1]) 
                : new File(input.getParent(), input.getName().replace("." + extension, "_watermarked." + extension));

        BufferedImage sourceImage = ImageIO.read(input);
        Graphics2D g2d = sourceImage.createGraphics();

        AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f);
        g2d.setComposite(alphaChannel);
        g2d.setColor(Color.BLACK);
        g2d.setFont(new java.awt.Font("Times New Roman", Font.BOLD, 64));
        FontMetrics fontMetrics = g2d.getFontMetrics();
        Rectangle2D rect = fontMetrics.getStringBounds(watermarkText, g2d);

        int centerX = (sourceImage.getWidth() - (int) rect.getWidth()) / 2;
        int centerY = sourceImage.getHeight() / 2;

        g2d.drawString(watermarkText, centerX, centerY);

        g2d.dispose();

        ImageIO.write(sourceImage, extension, output);
    }

    private static String getExtension(final String fileName) {
        int index = fileName.lastIndexOf('.');

        if (index >= 0) {
            return fileName.substring(index + 1);
        }

        // No period found
        return null;
    }
}

Input image: logo.tif

logo.tif

Result image: logo_watermarked.tif

logo_watermarked.tif

Unfortunately, upload to Stackoverflow converted the images to PNG. Original input and output files in TIFF format can be downloaded from Dropbopx.

Harald K
  • 26,314
  • 7
  • 65
  • 111
  • You provided the code, but what specifically was missing/wrong with the OP's code? I.e., what would one have to keep in mind when facing a similar problem? – JimmyB Jun 21 '19 at 09:26
  • @JimmyB Apart from the missing `Graphics.dispose()`, nothing really. I'm actually voting to close this question, as it's not possible to reproduce OP's result. Most likely, there's something special about his image (i.e. palette with no color to represent the translucent water mark, black/dark background or so) or his watermark text becoming empty in certain case or so. But we don't have that information. I might update the answer if OP provides more detail. – Harald K Jun 21 '19 at 09:48
  • Thanks for clarifying. I had to downvote your answer now, because, as you said, the OP's problem will not be fixed by your code (since it's the same as the OP's code). You probably should change the answer for now to something stating that the OP's code is ok and that the problem probably is with the image he's processing, which we cannot reproduce or fix without further information. – JimmyB Jun 21 '19 at 10:11
  • 1
    @JimmyB Ok. Have updated the answer now. I also tried to express this in the comment section of the question. – Harald K Jun 21 '19 at 11:28