Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java to add text watermarking effect to a picture

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The knowledge of this article "how to use Java to add text watermarking effect on pictures" is not understood by most people, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use Java to add text watermarking effect on pictures" article.

[1] get the original picture object

First of all, the first step is to make sure that our program gets the pictures that need to be processed.

There are usually two ways for our program to obtain pictures, one is to download to the local and read from the local, and the other is to obtain the pictures through the network address.

(1.1) read local pictures

For example, if we download the picture to our local computer (E:\ imgs directory), then we can get the image object directly in the main function through the following code.

/ / read picture file File file = new File ("E:/imgs/test.png"); System.out.println ("picture name:" + file.getName ()); System.out.println ("picture size:" + file.length () / 1024 + "kb"); / / convert file object to picture object BufferedImage image = ImageIO.read (file); System.out.println ("picture width:" + image.getWidth () + "px") System.out.println ("Picture height:" + image.getHeight () + "px")

The code effect is as follows:

(1.2) read network pictures

If the picture is a network picture, we can directly get its address to download, the relevant code is as follows (the name and type of the network picture, depending on the downloader).

URL url = new URL ("https://zyq2022.oss-cn-chengdu.aliyuncs.com/test.png");BufferedImage image = ImageIO.read (url.openStream ()); System.out.println (" Picture width: + image.getWidth () + "px"); System.out.println ("Picture height:" + image.getHeight () + "px")

The code effect is as follows:

[2] create a brush

After the above description, we have obtained the original picture, and then we need to create a brush, and then the style of the text we write is completely determined by it. It can set color, font size, font style and so on.

/ / create a brush (image is the image object in the previous step) Graphics2D pen = image.createGraphics (); / / set the brush color to white pen.setColor (Color.WHITE); / / set the brush font style to Microsoft yahei, italics, and the text size to 20pxpen.setFont (new Font( "Microsoft yahei", Font.ITALIC, 20))

Related instructions:

(1) pen.setColor (Color.WHITE)

This line of code means to set the brush color to white.

Other colors include: WHITE (white), LIGHT_GRAY (light gray), GRAY (gray), DARK_GRAY (dark gray), BLACK (black), RED (red), PINK (pink), ORANGE (orange), YELLOW (yellow), GREEN (green), MAGENTA (fuchsia), CYAN (cyan), BLUE (blue)

If none of the above colors satisfy you, or if you also want to set the font transparency, you can change it to the following format:

Pen.setColor (new Color (179,250,233,100))

The four parameters here are RGBA (click here that does not understand RGBA), and the range of the four parameters is 0-255, respectively.

(2) pen.setFont (new Font ("Microsoft Acer", Font.ITALIC, 20))

Font.PLAIN (normal), Font.BOLD (bold), Font.ITALIC (italic)

[3] add text watermark

The following line of code is the core code to add a text watermark.

/ / these three parameters are your text content, starting position Abscissa (px), ordinate position (px). Pen.drawString ("I am a picture watermark", 30,550)

(3.1) how to determine the location of the watermark?

First of all, we have to understand that we are talking about the coordinates on the picture, which is a representation, as follows:

So, how do we determine the actual coordinates of our watermark image?

We can first save the picture locally, then right-click and select the drawing tool class to open it.

After opening it, we move the mouse cursor to the place where we need to add text (such as the red arrow), and the coordinates of the corresponding location (the blue arrow) will be displayed at the bottom, so we can get these coordinates and write them into the program code.

[4] get processing pictures

After the above three operations, we have finished adding text to our picture, but it is still saved in the Java object, and we need to save it locally in order to see the effect (for example: here I will save the image to test2.png).

/ / create a new image file File file = new File ("E:/imgs/test2.png"); / / write the processed image data to the new image file FileOutputStream fos = new FileOutputStream (file); ImageIO.write (image, "png", fos)

Finally, after executing the code, we see that there is an extra picture of test2.png under the original picture folder.

After we open the test2.png image, we can see that the text watermark has been added successfully.

[5] Source code

Finally, the whole source code interpreted above is put below for your reference:

Package com.zyq.util; import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.net.URL; import javax.imageio.ImageIO; public class ImgTest {public static void main (String [] args) throws IOException {/ / read the local picture file / / File file = new File ("E:/imgs/test.png") / / System.out.println ("picture name:" + file.getName ()); / / System.out.println ("picture size:" + file.length () / 1024 + "kb"); / / convert file objects to picture objects / / BufferedImage image = ImageIO.read (file) / / get network picture URL url = new URL ("https://zyq2022.oss-cn-chengdu.aliyuncs.com/test.png"); BufferedImage image = ImageIO.read (url.openStream ()); / / create brush Graphics2D pen = image.createGraphics (); / / set brush color to white / / pen.setColor (Color.WHITE)) Pen.setColor (new Color (179,250,233,200)); / / set the brush font style to Microsoft Yahei, italic, and the text size to 20px pen.setFont (new Font ("Microsoft Yahei", Font.ITALIC, 20); / / write watermark text and coordinate pen.drawString ("I am a picture watermark", 30550) / / create a new image file File file = new File ("E:/imgs/test2.png"); / / write the processed image data to the new image file FileOutputStream fos = new FileOutputStream (file); ImageIO.write (image, "png", fos) }} the above is about the content of this article on "how to use Java to add text watermarking effect on pictures". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to learn more about related knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report