圖片加水印


package com.jiuy.core.util;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 圖片水印
 */
@SuppressWarnings("restriction")
public class WaterMarker {


    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        String srcImgPath = "e:/2.jpg";
        String iconPath = "e:/3.png";
        String targerPath = "e:/img_mark_icon2.jpg";
        long startTime = System.currentTimeMillis();
        byte[] bytes = image2Bytes(iconPath);
        // 給圖片添加水印
        WaterMarker.markImageByIcon(iconPath, srcImgPath, targerPath);
        InputStream inputStream = new FileInputStream(srcImgPath);
        WaterMarker.markImageByIcon(inputStream, bytes);
        System.out.println("markImageByIcon1 time: " + (System.currentTimeMillis() - startTime));

    }

    static byte[] image2Bytes(String imgSrc) throws Exception  
    {  
        FileInputStream fin = new FileInputStream(new File(imgSrc));  
        //可能溢出,簡單起見就不考慮太多,如果太大就要另外想辦法,比如一次傳入固定長度byte[]  
        byte[] bytes  = new byte[fin.available()];  
        //將文件內容寫入字節數組,提供測試的case  
        fin.read(bytes);  

        fin.close();  
        return bytes;  
}  

    /**
     * 給圖片添加水印
     * 
     * @param iconPath
     *            水印圖片路徑
     * @param srcImgPath
     *            源圖片路徑
     * @param targerPath
     *            目標圖片路徑
     */
    public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath) {
        markImageByIcon(iconPath, srcImgPath, targerPath, null);
    }

    /**
     * 給圖片添加水印、可設置水印圖片旋轉角度
     * 
     * @param iconPath
     *            水印圖片路徑
     * @param srcImgPath
     *            源圖片路徑
     * @param targerPath
     *            目標圖片路徑
     * @param degree
     *            水印圖片旋轉角度
     */
    public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath, Integer degree) {


        OutputStream os = null;
        try {
            Image srcImg = ImageIO.read(new File(srcImgPath));

            int width = srcImg.getWidth(null);
            int height = srcImg.getHeight(null);

            BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            // 得到畫筆對象
            Graphics2D g = buffImg.createGraphics();

            // 設置對線段的鋸齒狀邊緣處理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

            g.drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

            // 水印圖象的路徑 水印一般爲gif或者png的,這樣可設置透明度
            ImageIcon imgIcon = new ImageIcon(iconPath);

            // 得到Image對象。
            Image img = imgIcon.getImage();

            float alpha = 0.5f; // 透明度
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));

            System.out.println("width,height: " + width + "," + height);
            // 表示水印圖片的位置
            g.drawImage(img, width / 4, height / 8, null);
            g.drawImage(img, width - img.getWidth(null)/2, height / 32, null);
            g.drawImage(img, -img.getWidth(null)/2, height / 2, null);
            g.drawImage(img, width / 2, height / 2 - height / 16, null);
            g.drawImage(img, width / 8, height - height / 8, null);
            g.drawImage(img, width - img.getWidth(null)/2, height - height / 4, null);

            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));

            g.dispose();

            os = new FileOutputStream(targerPath);

            // 生成圖片
            ImageIO.write(buffImg, "JPG", os);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static byte[] markImageByIcon(InputStream inputStream, byte[] content) {

        try {
            Image srcImg = ImageIO.read(inputStream);

            int width = srcImg.getWidth(null);
            int height = srcImg.getHeight(null);

            BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            // 得到畫筆對象
            Graphics2D g = buffImg.createGraphics();

            // 設置對線段的鋸齒狀邊緣處理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

            g.drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

            // 水印圖象的路徑 水印一般爲gif或者png的,這樣可設置透明度
            ImageIcon imgIcon = new ImageIcon(content);

            // 得到Image對象。
            Image img = imgIcon.getImage();

            float alpha = 0.5f; // 透明度
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));

            System.out.println("width,height: " + width + "," + height);
            // 表示水印圖片的位置
            g.drawImage(img, width / 4, height / 8, null);
            g.drawImage(img, width - img.getWidth(null)/2, height / 32, null);
            g.drawImage(img, -img.getWidth(null)/2, height / 2, null);
            g.drawImage(img, width / 2, height / 2 - height / 16, null);
            g.drawImage(img, width / 8, height - height / 8, null);
            g.drawImage(img, width - img.getWidth(null)/2, height - height / 4, null);

            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));

            g.dispose();

            ByteArrayOutputStream os = new ByteArrayOutputStream();
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
            encoder.encode(buffImg);


            return os.toByteArray();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return null;
    }



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