国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

教你怎么用Java實(shí)現給圖片打上水印

發(fā)布時(shí)間:2021-07-17 21:51 來(lái)源:腳本之家 閱讀:0 作者:牙刷冰冰 欄目: 編程語(yǔ)言 歡迎投稿:712375056

目錄

一、原圖片

二、打水印(文字)

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

public class ImageUtils {

    // 水印字體
    private static final Font FONT = new Font("微軟雅黑", Font.PLAIN, 14);

    // 透明度
    private static final AlphaComposite COMPOSITE = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f);

    // 水印之間的間隔
    private static final int XMOVE = 150;

    // 水印之間的間隔
    private static final int YMOVE = 200;
    
    /**
     * 打水印(文字)
     *
     * @param srcImgPath       源文件地址
     * @param font             字體
     * @param markContentColor 水印顏色
     * @param waterMarkContent 水印內容
     */
    public static void markWithContent(String srcImgPath, Font font, Color markContentColor, String waterMarkContent) {
        FileOutputStream fos = null;
        try {
            // 讀取原圖片信息
            File srcFile = new File(srcImgPath);
            BufferedImage srcImg = ImageIO.read(srcFile);

            // 圖片寬、高
            int imgWidth = srcImg.getWidth();
            int imgHeight = srcImg.getHeight();

            // 圖片緩存
            BufferedImage bufImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);

            // 創(chuàng  )建繪圖工具
            Graphics2D g = bufImg.createGraphics();

            // 畫(huà)入原始圖像
            g.drawImage(srcImg, 0, 0, imgWidth, imgHeight, null);

            // 設置水印顏色
            g.setColor(markContentColor);

            // 設置水印透明度
            g.setComposite(COMPOSITE);

            // 設置傾斜角度
            g.rotate(Math.toRadians(-35), (double) bufImg.getWidth() / 2, (double) bufImg.getHeight() / 2);

            // 設置水印字體
            g.setFont(font);

            // 消除java.awt.Font字體的鋸齒
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            int x = -imgWidth / 2;
            int y;

            // 字體長(cháng)度
            int markWidth = FONT.getSize() * getTextLength(waterMarkContent);
            // 字體高度
            int markHeight = FONT.getSize();

            // 循環(huán)添加水印
            while (x < imgWidth * 1.5) {
                y = -imgHeight / 2;
                while (y < imgHeight * 1.5) {
                    g.drawString(waterMarkContent, x, y);
                    y += markHeight + YMOVE;
                }
                x += markWidth + XMOVE;
            }

            // 釋放畫(huà)圖工具
            g.dispose();

            // 輸出圖片
            fos = new FileOutputStream(srcFile);
            ImageIO.write(bufImg, "jpg", fos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


    //計算水印文本長(cháng)度
    //1、中文長(cháng)度即文本長(cháng)度 2、英文長(cháng)度為文本長(cháng)度二分之一
    public static int getTextLength(String text) {
        //水印文字長(cháng)度
        int length = text.length();

        for (int i = 0; i < text.length(); i++) {
            String s = String.valueOf(text.charAt(i));
            if (s.getBytes().length > 1) {
                length++;
            }
        }
        length = length % 2 == 0 ? length / 2 : length / 2 + 1;
        return length;
    }
    
    
    public static void main(String[] args) {
        ImageUtils.markWithContent("C:\\Users\\lbb\\Pictures\\test.jpg", FONT, Color.darkGray, "水印文字");
    }
}

打了水印后的效果

三、打水印(圖片)

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

public class ImageUtils {

    // 透明度
    private static final AlphaComposite COMPOSITE = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f);

    // 水印之間的間隔
    private static final int XMOVE = 150;

    // 水印之間的間隔
    private static final int YMOVE = 200;

    /**
     * 打水印(圖片)
     *
     * @param srcImgPath  源圖片路徑
     * @param markImgPath 水印圖片路徑
     */
    public static void markWithImg(String srcImgPath, String markImgPath) {
        FileOutputStream fos = null;

        try {
            // 讀取原始圖像
            File srcFile = new File(srcImgPath);
            BufferedImage srcImg = ImageIO.read(srcFile);

            // 原始寬度
            int srcImgWidth = srcImg.getWidth();
            // 原始高度
            int srcImgHeight = srcImg.getHeight();

            // 最終圖像
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);

            // 創(chuàng  )建繪圖工具
            Graphics2D g = bufImg.createGraphics();

            // 畫(huà)入原始圖像
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);

            // 讀取水印圖片
            BufferedImage markImg = ImageIO.read(new File(markImgPath));

            // 圖片寬、高
            int markImgWidth = markImg.getWidth();
            int markImgHeight = markImg.getHeight();

            // 設置水印透明度
            g.setComposite(COMPOSITE);

            // 設置傾斜角度
            g.rotate(Math.toRadians(-35), (double) bufImg.getWidth() / 2, (double) bufImg.getHeight() / 2);

            // 循環(huán)添加水印
            int x = -srcImgWidth / 2;
            int y;
            while (x < srcImgWidth * 1.5) {
                y = -srcImgHeight / 2;
                while (y < srcImgHeight * 1.5) {
                    g.drawImage(markImg, x, y, null);
                    y += markImgHeight + YMOVE;
                }
                x += markImgWidth + XMOVE;
            }

            // 釋放畫(huà)圖工具
            g.dispose();

            // 輸出圖片
            fos = new FileOutputStream(srcFile);
            ImageIO.write(bufImg, "jpg", fos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    public static void main(String[] args) {
        ImageUtils.markWithImg("C:\\Users\\lbb\\Pictures\\test.jpg", "C:\\Users\\lbb\\Pictures\\mark.png");
    }
}

下面是水印圖片

下面是打了水印后的效果

到此這篇關(guān)于教你怎么用Java實(shí)現給圖片打上水印的文章就介紹到這了,更多相關(guān)Java實(shí)現給圖片打上水印內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

爱情岛论坛亚洲品质自拍HD| 亚洲无线码在线一区观看| 内射一区二区精品视频在线观看| 久本草在线中文字幕亚洲| 亚洲AV无码潮喷在线观看蜜桃| 五月丁香国产在线视频|