海報繪製 - Java 貼圖 - Java 圖片繪製工具類
阿新 • • 發佈:2021-01-13
package tools.io;
import lombok.AllArgsConstructor;
import lombok.Getter;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.Objects;
/**
* @author Created by 譚健 on 2021/1/11. 星期一. 17:43.
* © All Rights Reserved.
*/
public class CustomSketchpadUtils {
/**
* 圖片座標
*/
@Getter
private static class ImagePosition {
private final int x;
private final int y;
private final BufferedImage bufferedImage;
private int width;
private int height;
public ImagePosition(int x, int y, BufferedImage bufferedImage) {
this.x = x;
this.y = y;
this.bufferedImage = bufferedImage;
}
/**
* 設定圖片繪製為原樣繪製
*/
public ImagePosition withDefaultHeightAndWidth() {
this.width = bufferedImage.getWidth();
this.height = bufferedImage.getHeight();
return this;
}
/**
* 繪製圖片為自定義大小繪製
*/
public ImagePosition withResetHeightAndWidth(int width, int height) {
this.width = width;
this.height = height;
return this;
}
}
/**
* 建立一個圖片座標
*
* @param x x
* @param y y
* @param bufferedImage 圖片
* @return
*/
public static ImagePosition createImagePosition(int x, int y, BufferedImage bufferedImage) {
return new ImagePosition(x, y, bufferedImage);
}
/**
* 文字座標
*/
@AllArgsConstructor
@Getter
private static class TextPosition {
private int x;
private final int y;
private final Color color;
private final Font font;
private String text;
/**
* @param max 最大文字數量
* @return
*/
public TextPosition maxLength(int max) {
int length = text.length();
if (max < length) {
this.text = this.text.substring(0, max);
}
return this;
}
public TextPosition center(int canvasWidth) {
this.x = canvasWidth / 2 - this.text.length() * 9;
return this;
}
}
/**
* 建立一個文字座標
*
* @param x x
* @param y y
* @param text 文字
* @param color 顏色
* @param font 字型
* @return
*/
public static TextPosition createTextPosition(int x, int y, Color color, Font font, String text) {
return new TextPosition(x, y, color, font, text);
}
/**
* 讀取圖片到緩衝
*
* @param imgUrl 背景地址
*/
public static BufferedImage readImg(String imgUrl) {
try {
return ImageIO.read(new URL(imgUrl));
} catch (IOException e) {
e.printStackTrace();
}
throw new NullPointerException("圖片讀取失敗");
}
/**
* 建立指定大小的畫布
*
* @param width 寬度
* @param height 高度
* @return
*/
public static BufferedImage createCanvas(int width, int height) {
return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
}
/**
* 建立一個帶背景的畫布(平鋪模式)
*
* @param url 圖片地址
* @return
*/
public static BufferedImage createCanvas(String url) {
BufferedImage background = readImg(url);
BufferedImage canvas = new BufferedImage(background.getWidth(), background.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
drawImage((Graphics2D) canvas.getGraphics(), createImagePosition(0, 0, background).withDefaultHeightAndWidth());
return canvas;
}
/**
* 建立一個帶背景的畫布(圓角平鋪模式)
*
* @param url 圖片地址
* @return
*/
public static BufferedImage createFilletCanvas(String url) {
BufferedImage background = readImg(url);
int width = background.getWidth();
int height = background.getHeight();
// 透明底的圖片
BufferedImage canvas = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
setFillet(background, width, height, canvas);
return canvas;
}
public static void setFillet(BufferedImage background, int width, int height, BufferedImage canvas) {
Graphics2D graphics2D = canvas.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int border = 1;
int resetWidth = width - border * 2;
int resetHeight = height - border * 2;
Ellipse2D.Double shape = new Ellipse2D.Double(border, border, resetWidth, resetHeight);
graphics2D.setClip(shape);
drawImage(graphics2D, createImagePosition(border, border, background).withResetHeightAndWidth(resetWidth, resetHeight));
graphics2D.dispose();
graphics2D = canvas.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
border = 4;
Stroke s = new BasicStroke(1.5F, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
graphics2D.setStroke(s);
graphics2D.setColor(Color.WHITE);
graphics2D.drawOval(border, border, width - border * 2, width - border * 2);
graphics2D.dispose();
}
/**
* 繪製一個文字
*
* @param graphics2D 畫板
* @param text 文字座標
*/
public static void drawString(Graphics2D graphics2D, TextPosition text) {
graphics2D.setColor(text.getColor());
graphics2D.setFont(text.getFont());
graphics2D.drawString(text.getText(), text.getX(), text.getY());
}
/**
* 繪製一個圖片
*
* @param graphics2D 畫板
* @param image 圖片
*/
@SuppressWarnings("all")
public static void drawImage(Graphics2D graphics2D, ImagePosition image) {
BufferedImage bufferedImage = image.getBufferedImage();
if (Objects.isNull(image.getWidth()) || Objects.isNull(image.getHeight())) {
image.withDefaultHeightAndWidth();
}
Image scaledInstance = bufferedImage.getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_DEFAULT);
graphics2D.drawImage(scaledInstance, image.getX(), image.getY(), null);
}
private static InputStream write2InputStream(BufferedImage canvas) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(canvas, "png", os);
} catch (IOException e) {
e.printStackTrace();
}
return new ByteArrayInputStream(os.toByteArray());
}
public static void write2Localhost(BufferedImage canvas, String path) {
try {
ImageIO.write(canvas, "png", new FileOutputStream(path));
} catch (IOException e) {
e.printStackTrace();
}
}
}