com.siashan.toolkit.image.element.ImageElement Maven / Gradle / Ivy
package com.siashan.toolkit.image.element;
import com.siashan.toolkit.image.enums.ZoomMode;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.net.URL;
/**
* 图片元素
*
* @author siashan
* @date 2021/12/24
* @since 1.0.8
*/
public class ImageElement extends AbstractElement {
/**
* 图片对象
*/
private BufferedImage image;
/**
* 图片地址
*/
private String imgUrl;
/**
* 绘制宽度
*/
private Integer width;
/**
* 绘制高度
*/
private Integer height;
/**
* 圆角大小
*/
private Integer roundCorner;
/**
* 缩放模式
*/
private ZoomMode zoomMode;
/**
* 旋转角度
*/
private Integer rotate;
/**
* 高斯模糊(毛玻璃)
*/
private Integer blur;
/**
* 构造方法
*
* @param imgUrl 图片url
* @param x x坐标
* @param y y坐标
*/
public ImageElement(String imgUrl, int x, int y) throws Exception {
this.imgUrl = imgUrl;
//事先获得宽高,后面计算要用
this.width = getImage().getWidth();
this.height = getImage().getHeight();
this.zoomMode = ZoomMode.Origin;
super.setX(x);
super.setY(y);
}
/**
* 构造方法
*
* @param image 图片对象
* @param x x坐标
* @param y y坐标
*/
public ImageElement(BufferedImage image, int x, int y) {
this.image = image;
this.width = image.getWidth();
this.height = image.getHeight();
this.zoomMode = ZoomMode.Origin;
super.setX(x);
super.setY(y);
}
/**
* 构造方法
*
* @param imgUrl 图片url
* @param x x坐标
* @param y y坐标
* @param width 宽度
* @param height 高度
* @param zoomMode 缩放模式
*/
public ImageElement(String imgUrl, int x, int y, int width, int height, ZoomMode zoomMode) {
this.imgUrl = imgUrl;
this.width = width;
this.height = height;
this.zoomMode = zoomMode;
super.setX(x);
super.setY(y);
}
/**
* 构造方法
*
* @param image 图片对象
* @param x x坐标
* @param y y坐标
* @param width 宽度
* @param height 高度
* @param zoomMode 缩放模式
*/
public ImageElement(BufferedImage image, int x, int y, int width, int height, ZoomMode zoomMode) {
this.image = image;
this.width = width;
this.height = height;
this.zoomMode = zoomMode;
super.setX(x);
super.setY(y);
}
/**
* 获取图片对象
*
* @return 图片对象
* @throws Exception
*/
public BufferedImage getImage() throws Exception {
if (this.image == null) {
try {
this.image = ImageIO.read(new URL(this.imgUrl));
} catch (Exception e) {
throw e;
}
}
return image;
}
public ImageElement setImage(BufferedImage image) {
this.image = image;
return this;
}
public String getImgUrl() {
return imgUrl;
}
public ImageElement setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
return this;
}
public Integer getWidth() {
return width;
}
public ImageElement setWidth(Integer width) {
this.width = width;
return this;
}
public Integer getHeight() {
return height;
}
public ImageElement setHeight(Integer height) {
this.height = height;
return this;
}
public Integer getRoundCorner() {
return roundCorner;
}
public ImageElement setRoundCorner(Integer roundCorner) {
this.roundCorner = roundCorner;
return this;
}
public ZoomMode getZoomMode() {
return zoomMode;
}
public ImageElement setZoomMode(ZoomMode zoomMode) {
this.zoomMode = zoomMode;
return this;
}
public Integer getRotate() {
return rotate;
}
public ImageElement setRotate(Integer rotate) {
this.rotate = rotate;
return this;
}
public Integer getBlur() {
return blur;
}
public void setBlur(Integer blur) {
this.blur = blur;
}
}