package com.cyk.project.seal;
import lombok.Builder;
import lombok.Data;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
@Builder
@Data
public class Seal {
private static final int INIT_BEGIN = 5;
private Integer size;
private Color color;
private SealFont mainFont;
private SealFont viceFont;
private SealFont titleFont;
private SealFont centerFont;
private SealCircle borderCircle;
private SealCircle borderInnerCircle;
private SealCircle innerCircle;
private Integer borderSquare;
private String stamp;
public boolean draw(String pngPath) throws Exception {
if (borderSquare != null) {
return draw3(pngPath);
}
BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2d = bi.createGraphics();
RenderingHints hints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHints(hints);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 0));
g2d.fillRect(0, 0, size, size);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1));
g2d.setPaint(color == null ? Color.RED : color);
if (borderCircle != null) {
drawCircle(g2d, borderCircle, INIT_BEGIN, INIT_BEGIN);
} else {
throw new Exception("BorderCircle can not null!");
}
int borderCircleWidth = borderCircle.getWidth();
int borderCircleHeight = borderCircle.getHeight();
if (borderInnerCircle != null) {
int x = INIT_BEGIN + borderCircleWidth - borderInnerCircle.getWidth();
int y = INIT_BEGIN + borderCircleHeight - borderInnerCircle.getHeight();
drawCircle(g2d, borderInnerCircle, x, y);
}
if (innerCircle != null) {
int x = INIT_BEGIN + borderCircleWidth - innerCircle.getWidth();
int y = INIT_BEGIN + borderCircleHeight - innerCircle.getHeight();
drawCircle(g2d, innerCircle, x, y);
}
if (borderCircleHeight != borderCircleWidth) {
drawArcFont4Oval(g2d, borderCircle, mainFont, true);
} else {
drawArcFont4Circle(g2d, borderCircleHeight, mainFont, true);
}
if (borderCircleHeight != borderCircleWidth) {
drawArcFont4Oval(g2d, borderCircle, viceFont, false);
} else {
drawArcFont4Circle(g2d, borderCircleHeight, viceFont, false);
}
drawFont(g2d, (borderCircleWidth + INIT_BEGIN) * 2, (borderCircleHeight + INIT_BEGIN) * 2, centerFont);
drawFont(g2d, (borderCircleWidth + INIT_BEGIN) * 2, (borderCircleHeight + INIT_BEGIN) * 2, titleFont);
g2d.dispose();
return ImageIO.write(bi, "PNG", new File(pngPath));
}
private static void drawArcFont4Circle(Graphics2D g2d, int circleRadius, SealFont font, boolean isTop) {
if (font == null) {
return;
}
int textLen = font.getText().length();
int size = font.getSize() == null ? (55 - textLen * 2) : font.getSize();
int style = font.getBold() ? Font.BOLD : Font.PLAIN;
Font f = new Font(font.getFamily(), style, size);
FontRenderContext context = g2d.getFontRenderContext();
Rectangle2D rectangle = f.getStringBounds(font.getText(), context);
double space;
if (font.getSpace() != null) {
space = font.getSpace();
} else {
if (textLen == 1) {
space = 0;
} else {
space = rectangle.getWidth() / (textLen - 1) * 0.9;
}
}
int margin = font.getMargin() == null ? INIT_BEGIN : font.getMargin();
double newRadius = circleRadius + rectangle.getY() - margin;
double radianPerInterval = 2 * Math.asin(space / (2 * newRadius));
double fix = 0.04;
if (isTop) {
fix = 0.18;
}
double firstAngle;
if (!isTop) {
if (textLen % 2 == 1) {
firstAngle = Math.PI + Math.PI / 2 - (textLen - 1) * radianPerInterval / 2.0 - fix;
} else {
firstAngle = Math.PI + Math.PI / 2 - ((textLen / 2.0 - 0.5) * radianPerInterval) - fix;
}
} else {
if (textLen % 2 == 1) {
firstAngle = (textLen - 1) * radianPerInterval / 2.0 + Math.PI / 2 + fix;
} else {
firstAngle = (textLen / 2.0 - 0.5) * radianPerInterval + Math.PI / 2 + fix;
}
}
for (int i = 0; i < textLen; i++) {
double theta;
double thetaX;
double thetaY;
if (!isTop) {
theta = firstAngle + i * radianPerInterval;
thetaX = newRadius * Math.sin(Math.PI / 2 - theta);
thetaY = newRadius * Math.cos(theta - Math.PI / 2);
} else {
theta = firstAngle - i * radianPerInterval;
thetaX = newRadius * Math.sin(Math.PI / 2 - theta);
thetaY = newRadius * Math.cos(theta - Math.PI / 2);
}
AffineTransform transform;
if (!isTop) {
transform = AffineTransform.getRotateInstance(Math.PI + Math.PI / 2 - theta);
} else {
transform = AffineTransform.getRotateInstance(Math.PI / 2 - theta + Math.toRadians(8));
}
Font f2 = f.deriveFont(transform);
g2d.setFont(f2);
g2d.drawString(font.getText().substring(i, i + 1), (float) (circleRadius + thetaX + INIT_BEGIN), (float) (circleRadius - thetaY + INIT_BEGIN));
}
}
private static void drawArcFont4Oval(Graphics2D g2d, SealCircle sealCircle, SealFont font, boolean isTop) {
if (font == null) {
return;
}
float radiusX = sealCircle.getWidth();
float radiusY = sealCircle.getHeight();
float radiusWidth = radiusX + sealCircle.getLine();
float radiusHeight = radiusY + sealCircle.getLine();
int textLen = font.getText().length();
int size = font.getSize() == null ? 25 + (10 - textLen) / 2 : font.getSize();
int style = font.getBold() ? Font.BOLD : Font.PLAIN;
Font f = new Font(font.getFamily(), style, size);
double totalArcAng = font.getSpace() * textLen;
float minRat = 0.90f;
double startAngle = isTop ? -90f - totalArcAng / 2f : 90f - totalArcAng / 2f;
double step = 0.5;
int alCount = (int) Math.ceil(totalArcAng / step) + 1;
double[] angleArr = new double[alCount];
double[] arcLenArr = new double[alCount];
int num = 0;
double accArcLen = 0.0;
angleArr[num] = startAngle;
arcLenArr[num] = accArcLen;
num++;
double angR = startAngle * Math.PI / 180.0;
double lastX = radiusX * Math.cos(angR) + radiusWidth;
double lastY = radiusY * Math.sin(angR) + radiusHeight;
for (double i = startAngle + step; num < alCount; i += step) {
angR = i * Math.PI / 180.0;
double x = radiusX * Math.cos(angR) + radiusWidth, y = radiusY * Math.sin(angR) + radiusHeight;
accArcLen += Math.sqrt((lastX - x) * (lastX - x) + (lastY - y) * (lastY - y));
angleArr[num] = i;
arcLenArr[num] = accArcLen;
lastX = x;
lastY = y;
num++;
}
double arcPer = accArcLen / textLen;
for (int i = 0; i < textLen; i++) {
double arcL = i * arcPer + arcPer / 2.0;
double ang = 0.0;
for (int p = 0; p < arcLenArr.length - 1; p++) {
if (arcLenArr[p] <= arcL && arcL <= arcLenArr[p + 1]) {
ang = (arcL >= ((arcLenArr[p] + arcLenArr[p + 1]) / 2.0)) ? angleArr[p + 1] : angleArr[p];
break;
}
}
angR = (ang * Math.PI / 180f);
Float x = radiusX * (float) Math.cos(angR) + radiusWidth;
Float y = radiusY * (float) Math.sin(angR) + radiusHeight;
double qxang = Math.atan2(radiusY * Math.cos(angR), -radiusX * Math.sin(angR));
double fxang = qxang + Math.PI / 2.0;
int subIndex = isTop ? i : textLen - 1 - i;
String c = font.getText().substring(subIndex, subIndex + 1);
FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(f);
int w = fm.stringWidth(c), h = fm.getHeight();
if (isTop) {
x += h * minRat * (float) Math.cos(fxang);
y += h * minRat * (float) Math.sin(fxang);
x += -w / 2f * (float) Math.cos(qxang);
y += -w / 2f * (float) Math.sin(qxang);
} else {
x += (h * minRat) * (float) Math.cos(fxang);
y += (h * minRat) * (float) Math.sin(fxang);
x += w / 2f * (float) Math.cos(qxang);
y += w / 2f * (float) Math.sin(qxang);
}
AffineTransform affineTransform = new AffineTransform();
affineTransform.scale(0.8, 1);
if (isTop)
affineTransform.rotate(Math.toRadians((fxang * 180.0 / Math.PI - 90)), 0, 0);
else
affineTransform.rotate(Math.toRadians((fxang * 180.0 / Math.PI + 180 - 90)), 0, 0);
Font f2 = f.deriveFont(affineTransform);
g2d.setFont(f2);
g2d.drawString(c, x.intValue() + INIT_BEGIN, y.intValue() + INIT_BEGIN);
}
}
private static void drawFont(Graphics2D g2d, int circleWidth, int circleHeight, SealFont font) {
if (font == null) {
return;
}
int textLen = font.getText().length();
int size = font.getSize() == null ? (55 - textLen * 2) : font.getSize();
int style = font.getBold() ? Font.BOLD : Font.PLAIN;
Font f = new Font(font.getFamily(), style, size);
g2d.setFont(f);
FontRenderContext context = g2d.getFontRenderContext();
String[] fontTexts = font.getText().split("\n");
if (fontTexts.length > 1) {
int y = 0;
for (String fontText : fontTexts) {
y += Math.abs(f.getStringBounds(fontText, context).getHeight());
}
float margin = INIT_BEGIN + (float) (circleHeight / 2 - y / 2);
for (String fontText : fontTexts) {
Rectangle2D rectangle2D = f.getStringBounds(fontText, context);
g2d.drawString(fontText, (float) (circleWidth / 2 - rectangle2D.getCenterX()), margin);
margin += Math.abs(rectangle2D.getHeight());
}
} else {
Rectangle2D rectangle2D = f.getStringBounds(font.getText(), context);
float margin = font.getMargin() == null ?
(float) (circleHeight / 2 - rectangle2D.getCenterY()) :
(float) (circleHeight / 2 - rectangle2D.getCenterY()) + (float) font.getMargin();
g2d.drawString(font.getText(), (float) (circleWidth / 2 - rectangle2D.getCenterX()), margin);
}
}
private static void drawCircle(Graphics2D g2d, SealCircle circle, int x, int y) {
if (circle == null) {
return;
}
int lineSize = circle.getLine() == null ? circle.getHeight() * 2 / (35) : circle.getLine();
g2d.setStroke(new BasicStroke(lineSize));
g2d.drawOval(x, y, circle.getWidth() * 2, circle.getHeight() * 2);
}
public boolean draw3(String pngPath) throws Exception {
if (mainFont == null) {
throw new IllegalArgumentException("请输入文字");
}
int fixH = 2;
int fixW = 2;
BufferedImage bi = new BufferedImage(size, size / 2, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2d = bi.createGraphics();
g2d.setPaint(Color.RED);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
bi = drawFont(bi, mainFont, borderSquare, size, fixH, fixW);
g2d.dispose();
return ImageIO.write(bi, "PNG", new File(pngPath));
}
private static BufferedImage drawFont(BufferedImage bi, SealFont font, int lineSize, int imageSize, int fixH, int fixW) {
int marginW = fixW + lineSize;
BufferedImage nbi = new BufferedImage(imageSize, imageSize/10, bi.getType());
Graphics2D ng2d = nbi.createGraphics();
ng2d.setPaint(Color.BLUE);
ng2d.drawImage(bi, 0, 0, imageSize, imageSize/10, null);
ng2d.setStroke(new BasicStroke(lineSize));
ng2d.drawRect(0, 0, imageSize, imageSize/10);
ng2d.dispose();
bi = nbi;
Graphics2D g2d = bi.createGraphics();
g2d.setPaint(Color.BLUE);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
FontRenderContext context = g2d.getFontRenderContext();
Font f = new Font(font.getFamily(), Font.BOLD, font.getSize());
g2d.setFont(f);
Rectangle2D rectangle = f.getStringBounds(font.getText().substring(0, 1), context);
float marginH = (float) (Math.abs(rectangle.getCenterY()) * 2 + marginW) + fixH;
g2d.drawString(font.getText(), marginW, marginH);
return bi;
}
}
@Data
@Builder
class SealFont {
private String text;
private String family;
private Integer size;
private Boolean bold;
private Double space;
private Integer margin;
public String getFamily() {
return family == null ? "宋体" : family;
}
public boolean getBold() {
return bold == null ? true : bold;
}
public SealFont append(String text) {
this.text += text;
return this;
}
}
@Data
@Builder
class SealCircle {
private Integer line;
private Integer width;
private Integer height;
}
```java
package com.cyk.project.seal;
import java.io.File;
public class SealUtils {
public static void OfficialSeal_1(String path,String orgName) throws Exception {
File file = new File(path);
if (!file.exists()) {
file.getParentFile().mkdirs();
}
Seal.builder().size(200).borderCircle(SealCircle.builder().line(4).width(95).height(95).build())
.mainFont(SealFont.builder().text(orgName).size(22).space(22.0).margin(4).build())
.centerFont(SealFont.builder().text("★").size(60).build())
.titleFont(SealFont.builder().text("学时证明").size(16).space(8.0).margin(54).build()).build().draw(path);
}
public static void OfficialSeal_2() throws Exception {
Seal.builder().size(300).borderCircle(SealCircle.builder().line(5).width(140).height(140).build())
.mainFont(SealFont.builder().text("中国四大天王股份有限公司").size(35).space(35.0).margin(10).build())
.centerFont(SealFont.builder().text("★").size(100).build())
.titleFont(SealFont.builder().text("电子签章").size(22).space(10.0).margin(68).build()).build()
.draw("img/公章2.png");
}
public static void OfficialSeal_3() throws Exception {
Seal.builder()
.size(300)
.borderCircle(SealCircle.builder().line(3).width(144).height(100).build())
.borderInnerCircle(SealCircle.builder().line(1).width(140).height(96).build())
.mainFont(SealFont.builder().text("中国四大天王股份有限公司").size(25).space(12.0).margin(10).build())
.centerFont(SealFont.builder().text("NO.5201314").size(20).build())
.titleFont(SealFont.builder().text("电子合同专用章").size(20).space(9.0).margin(64).build())
.build()
.draw("img/公章3.png");
}
public static void PrivateSeal_1() throws Exception {
Seal.builder()
.size(300)
.borderSquare(16)
.mainFont(SealFont.builder().text("刘德华").size(120).build())
.build()
.draw("img/私章1.png");
}
public static void PrivateSeal_2() throws Exception {
Seal.builder()
.size(300)
.borderSquare(16)
.mainFont(SealFont.builder().text("刘德华印").size(120).build())
.build()
.draw("img/私章2.png");
}
public static void PrivateSeal_3() throws Exception {
Seal.builder()
.size(200)
.borderSquare(4)
.mainFont(SealFont.builder().text("经理审核一致").size(10).build())
.build()
.draw("D:/agreement.png");
}
public static void main(String[] args) throws Exception {
PrivateSeal_3();
}
}