Class Images

java.lang.Object
org.miaixz.bus.extra.image.Images
All Implemented Interfaces:
Flushable, Serializable

public class Images extends Object implements Flushable, Serializable
图像编辑器
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Constructor Details

    • Images

      public Images(BufferedImage srcImage)
      构造,目标图片类型取决于来源图片类型
      Parameters:
      srcImage - 来源图片
    • Images

      public Images(BufferedImage srcImage, String targetImageType)
      构造
      Parameters:
      srcImage - 来源图片
      targetImageType - 目标图片类型,null则读取来源图片类型
  • Method Details

    • from

      public static Images from(Path imagePath)
      从Path读取图片并开始处理
      Parameters:
      imagePath - 图片文件路径
      Returns:
      Img
    • from

      public static Images from(File imageFile)
      从文件读取图片并开始处理
      Parameters:
      imageFile - 图片文件
      Returns:
      Img
    • from

      public static Images from(org.miaixz.bus.core.io.resource.Resource resource)
      从资源对象中读取图片并开始处理
      Parameters:
      resource - 图片资源对象
      Returns:
      Img
    • from

      public static Images from(InputStream in)
      从流读取图片并开始处理
      Parameters:
      in - 图片流
      Returns:
      Img
    • from

      public static Images from(ImageInputStream imageStream)
      从ImageInputStream取图片并开始处理
      Parameters:
      imageStream - 图片流
      Returns:
      Img
    • from

      public static Images from(URL imageUrl)
      从URL取图片并开始处理
      Parameters:
      imageUrl - 图片URL
      Returns:
      Img
    • from

      public static Images from(Image image)
      从Image取图片并开始处理
      Parameters:
      image - 图片
      Returns:
      Img
    • setTargetImageType

      public Images setTargetImageType(String imgType)
      设置目标图片文件格式,用于写出
      Parameters:
      imgType - 图片格式
      Returns:
      this
      See Also:
    • setPositionBaseCentre

      public Images setPositionBaseCentre(boolean positionBaseCentre)
      计算x,y坐标的时候是否从中心做为原始坐标开始计算
      Parameters:
      positionBaseCentre - 是否从中心做为原始坐标开始计算
      Returns:
      this
    • setQuality

      public Images setQuality(double quality)
      设置图片输出质量,数字为0~1(不包括0和1)表示质量压缩比,除此数字外设置表示不压缩
      Parameters:
      quality - 质量,数字为0~1(不包括0和1)表示质量压缩比,除此数字外设置表示不压缩
      Returns:
      this
    • setQuality

      public Images setQuality(float quality)
      设置图片输出质量,数字为0~1(不包括0和1)表示质量压缩比,除此数字外设置表示不压缩
      Parameters:
      quality - 质量,数字为0~1(不包括0和1)表示质量压缩比,除此数字外设置表示不压缩
      Returns:
      this
    • setBackgroundColor

      public Images setBackgroundColor(Color backgroundColor)
      设置图片的背景色
      Parameters:
      backgroundColor - Ansi4BitColor 背景色
      Returns:
      this
    • scale

      public Images scale(float scale)
      缩放图像(按比例缩放)
      Parameters:
      scale - 缩放比例。比例大于1时为放大,小于1大于0为缩小
      Returns:
      this
    • scale

      public Images scale(int width, int height)
      缩放图像(按长宽缩放) 注意:目标长宽与原图不成比例会变形
      Parameters:
      width - 目标宽度
      height - 目标高度
      Returns:
      this
    • scale

      public Images scale(int width, int height, int scaleType)
      缩放图像(按长宽缩放) 注意:目标长宽与原图不成比例会变形
      Parameters:
      width - 目标宽度
      height - 目标高度
      scaleType - 缩放类型,可选Image.SCALE_SMOOTH平滑模式或Image.SCALE_DEFAULT默认模式
      Returns:
      this
    • scale

      public Images scale(int width, int height, Color fixedColor)
      等比缩放图像,此方法按照按照给定的长宽等比缩放图片,按照长宽缩放比最多的一边等比缩放,空白部分填充背景色 缩放后默认为jpeg格式
      Parameters:
      width - 缩放后的宽度
      height - 缩放后的高度
      fixedColor - 比例不对时补充的颜色,不补充为null
      Returns:
      this
    • cut

      public Images cut(Rectangle rectangle)
      图像切割(按指定起点坐标和宽高切割)
      Parameters:
      rectangle - 矩形对象,表示矩形区域的x,y,width,height
      Returns:
      this
    • cut

      public Images cut(int x, int y)
      图像切割为圆形(按指定起点坐标和半径切割),填充满整个图片(直径取长宽最小值)
      Parameters:
      x - 原图的x坐标起始位置
      y - 原图的y坐标起始位置
      Returns:
      this
    • cut

      public Images cut(int x, int y, int radius)
      图像切割为圆形(按指定起点坐标和半径切割)
      Parameters:
      x - 原图的x坐标起始位置
      y - 原图的y坐标起始位置
      radius - 半径,小于0表示填充满整个图片(直径取长宽最小值)
      Returns:
      this
    • round

      public Images round(double arc)
      图片圆角处理
      Parameters:
      arc - 圆角弧度,0~1,为长宽占比
      Returns:
      this
    • gray

      public Images gray()
      彩色转为灰度
      Returns:
      this
    • binary

      public Images binary()
      彩色转为黑白二值化图片
      Returns:
      this
    • pressText

      public Images pressText(String pressText, Color color, Font font, int x, int y, float alpha)
      给图片添加文字水印 此方法只在给定位置写出一个水印字符串
      Parameters:
      pressText - 水印文字
      color - 水印的字体颜色
      font - Font 字体相关信息
      x - 修正值。 默认在中间,偏移量相对于中间偏移
      y - 修正值。 默认在中间,偏移量相对于中间偏移
      alpha - 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
      Returns:
      处理后的图像
    • pressText

      public Images pressText(ImageText imageText)
      给图片添加文字水印 此方法只在给定位置写出一个水印字符串
      Parameters:
      imageText - 显示的文本信息
      Returns:
      处理后的图像
    • pressTextFull

      public Images pressTextFull(String pressText, Color color, Font font, int lineHeight, int degree, float alpha)
      给图片添加全屏文字水印
      Parameters:
      pressText - 水印文字,文件间的间隔使用尾部添加空格方式实现
      color - 水印的字体颜色
      font - Font 字体相关信息
      lineHeight - 行高
      degree - 旋转角度,(单位:弧度),以圆点(0,0)为圆心,正代表顺时针,负代表逆时针
      alpha - 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
      Returns:
      处理后的图像
    • pressImage

      public Images pressImage(Image pressImg, int x, int y, float alpha)
      给图片添加图片水印
      Parameters:
      pressImg - 水印图片,可以使用ImageIO.read(File)方法读取文件
      x - 修正值。 默认在中间,偏移量相对于中间偏移
      y - 修正值。 默认在中间,偏移量相对于中间偏移
      alpha - 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
      Returns:
      this
    • pressImage

      public Images pressImage(Image pressImg, Rectangle rectangle, float alpha)
      给图片添加图片水印 如果源图片对象为 BufferedImage,则绘制在源图片上,否则创建新的图片
      Parameters:
      pressImg - 水印图片,可以使用ImageIO.read(File)方法读取文件
      rectangle - 矩形对象,表示矩形区域的x,y,width,height,x,y从背景图片中心计算
      alpha - 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
      Returns:
      this
    • pressImageFull

      public Images pressImageFull(Image pressImage, int lineHeight, int degree, float alpha)
      给图片添加全屏图片水印
      Parameters:
      pressImage - 水印图片
      lineHeight - 行高
      degree - 旋转角度,(单位:弧度),以圆点(0,0)为圆心,正代表顺时针,负代表逆时针
      alpha - 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
      Returns:
      this imashimaro
    • rotate

      public Images rotate(int degree)
      旋转图片为指定角度 来自:http://blog.51cto.com/cping1982/130066
      Parameters:
      degree - 旋转角度
      Returns:
      旋转后的图片
    • flip

      public Images flip()
      水平翻转图像
      Returns:
      this
    • stroke

      public Images stroke(Color color, float width)
      描边,此方法为向内描边,会覆盖图片相应的位置
      Parameters:
      color - 描边颜色,默认黑色
      width - 边框粗细
      Returns:
      this
    • stroke

      public Images stroke(Color color, Stroke stroke)
      描边,此方法为向内描边,会覆盖图片相应的位置
      Parameters:
      color - 描边颜色,默认黑色
      stroke - 描边属性,包括粗细、线条类型等,见BasicStroke
      Returns:
      this
    • getImg

      public Image getImg()
      获取处理过的图片
      Returns:
      处理过的图片
    • write

      public Images write(OutputStream out) throws org.miaixz.bus.core.lang.exception.InternalException
      写出图像为结果设置格式 结果类型设定见setTargetImageType(String)
      Parameters:
      out - 写出到的目标流
      Returns:
      this
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - IO异常
    • write

      public Images write(ImageOutputStream targetImageStream) throws org.miaixz.bus.core.lang.exception.InternalException
      写出图像为结果设置格式 结果类型设定见setTargetImageType(String)
      Parameters:
      targetImageStream - 写出到的目标流
      Returns:
      this
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - IO异常
    • write

      public Images write(File targetFile) throws org.miaixz.bus.core.lang.exception.InternalException
      写出图像为目标文件扩展名对应的格式
      Parameters:
      targetFile - 目标文件
      Returns:
      this
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - IO异常
    • flush

      public void flush()
      Specified by:
      flush in interface Flushable