Package org.miaixz.bus.extra.image
Class Images
java.lang.Object
org.miaixz.bus.extra.image.Images
- All Implemented Interfaces:
Flushable,Serializable
图像编辑器
- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionImages(BufferedImage srcImage) 构造,目标图片类型取决于来源图片类型Images(BufferedImage srcImage, String targetImageType) 构造 -
Method Summary
Modifier and TypeMethodDescriptionbinary()彩色转为黑白二值化图片cut(int x, int y) 图像切割为圆形(按指定起点坐标和半径切割),填充满整个图片(直径取长宽最小值)cut(int x, int y, int radius) 图像切割为圆形(按指定起点坐标和半径切割)图像切割(按指定起点坐标和宽高切割)flip()水平翻转图像voidflush()static Images从Image取图片并开始处理static Images从文件读取图片并开始处理static Imagesfrom(InputStream in) 从流读取图片并开始处理static Images从URL取图片并开始处理static Images从Path读取图片并开始处理static Imagesfrom(ImageInputStream imageStream) 从ImageInputStream取图片并开始处理static Imagesfrom(org.miaixz.bus.core.io.resource.Resource resource) 从资源对象中读取图片并开始处理getImg()获取处理过的图片gray()彩色转为灰度pressImage(Image pressImg, int x, int y, float alpha) 给图片添加图片水印pressImage(Image pressImg, Rectangle rectangle, float alpha) 给图片添加图片水印 如果源图片对象为BufferedImage,则绘制在源图片上,否则创建新的图片pressImageFull(Image pressImage, int lineHeight, int degree, float alpha) 给图片添加全屏图片水印给图片添加文字水印 此方法只在给定位置写出一个水印字符串给图片添加文字水印 此方法只在给定位置写出一个水印字符串pressTextFull(String pressText, Color color, Font font, int lineHeight, int degree, float alpha) 给图片添加全屏文字水印rotate(int degree) 旋转图片为指定角度 来自:http://blog.51cto.com/cping1982/130066round(double arc) 图片圆角处理scale(float scale) 缩放图像(按比例缩放)scale(int width, int height) 缩放图像(按长宽缩放) 注意:目标长宽与原图不成比例会变形scale(int width, int height, int scaleType) 缩放图像(按长宽缩放) 注意:目标长宽与原图不成比例会变形等比缩放图像,此方法按照按照给定的长宽等比缩放图片,按照长宽缩放比最多的一边等比缩放,空白部分填充背景色 缩放后默认为jpeg格式setBackgroundColor(Color backgroundColor) 设置图片的背景色setPositionBaseCentre(boolean positionBaseCentre) 计算x,y坐标的时候是否从中心做为原始坐标开始计算setQuality(double quality) 设置图片输出质量,数字为0~1(不包括0和1)表示质量压缩比,除此数字外设置表示不压缩setQuality(float quality) 设置图片输出质量,数字为0~1(不包括0和1)表示质量压缩比,除此数字外设置表示不压缩setTargetImageType(String imgType) 设置目标图片文件格式,用于写出描边,此方法为向内描边,会覆盖图片相应的位置描边,此方法为向内描边,会覆盖图片相应的位置写出图像为目标文件扩展名对应的格式write(OutputStream out) 写出图像为结果设置格式 结果类型设定见setTargetImageType(String)write(ImageOutputStream targetImageStream) 写出图像为结果设置格式 结果类型设定见setTargetImageType(String)
-
Constructor Details
-
Images
构造,目标图片类型取决于来源图片类型- Parameters:
srcImage- 来源图片
-
Images
构造- Parameters:
srcImage- 来源图片targetImageType- 目标图片类型,null则读取来源图片类型
-
-
Method Details
-
from
从Path读取图片并开始处理- Parameters:
imagePath- 图片文件路径- Returns:
- Img
-
from
从文件读取图片并开始处理- Parameters:
imageFile- 图片文件- Returns:
- Img
-
from
从资源对象中读取图片并开始处理- Parameters:
resource- 图片资源对象- Returns:
- Img
-
from
从流读取图片并开始处理- Parameters:
in- 图片流- Returns:
- Img
-
from
从ImageInputStream取图片并开始处理- Parameters:
imageStream- 图片流- Returns:
- Img
-
from
从URL取图片并开始处理- Parameters:
imageUrl- 图片URL- Returns:
- Img
-
from
从Image取图片并开始处理- Parameters:
image- 图片- Returns:
- Img
-
setTargetImageType
设置目标图片文件格式,用于写出- Parameters:
imgType- 图片格式- Returns:
- this
- See Also:
-
setPositionBaseCentre
计算x,y坐标的时候是否从中心做为原始坐标开始计算- Parameters:
positionBaseCentre- 是否从中心做为原始坐标开始计算- Returns:
- this
-
setQuality
设置图片输出质量,数字为0~1(不包括0和1)表示质量压缩比,除此数字外设置表示不压缩- Parameters:
quality- 质量,数字为0~1(不包括0和1)表示质量压缩比,除此数字外设置表示不压缩- Returns:
- this
-
setQuality
设置图片输出质量,数字为0~1(不包括0和1)表示质量压缩比,除此数字外设置表示不压缩- Parameters:
quality- 质量,数字为0~1(不包括0和1)表示质量压缩比,除此数字外设置表示不压缩- Returns:
- this
-
setBackgroundColor
设置图片的背景色- Parameters:
backgroundColor-Ansi4BitColor背景色- Returns:
- this
-
scale
缩放图像(按比例缩放)- Parameters:
scale- 缩放比例。比例大于1时为放大,小于1大于0为缩小- Returns:
- this
-
scale
缩放图像(按长宽缩放) 注意:目标长宽与原图不成比例会变形- Parameters:
width- 目标宽度height- 目标高度- Returns:
- this
-
scale
缩放图像(按长宽缩放) 注意:目标长宽与原图不成比例会变形- Parameters:
width- 目标宽度height- 目标高度scaleType- 缩放类型,可选Image.SCALE_SMOOTH平滑模式或Image.SCALE_DEFAULT默认模式- Returns:
- this
-
scale
等比缩放图像,此方法按照按照给定的长宽等比缩放图片,按照长宽缩放比最多的一边等比缩放,空白部分填充背景色 缩放后默认为jpeg格式- Parameters:
width- 缩放后的宽度height- 缩放后的高度fixedColor- 比例不对时补充的颜色,不补充为null- Returns:
- this
-
cut
图像切割(按指定起点坐标和宽高切割)- Parameters:
rectangle- 矩形对象,表示矩形区域的x,y,width,height- Returns:
- this
-
cut
图像切割为圆形(按指定起点坐标和半径切割),填充满整个图片(直径取长宽最小值)- Parameters:
x- 原图的x坐标起始位置y- 原图的y坐标起始位置- Returns:
- this
-
cut
图像切割为圆形(按指定起点坐标和半径切割)- Parameters:
x- 原图的x坐标起始位置y- 原图的y坐标起始位置radius- 半径,小于0表示填充满整个图片(直径取长宽最小值)- Returns:
- this
-
round
图片圆角处理- Parameters:
arc- 圆角弧度,0~1,为长宽占比- Returns:
- this
-
gray
彩色转为灰度- Returns:
- this
-
binary
彩色转为黑白二值化图片- Returns:
- this
-
pressText
给图片添加文字水印 此方法只在给定位置写出一个水印字符串- Parameters:
pressText- 水印文字color- 水印的字体颜色font-Font字体相关信息x- 修正值。 默认在中间,偏移量相对于中间偏移y- 修正值。 默认在中间,偏移量相对于中间偏移alpha- 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字- Returns:
- 处理后的图像
-
pressText
给图片添加文字水印 此方法只在给定位置写出一个水印字符串- 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
给图片添加图片水印- Parameters:
pressImg- 水印图片,可以使用ImageIO.read(File)方法读取文件x- 修正值。 默认在中间,偏移量相对于中间偏移y- 修正值。 默认在中间,偏移量相对于中间偏移alpha- 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字- Returns:
- this
-
pressImage
给图片添加图片水印 如果源图片对象为BufferedImage,则绘制在源图片上,否则创建新的图片- Parameters:
pressImg- 水印图片,可以使用ImageIO.read(File)方法读取文件rectangle- 矩形对象,表示矩形区域的x,y,width,height,x,y从背景图片中心计算alpha- 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字- Returns:
- this
-
pressImageFull
给图片添加全屏图片水印- Parameters:
pressImage- 水印图片lineHeight- 行高degree- 旋转角度,(单位:弧度),以圆点(0,0)为圆心,正代表顺时针,负代表逆时针alpha- 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字- Returns:
- this imashimaro
-
rotate
旋转图片为指定角度 来自:http://blog.51cto.com/cping1982/130066- Parameters:
degree- 旋转角度- Returns:
- 旋转后的图片
-
flip
水平翻转图像- Returns:
- this
-
stroke
描边,此方法为向内描边,会覆盖图片相应的位置- Parameters:
color- 描边颜色,默认黑色width- 边框粗细- Returns:
- this
-
stroke
描边,此方法为向内描边,会覆盖图片相应的位置- Parameters:
color- 描边颜色,默认黑色stroke- 描边属性,包括粗细、线条类型等,见BasicStroke- Returns:
- this
-
getImg
获取处理过的图片- Returns:
- 处理过的图片
-
write
写出图像为结果设置格式 结果类型设定见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
写出图像为目标文件扩展名对应的格式- Parameters:
destFile- 目标文件- Returns:
- this
- Throws:
org.miaixz.bus.core.lang.exception.InternalException- IO异常
-
flush
public void flush()
-