Image Processing in Java | Set 10 ( Watermarking an image )
We strongly recommend to refer below post as a prerequisite of this.
- Image Processing in Java | Set 1 (Read and Write)
- Image Processing In Java | Set 2 (Get and set Pixels)
In this set we will be generating a watermark and apply it on an input image.
For generating a text and applying it in an image we will use java.awt.Graphics package. Font and color of text is applied by using java.awt.Color and java.awt.Font classes.
Some of the methods used in the code:
- getGraphics() – This method is found in BufferedImage class, and it returns a 2DGraphics object out of image file.
- drawImage(Image img, int x, int y, ImageObserver observer) – The x,y location specifies the position for the top-left of the image. The observer parameter notifies the application of updates to an image that is loaded asynchronously. The observer parameter is not frequently used directly and is not needed for the BufferedImage class, so it usually is null.
- setFont(Font f) – This method is found in Font class of awt package and the constructor takes (FONT_TYPE, FONT_STYLE, FONT_SIZE) as arguments.
- setColor(Color c) – This method is found in Color class of awt package and the constructor takes (R, G, B, A) as arguments.
- drawString(String str, int x, int y) – Fond in Graphics class takes the string text and the location coordinates as x and y as arguments.
NOTE:Code will not run on online ide since it requires image in drive.
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.