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

Implementation method of adding watermark by java drawing tool

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains the "java drawing tool to add watermark implementation method", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "java drawing tool to add watermark implementation method" bar!

Watermark the picture with Java code

Don't beep much, just go to the code:

/ * @ param srcImgFile original picture file object * @ param outFile output picture file object * @ param waterMarkContent watermark content * @ param markContentColor watermark color * @ param rate font spacing * @ param x x axis location * @ param y y axis Location * @ param font font * / public static void addWaterMark (File srcImgFile File outFile, String waterMarkContent, Color markContentColor, double rate, int x, int y, Font font) {try {/ / read the original picture information Image srcImg = ImageIO.read (srcImgFile) / / convert the file to an image int srcImgWidth = srcImg.getWidth (null); / / get the width of the picture int srcImgHeight = srcImg.getHeight (null); / / get the high / / watermarked image BufferedImage bufImg = new BufferedImage (srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB); / / get the image buffer Graphics2D g = bufImg.createGraphics () / / create Graphics2D brush object g.drawImage (srcImg, 0,0, srcImgWidth, srcImgHeight, null); g.setColor (markContentColor); / / set the watermark color g.setFont (font) according to the background of the image; / / set the font WaterMarkUtils.MyDrawString (waterMarkContent,x,y,rate,g) / / set font spacing plus output g.dispose (); / / process / / output image FileOutputStream outImgStream = new FileOutputStream (outFile); / / create file output stream ImageIO.write (bufImg, "jpg", outImgStream); System.out.println ("add watermark complete") OutImgStream.flush (); / / refresh the file outImgStream.close (); / / release resources} catch (Exception e) {System.out.println ("exception"); e.printStackTrace ();}} set font spacing

This is found on Baidu, the original posted address https://blog.csdn.net/zixiaomuwu/article/details/51068698

/ * set font spacing plus output * @ param str output string * @ param x x axis * @ param y y axis * @ param rate font spacing * @ param g brush object * / public static void MyDrawString (String str,int x ther int yre double rate,Graphics2D g) {String tempStr=new String () Int orgStringWight=g.getFontMetrics (). StringWidth (str); int orgStringLength=str.length (); int tempx=x; int tempy=y; while (str.length () > 0) {tempStr=str.substring (0,1); str=str.substring (1, str.length ()); g.drawString (tempStr, tempx, tempy) Tempx= (int) (tempx+ (double) orgStringWight/ (double) orgStringLength*rate);}} Thank you for reading, this is the content of "java drawing tool to add watermark". After the study of this article, I believe you have a deeper understanding of the implementation of java drawing tool to add watermark, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report