-1

I am trying to calculate image center(to add water marks)following - add-water-to-image and the results shows negative x, for some of the images, here is the math part:

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

and the entire function:

public void addTextWatermark(String text, File sourceImageFile, File destImageFile, int textSize) {
        try {
            BufferedImage sourceImage = ImageIO.read(sourceImageFile);
            Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();

            // initializes necessary graphic properties
            AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f);
            g2d.setComposite(alphaChannel);
            g2d.setColor(Color.BLACK.darker());
            //Font(fontName, fontStyle, foneSize)
            g2d.setFont(new java.awt.Font("Arial", Font.BOLD, textSize));
            FontMetrics fontMetrics = g2d.getFontMetrics();
            //text - input text , g2d - Graphics2D
            Rectangle2D rect = fontMetrics.getStringBounds(text, g2d);

            // calculates the coordinate where the String is painted
            int centerX = (sourceImage.getWidth() - (int) rect.getWidth()) / 2;
            int centerY = sourceImage.getHeight() / 2;

            // paints the textual watermark
            g2d.drawString(text, centerX, centerY);

            ImageIO.write(sourceImage, "png", destImageFile);
            g2d.dispose();


        } catch (IOException ex) {
            System.out.println(ex.getMessage().toString());
        }
    }

1-Is there a way to ensure the math will work for all images?

2-Is there a diffrence between jpg and png in this calclation?

Thanks.

edit


The image sizes that was causeing it were:

1-to big(3000*3000).

2-to small(60*60).

With textSize(g2d.setFont(new java.awt.Font("Arial", Font.BOLD, textSize));) - 32 or less.

Itsik Mauyhas
  • 3,824
  • 14
  • 69
  • 114
  • Math actually works, if the value is negative it means that your watermark is bigger than the actual image. In this scenario, you can simply resize the watermark to be smalle than the image itself. – BackSlash Jun 25 '17 at 10:41
  • Well yes, that will happen if `rect.getWidth()` is bigger than `sourceImage.getWidth()`. Have you looked at the respective widths? Fundamentally, this is saying "You can't fit all of the text at that size onto that image." – Jon Skeet Jun 25 '17 at 10:41
  • @BackSlash the watermark is just a few chars, and the image is 1200x600px, is it possible that the watermark are bigger? – Itsik Mauyhas Jun 25 '17 at 10:48
  • @JonSkeet - how can I watch them in compilation time, I can't change the watermark size, it is just a few chars. – Itsik Mauyhas Jun 25 '17 at 10:51
  • You can use a debugger, or you can add System.out.println() statements in the code to print the values. – JB Nizet Jun 25 '17 at 11:05
  • @JBNizet - that was not what I meant LOL, is there a way to resize this props so it will match the water or visa versa? – Itsik Mauyhas Jun 25 '17 at 11:11
  • I have no idea what a "props" is. You're claiming that the image is way larger than the watermark, but also complain about X being negative, which proves otherwise. Jon suggested you to look at the values of the respective widths, and you asked how you could "watch them in compilation time", which doesn't make sense. I answered by a suggestion to use a debugger or println statements, to know what the actual widths are. – JB Nizet Jun 25 '17 at 11:17
  • 1
    @ItsikMauyhas Post exactly in your question what your input is, what you expected the output to be, and what it really was. Don't add important details such as the image size in the comments (and you still haven't mentioned what the watermark size is in pixels, which is the only relevant information here. Yes - a few chars in a font size 500pt will be bigger than 1200x600px) – Erwin Bolwidt Jun 25 '17 at 11:44
  • @ItsikMauyhas: You can't check this at compilation time, because the sizes aren't known at compilation time. – Jon Skeet Jun 25 '17 at 11:47
  • @ErwinBolwidt - adding some details to the question but I solved it with jon help and - https://stackoverflow.com/questions/244164/how-can-i-resize-an-image-using-java, you are right the image size was huge compare to watermark size. – Itsik Mauyhas Jun 25 '17 at 11:56

1 Answers1

0

So I found 2 ways to handle this issue:

1- if x or y are smaller then 0 I have resize the image using resize-image

2- if the watermark is to small just increase text size - g2d.setFont(new java.awt.Font("Arial", Font.BOLD, textSize));

Thanks for all the help @jon Skeet.

Community
  • 1
  • 1
Itsik Mauyhas
  • 3,824
  • 14
  • 69
  • 114