zoukankan      html  css  js  c++  java
  • 【Java】图片添加水印及水印文字的操作

    添加文字水印

      1 package pers.io;
      2 
      3 import java.awt.Color;
      4 import java.awt.Font;
      5 import java.awt.FontMetrics;
      6 import java.awt.Graphics2D;
      7 import java.awt.RenderingHints;
      8 import java.awt.image.BufferedImage;
      9 import java.io.File;
     10 import java.io.FileOutputStream;
     11 import java.io.IOException;
     12 import java.util.Random;
     13 
     14 import javax.imageio.ImageIO;
     15 
     16 /**
     17  * 水印添加
     18  * 
     19  * @author three
     20  *
     21  */
     22 public class Watermark {
     23 
     24     public static void main(String[] args) {
     25         mark1();
     26         mark2();
     27         mark3();
     28     }
     29 
     30     /**
     31      * 方法(一)
     32      * 系统自带字体
     33      */
     34     public static void mark1() {
     35         try {
     36             BufferedImage src = ImageIO.read(new File("d:/01.jpg"));
     37             Graphics2D g = src.createGraphics();
     38             // 抗锯齿
     39             g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
     40             // 设置字体
     41             Font font = new Font("隶书", Font.BOLD, 30);
     42             g.setFont(font);
     43 
     44             String str = "three 205024319";
     45             int x = 10;
     46             int y = 40;
     47             g.drawString(str, x, y);
     48             g.dispose();
     49 
     50             ImageIO.write(src, "jpg", new FileOutputStream("d:/mark.jpg"));
     51         } catch (IOException e) {
     52             // TODO Auto-generated catch block
     53             e.printStackTrace();
     54         }
     55 
     56     }
     57 
     58     /**
     59      * 方法(二)
     60      * 装载外部字体
     61      */
     62     public static void mark2() {
     63         try {
     64             BufferedImage src = ImageIO.read(new File("d:\mark.jpg"));
     65             Graphics2D g = src.createGraphics();
     66             g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
     67 
     68             //设置字体,使用下载的字体
     69             Font font = Font.createFont(Font.TRUETYPE_FONT, new File("f:\download\font_1.ttf"));
     70             Font ff = font.deriveFont(Font.PLAIN, 30f);
     71             g.setFont(ff);
     72 
     73             String str = "版权所有 205024319";
     74             int x = 10;
     75             int y = 80;
     76             g.drawString(str, x, y);
     77             g.dispose();
     78 
     79             ImageIO.write(src, "jpg", new FileOutputStream("d:\mark.jpg"));
     80         } catch (Exception e) {
     81             e.printStackTrace();
     82         }
     83     }
     84 
     85     /**
     86      * 字体定位(水印位置)
     87      */
     88     public static void mark3() {
     89         try {
     90             BufferedImage src = ImageIO.read(new File("d:\01.jpg"));
     91             int iw = src.getWidth();
     92             int ih = src.getHeight();
     93 
     94             Graphics2D g = src.createGraphics();
     95             g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
     96 
     97             Font font = new Font("", Font.ITALIC, 30);
     98             g.setFont(font);
     99 
    100             String str = "three 205024319";
    101             FontMetrics fm = g.getFontMetrics();
    102             int fw = fm.stringWidth(str);
    103             int fh = fm.getHeight();
    104 
    105             // 左上角
    106             int x = 0;
    107             int y = fh;
    108             g.setColor(Color.WHITE);
    109             g.drawString(str, x, y);
    110 
    111             // 右下角
    112             x = iw - fw;
    113             y = ih;
    114             g.drawString(str, x, y);
    115 
    116             // 中心
    117             x = (iw - fw) / 2;
    118             y = ih / 2;
    119             g.drawString(str, x, y);
    120 
    121             // 随机位置
    122             g.setColor(Color.BLUE);
    123             for (int i = 0; i <= 5; i++) {
    124                 Random rand = new Random();
    125                 x = rand.nextInt(iw - fw);
    126                 y = rand.nextInt(ih - fh) + fh;
    127                 g.drawString(str, x, y);
    128             }
    129 
    130             g.dispose();
    131 
    132             ImageIO.write(src, "jpg", new FileOutputStream("d:\markPlus.jpg"));
    133         } catch (IOException e) {
    134             e.printStackTrace();
    135         }
    136 
    137     }
    138 
    139 }

    添加图片水印

     1     /**
     2      * 图片水印
     3      */
     4     public static void imgMark(File file) {
     5         try {
     6             BufferedImage i = ImageIO.read(file);
     7             int iw = i.getWidth();
     8             int ih = i.getHeight();
     9             
    10             //选择要添加的logo
    11             BufferedImage logo = ImageIO.read(new File("d:\logo.png"));
    12             int lw = logo.getWidth();
    13             int lh = logo.getHeight();
    14             
    15             Graphics2D g = i.createGraphics();
    16             //右下角位置
    17             g.drawImage(logo, iw-lw-10,ih-lh-10, null);
    18             g.dispose();
    19             
    20             ImageIO.write(i, file.getName().substring(file.getName().lastIndexOf(".")+1), new FileOutputStream(file));
    21         } catch (IOException e) {
    22             e.printStackTrace();
    23         }
    24     }
  • 相关阅读:
    Flink 源码(五):Flink 环境准备及提交流程(二)创建 Yarn 客户端应用程序
    Flink 源码(四):Flink 环境准备及提交流程(一)程序起点与创建 Yarn 客户端应用程序
    Hive高级(6):优化(6)explain执行计划详解(一)
    Flink实例(129):状态管理(十八)FLINK-SQL使用基础(13)Table API 和 SQL 模块状态管理(三) Flink SQL空闲状态保留时间(idle state retention time)实现原理
    Flink实例(128):状态管理(十七)FLINK-SQL使用基础(12)Table API 和 SQL 模块状态管理(二)
    Flink实例(127):状态管理(十六)FLINK-SQL使用基础(11)Table API 和 SQL 模块状态管理(一)
    Flink实例(126):状态管理(十五)State 过期时间TTL
    GIT基础(十):Git更新代码到本地仓库
    Java 面向对象(一)面向对象思想
    rust test
  • 原文地址:https://www.cnblogs.com/netyts/p/13795076.html
Copyright © 2011-2022 走看看