91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

Java怎么給圖片加水印

小億
102
2024-01-04 02:11:29
欄目: 編程語言

在Java中給圖片加水印可以通過以下步驟實現:

  1. 導入相關的類庫:
import java.awt.AlphaComposite;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
  1. 加載原始圖片和水印圖片:
BufferedImage originalImage = ImageIO.read(new File("original.jpg"));
BufferedImage watermarkImage = ImageIO.read(new File("watermark.png"));
  1. 創建一個新的圖片對象,大小與原始圖片相同:
BufferedImage combinedImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_RGB);
  1. 創建一個圖形對象并設置抗鋸齒和渲染模式:
Graphics2D graphics = (Graphics2D) combinedImage.getGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  1. 將原始圖片繪制到新的圖片對象上:
graphics.drawImage(originalImage, 0, 0, null);
  1. 設置水印圖片的透明度和位置,并繪制到新的圖片對象上:
AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f); // 設置透明度為0.5
graphics.setComposite(alphaComposite);

int x = combinedImage.getWidth() - watermarkImage.getWidth() - 10; // 水印圖片的x坐標
int y = combinedImage.getHeight() - watermarkImage.getHeight() - 10; // 水印圖片的y坐標
graphics.drawImage(watermarkImage, x, y, null);
  1. 添加水印文本:
graphics.setFont(new Font("Arial", Font.BOLD, 12));
graphics.drawString("watermark", 10, 20);
  1. 保存新的圖片對象為文件:
ImageIO.write(combinedImage, "JPEG", new File("result.jpg"));

完整示例代碼如下:

import java.awt.AlphaComposite;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class ImageWatermarkExample {
    public static void main(String[] args) {
        try {
            BufferedImage originalImage = ImageIO.read(new File("original.jpg"));
            BufferedImage watermarkImage = ImageIO.read(new File("watermark.png"));

            BufferedImage combinedImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_RGB);

            Graphics2D graphics = (Graphics2D) combinedImage.getGraphics();
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

            graphics.drawImage(originalImage, 0, 0, null);

            AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
            graphics.setComposite(alphaComposite);

            int x = combinedImage.getWidth() - watermarkImage.getWidth() - 10;
            int y = combinedImage.getHeight() - watermarkImage.getHeight() - 10;
            graphics.drawImage(watermarkImage, x, y, null);

            graphics.setFont(new Font("Arial", Font.BOLD, 12));
            graphics.drawString("watermark", 10, 20);

            ImageIO.write(combinedImage, "JPEG", new File("result.jpg"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

以上代碼將原始圖片和水印圖片合并為一張新的圖片,保存為result.jpg。其中,original.jpg是原始圖片,watermark.png是水印圖片,"watermark"是水印文本。您可以根據實際情況修改文件路徑和水印位置、透明度等參數。

0
西林县| 连江县| 平江县| 青河县| 遂溪县| 东宁县| 长顺县| 文成县| 阳原县| 华阴市| 会理县| 西乌珠穆沁旗| 桐柏县| 盘锦市| 黔西| 古田县| 武宁县| 田林县| 雷波县| 寿阳县| 南京市| 海晏县| 中超| 九江县| 芦溪县| 莱阳市| 祁东县| 丰都县| 香格里拉县| 沈丘县| 长泰县| 清徐县| 阿瓦提县| 资中县| 焦作市| 淄博市| 密山市| 健康| 开封市| 阿克| 乐安县|