Record Class ImageWatermark

java.lang.Object
java.lang.Record
cloud.opencode.base.image.watermark.ImageWatermark
Record Components:
position - the position | 位置
opacity - the opacity (0.0 to 1.0) | 透明度
margin - the margin from edge | 边缘间距

Security | 安全性:

  • Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
All Implemented Interfaces:
Watermark

public record ImageWatermark(Position position, float opacity, int margin) extends Record implements Watermark
Image Watermark 图片水印

Immutable record describing an image watermark configuration. The actual watermark pixels are passed at apply-time as a Raster, keeping the record lightweight and AWT-free.

描述图片水印配置的不可变 record。实际水印像素在调用 apply(Raster, Raster) 时以 Raster 形式传入,保持本 record 轻量 且不依赖 AWT。

Usage Examples | 使用示例:

// Simple watermark
ImageWatermark watermark = ImageWatermark.of();

// With position and opacity
ImageWatermark watermark = ImageWatermark.builder()
    .position(Position.BOTTOM_RIGHT)
    .opacity(0.5f)
    .margin(20)
    .build();

// Apply
Raster result = watermark.apply(srcRaster, logoRaster);
Since:
JDK 25, opencode-base-image V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

    • DEFAULT_OPACITY

      public static final float DEFAULT_OPACITY
      Default opacity | 默认透明度
      See Also:
    • DEFAULT_MARGIN

      public static final int DEFAULT_MARGIN
      Default margin | 默认边距
      See Also:
  • Constructor Details

    • ImageWatermark

      public ImageWatermark(Position position, float opacity, int margin)
      Compact constructor with validation 紧凑构造函数(带验证)
  • Method Details

    • of

      public static ImageWatermark of()
      Create a default watermark configuration (BOTTOM_RIGHT, opacity=1.0, margin=10). 创建默认水印配置(BOTTOM_RIGHT,opacity=1.0,margin=10)。
      Returns:
      the watermark | 水印
    • of

      public static ImageWatermark of(Position position)
      Create watermark configuration with the given position. 创建带指定位置的水印配置。
      Parameters:
      position - the position | 位置
      Returns:
      the watermark | 水印
    • of

      public static ImageWatermark of(Position position, float opacity)
      Create watermark configuration with position and opacity. 创建带位置和透明度的水印配置。
      Parameters:
      position - the position | 位置
      opacity - the opacity | 透明度
      Returns:
      the watermark | 水印
    • withPosition

      public ImageWatermark withPosition(Position newPosition)
      Create copy with different position 创建使用不同位置的副本
      Parameters:
      newPosition - the new position | 新位置
      Returns:
      new watermark | 新水印
    • withOpacity

      public ImageWatermark withOpacity(float newOpacity)
      Create copy with different opacity 创建使用不同透明度的副本
      Parameters:
      newOpacity - the new opacity | 新透明度
      Returns:
      new watermark | 新水印
    • withMargin

      public ImageWatermark withMargin(int newMargin)
      Create copy with different margin 创建使用不同边距的副本
      Parameters:
      newMargin - the new margin | 新边距
      Returns:
      new watermark | 新水印
    • builder

      public static ImageWatermark.Builder builder()
      Create a builder 创建构建器
      Returns:
      the builder | 构建器
    • builder

      public static ImageWatermark.Builder builder(ImageWatermark watermark)
      Create a builder from existing watermark 从现有水印创建构建器
      Parameters:
      watermark - the existing watermark | 现有水印
      Returns:
      the builder | 构建器
    • apply

      public Raster apply(Raster raster, Raster watermarkRaster)
      Apply this watermark on a Raster using a Raster-based watermark image (pure-Java alpha blending, no AWT). 使用 Raster 形式的水印图在 Raster 上应用本水印(纯 Java alpha 混合,不使用 AWT)。
      Parameters:
      raster - target raster | 目标 raster
      watermarkRaster - watermark raster (e.g. RGBA_8) | 水印 raster
      Returns:
      watermarked RGBA_8 raster | 加水印的 RGBA_8 raster
      Throws:
      NullPointerException - if any argument is null | 任意参数为 null 时抛出
      Since:
      opencode-base-image V1.0.4
    • applyTiled

      public Raster applyTiled(Raster raster, Raster watermarkRaster, int spacingX, int spacingY)
      Tile a Raster-based watermark across a target Raster. 在目标 Raster 上平铺 Raster 水印。
      Parameters:
      raster - target raster | 目标 raster
      watermarkRaster - watermark raster | 水印 raster
      spacingX - horizontal spacing | 水平间距
      spacingY - vertical spacing | 垂直间距
      Returns:
      tiled-watermarked RGBA_8 raster | 平铺水印后的 RGBA_8 raster
      Throws:
      NullPointerException - if any argument is null | 任意参数为 null 时抛出
      Since:
      opencode-base-image V1.0.4
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • position

      public Position position()
      Returns the value of the position record component.
      Specified by:
      position in interface Watermark
      Returns:
      the value of the position record component
    • opacity

      public float opacity()
      Returns the value of the opacity record component.
      Specified by:
      opacity in interface Watermark
      Returns:
      the value of the opacity record component
    • margin

      public int margin()
      Returns the value of the margin record component.
      Specified by:
      margin in interface Watermark
      Returns:
      the value of the margin record component