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 add watermarks to Java

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to add watermarks to Java. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

JAVA/JSP adds text to pictures or pictures to pictures.

Import java.awt.Color

Import java.awt.Font

Import java.awt.Graphics

Import java.awt.Image

Import java.awt.image.BufferedImage

Import java.io.File

Import java.io.FileOutputStream

Import javax.imageio.ImageIO

Import com.sun.image.codec.jpeg.JPEGCodec

Import com.sun.image.codec.jpeg.JPEGImageEncoder

Public final class ImageUtils {

Public ImageUtils () {

}

/ *

* public final static String getPressImgPath () {return ApplicationContext

* .getRealPath ("/ template/data/util/shuiyin.gif");}

, /

/ *

* print the picture onto the picture

*

* @ param pressImg--

* Watermark file

* @ param targetImg--

* Target file

* @ param x

*-x coordinates

* @ param y

*-y coordinates

, /

Public final static void pressImage (String pressImg, String targetImg

Int x, int y) {

Try {

/ / Target file

File _ file = new File (targetImg)

Image src = ImageIO.read (_ file)

Int wideth = src.getWidth (null)

Int height = src.getHeight (null)

BufferedImage image = new BufferedImage (wideth, height

BufferedImage.TYPE_INT_RGB)

Graphics g = image.createGraphics ()

G.drawImage (src, 0,0, wideth, height, null)

/ / Watermark file

File _ filebiao = new File (pressImg)

Image src_biao = ImageIO.read (_ filebiao)

Int wideth_biao = src_biao.getWidth (null)

Int height_biao = src_biao.getHeight (null)

G.drawImage (src_biao, (wideth-wideth_biao) / 2

(height-height_biao) / 2, wideth_biao, height_biao, null)

/ / end of watermark file

G.dispose ()

FileOutputStream out = new FileOutputStream (targetImg)

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder (out)

Encoder.encode (image)

Out.close ()

} catch (Exception e) {

E.printStackTrace ()

}

}

/ *

* print text watermark image

*

* @ param pressText

*-text

* @ param targetImg--

* Target image

* @ param fontName--

* Font name

* @ param fontStyle--

* Font style

* @ param color--

* font color

* @ param fontSize--

* Font size

* @ param x--

* offset

* @ param y

, /

Public static void pressText (String pressText, String targetImg

String fontName, int fontStyle, int color, int fontSize, int x

Int y) {

Try {

File _ file = new File (targetImg)

Image src = ImageIO.read (_ file)

Int wideth = src.getWidth (null)

Int height = src.getHeight (null)

BufferedImage image = new BufferedImage (wideth, height

BufferedImage.TYPE_INT_RGB)

Graphics g = image.createGraphics ()

G.drawImage (src, 0,0, wideth, height, null)

/ / String s = "www.qhd.com.cn"

G.setColor (new Color (color,false))

G.setFont (new Font (fontName, fontStyle, fontSize))

G.drawString (pressText, wideth-fontSize-x, height-fontSize

/ 2-y)

G.dispose ()

FileOutputStream out = new FileOutputStream (targetImg)

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder (out)

Encoder.encode (image)

Out.close ()

} catch (Exception e) {

System.out.println (e)

}

}

Public static void main (String [] args) {

PressText (Li Baidu, e:1.jpg, font-weight, Font.BOLD,255,70,700, 100)

PressImage ("e:/2.png", "e:/1.jpg", 0,0)

}

}

This is the end of the article on "how to add watermarks to Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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