Java 圖片加文字水印和圖片水印


  1:將字節數組轉換為本地圖片

 /**
         * 將字節數組轉換為本地圖片
         * @param data
         * @param path
         */
    public static void byte2image(byte[] data, String path) {
            if (data.length < 3 || path.equals("")){
                return;
            }
            try {
                FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
                imageOutput.write(data, 0, data.length);
                imageOutput.close();
                System.out.println("生成圖片成功:" + path);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
    }

2:給圖片加圖片水印

/**
 * 為背景圖片增加水印圖片
 *
 * @param path          水印圖片,本地地址,未做網絡圖片轉換
 * @param bgPath        背景圖片,同上
 * @param x              橫坐標,左上角0,往右遞增
 * @param y              縱坐標,左上角0,往下遞增
 * @param width          水印圖片寬度
 * @param height         水印圖片高度
 * @return
 */
public static byte[] generateImage(String path,String bgPath , int x, int y, int width, int height) {
    try {
        BufferedImage watermarkImage = ImageIO.read(new File(path));
        BufferedImage bgImage = ImageIO.read(new File(bgPath));
        int bgWidth = bgImage.getWidth(null);
        int bgHeight = bgImage.getHeight(null);
        BufferedImage image = new BufferedImage(bgWidth, bgHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        g.drawImage(bgImage, 0, 0, bgWidth, bgHeight, null);
        width = width == 0 ? watermarkImage.getWidth() : width;
        height = height == 0 ? watermarkImage.getHeight() : height;
        g.drawImage(watermarkImage, x, y, width, height, null);
        g.dispose();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", out);
        return out.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

 測試

public static void main(String[] args) {
    String path = "D:/image/src/main/picture/1218395382.jpg";
    String waterPath = "D:/image/src/main/picture/sijiali_s1.jpg";
    String imgResult = "D:/image/src/mina/picture/水印.jpg";
    byte[] bytes = generateImage(waterPath,path, 250, 10,  80, 100);
    byte2image(bytes,imgResult);
}

 效果:

原圖

水印圖片

結果圖片

 

3:給圖片加文字水印

/**
 * 添加文字水印的方法
 * @param pressText 要添加的文字
 * @param path 文件路徑,這里只處理了了本地文件,未處理網絡圖片
 * @param x 文字在圖片的橫坐標,左上角為0,往右遞增
 * @param y 文字在圖片的縱坐標,左上角為0,往下遞增
 * @param fontName 文字的字體名稱
 * @param fontSize 文字的大小
 * @param fontColor 文字的眼神
 * @param style 文字的格式,如Font.BOLD++Font.ITALIC表示粗體斜體
 * @return
 */
private static byte[] generateText(String pressText, String path, int x, int y, String fontName,
                         int fontSize, Color fontColor, int style){
    try {
        BufferedImage bgImage= ImageIO.read(new File(path));
        int wideth = bgImage.getWidth(null);
        int height = bgImage.getHeight(null);
        BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        g.drawImage(bgImage, 0, 0, wideth, height, null);

        //設置字體大小、顏色等
        g.setColor(fontColor);
        g.setFont(new Font(fontName, style, fontSize));

        g.drawString(pressText, x, y);
        g.dispose();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", out);
        return out.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

 測試

public static void main(String[] args) {
    String path = "D:/image/src/main/picture/1218395382.jpg";
    String path2 = "D:/image/src/main/picture/文字水印.jpg";
    //Font.BOLD+Font.ITALIC表示加粗且斜體
    byte[] bytes = generateText("斯嘉麗", path, 210, 50, "微軟雅黑", 30, Color.RED, Font.BOLD+Font.ITALIC);
    byte2image(bytes,path2);
}

結果:

原圖

 結果圖片

至此,圖片水印,文字水印完成了!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM