Java 製作圖片水印

package org.zj.util;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
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 class WaterMark {
 
 public static int MARK_POSITION_LEFT_UP = 0;
 public static int MARK_POSITION_RIGHT_UP = 1;
 public static int MARK_POSITION_LEFT_DOWN = 2;
 public static int MARK_POSITION_RIGHT_DOWN = 3;
 
 
  
    public final static void pressImage(String pressImg, String targetImg, int x, int y, float alpha) {  
        try {  
            File img = new File(targetImg);  
            Image src = ImageIO.read(img);  
            int wideth = src.getWidth(null);  
            int height = src.getHeight(null);  
            BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);  
            Graphics2D g = image.createGraphics();  
            g.drawImage(src, 0, 0, wideth, height, null);  
            //水印文件  
            Image src_biao = ImageIO.read(new File(pressImg));  
            int wideth_biao = src_biao.getWidth(null);  
            int height_biao = src_biao.getHeight(null);  
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
           
            //左下角距離邊距10像素
            int leftDownWideth = 10;
            int leftDownHeight = (height - height_biao) - 10;
           
            //右下角距離邊距10像素
            int rightDownWideth = (wideth - wideth_biao) - 10;
            int rightDownHeight = (height - height_biao) - 10;
           
            //左上角距離邊距10像素
            int leftUpWideth = 10;
            int leftUpHeight = 10;
           
            //右上角距離邊距10像素
            int rightUpWideth = (wideth - wideth_biao) - 10;
            int rightUpHeight = 10;
           
            //水印位置
            g.drawImage(src_biao, x, y , wideth_biao, height_biao, null);  
           
           
            //水印文件結束  
            g.dispose();  
            ImageIO.write((BufferedImage) image, "jpg", img);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
   
   
     
    public final static void pressImage(String pressImg, String targetImg, int position, float alpha) {  
     try {  
      File img = new File(targetImg);  
      Image src = ImageIO.read(img);  
      int wideth = src.getWidth(null);  
      int height = src.getHeight(null);  
      BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);  
      Graphics2D g = image.createGraphics();  
      g.drawImage(src, 0, 0, wideth, height, null);  
      //水印文件  
      Image src_biao = ImageIO.read(new File(pressImg));  
      int wideth_biao = src_biao.getWidth(null);  
      int height_biao = src_biao.getHeight(null);  
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
      
      //左下角距離邊距10像素
      int leftDownWideth = 10;
      int leftDownHeight = (height - height_biao) - 10;
      
      //右下角距離邊距10像素
      int rightDownWideth = (wideth - wideth_biao) - 10;
      int rightDownHeight = (height - height_biao) - 10;
      
      //左上角距離邊距10像素
      int leftUpWideth = 10;
      int leftUpHeight = 10;
      
      //右上角距離邊距10像素
      int rightUpWideth = (wideth - wideth_biao) - 10;
      int rightUpHeight = 10;
      
      
      int x = 0;
      int y = 0;
      
      switch (position) {
   case 0:
    x = leftUpWideth;
    y = leftUpHeight;
    break;
   case 1:
    x = rightUpWideth;
    y = rightUpHeight;
    break;
   case 2:
    x = leftDownWideth;
    y = leftDownHeight;
    break;
   case 3:
    x = rightDownWideth;
    y = rightDownHeight;
    break;
    
   default:
    break;
   }
      
      
      //水印位置
      g.drawImage(src_biao, x, y , wideth_biao, height_biao, null);  
      
      //水印文件結束
      g.dispose();  
      ImageIO.write((BufferedImage) image, "jpg", img);  
     } catch (Exception e) {  
      e.printStackTrace();  
     }  
    }  


 
   
    public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color, int fontSize, int x, int y, float alpha) {  
        try {  
            File img = new File(targetImg);  
            Image src = ImageIO.read(img);  
            int width = src.getWidth(null);  
            int height = src.getHeight(null);  
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
            Graphics2D g = image.createGraphics();  
            g.drawImage(src, 0, 0, width, height, null);  
            g.setColor(color);  
            g.setFont(new Font(fontName, fontStyle, fontSize));  
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
           
           
            // 左上角
            int leftUpWideth = 10;
            int leftUpHeight = fontSize+10;
           
            // 左上角
            int rightUpWideth = (width - (getLength(pressText) * fontSize)) - 30;
            int rightUpHeight = fontSize+10;
           
           
          //左下角
            int leftDownWideth = 10;
            int leftDownHeight = height - fontSize;
           
            //右下角
            int rightDownWideth = (width - (getLength(pressText) * fontSize)) - 30;
            int rightDownHeight = height - 10;
           
           
            g.drawString(pressText, x, y);
            g.dispose();  
            ImageIO.write((BufferedImage) image, "jpg", img);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }
   
     
    public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color, int fontSize, int position, float alpha) {  
     try {  
      File img = new File(targetImg);  
      Image src = ImageIO.read(img);  
      int width = src.getWidth(null);  
      int height = src.getHeight(null);  
      BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
      Graphics2D g = image.createGraphics();  
      g.drawImage(src, 0, 0, width, height, null);  
      g.setColor(color);  
      g.setFont(new Font(fontName, fontStyle, fontSize));  
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
      
      
      // 左上角
      int leftUpWideth = 10;
      int leftUpHeight = fontSize+10;
      
      // 右上角
      int rightUpWideth = (width - (getLength(pressText) * fontSize)) - 30;
      int rightUpHeight = fontSize+10;
      
      
      //左下角
      int leftDownWideth = 10;
      int leftDownHeight = height - fontSize;
      
      //右下角
      int rightDownWideth = (width - (getLength(pressText) * fontSize)) - 30;
      int rightDownHeight = height - 10;
      
      int x = 0;
      int y = 0;
      
      switch (position) {
   case 0:
    x = leftUpWideth;
    y = leftUpHeight;
    break;
   case 1:
    x = rightUpWideth;
    y = rightUpHeight;
    break;
   case 2:
    x = leftDownWideth;
    y = leftDownHeight;
    break;
   case 3:
    x = rightDownWideth;
    y = rightDownHeight;
    break;
    
   default:
    break;
   }
      
      g.drawString(pressText, x, y);
      g.dispose();  
      ImageIO.write((BufferedImage) image, "jpg", img);  
     } catch (Exception e) {  
      e.printStackTrace();  
     }  
    }
   
   
   
    public static int getLength(String text) {  
        int length = 0;  
        for (int i = 0; i < text.length(); i++) {  
            if (new String(text.charAt(i) + "").getBytes().length > 1) {  
                length += 2;  
            } else {  
                length += 1;  
            }  
        }  
        return length / 2;  
    }  
   
   
 
 public static void ImgYin(String s,String ImgPath){   
     try{   
      File _file = new File(ImgPath);   
      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="我要加的水印";   
      g.setColor(Color.RED);
      g.setFont(new Font("宋體",Font.PLAIN,20));   
      Font aa=new Font("宋體",Font.PLAIN,20);   
     
      // 所寫文字位置
      g.drawString(s,wideth-wideth/2,height-height/4);   
     
      g.dispose();   
      FileOutputStream out=new FileOutputStream(ImgPath);   
      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);   
      encoder.encode(image);   
      out.close();   
     }   
     catch(Exception e){   
      System.out.println(e);   
     }   
 }


 public static void main(String[] args) {
//  pressText("測試test.zz","E:/11.jpg","宋體",Font.BOLD,Color.red,20,20,20,1.0f);
//  ImgYin("測試測試測試哈哈","E:/11.jpg");
//  pressImage("E:/3.jpg","E:/11.jpg",100,18,0.5f);
  System.out.println("執行完畢...");
 }
 
}

發佈了106 篇原創文章 · 獲贊 4 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章