在做項目過程中,遇到一個需求,讓我生成一個下面格式的印章下載,我查找資料後,寫出瞭如下的代碼,生成印章。
導出的印章:
代碼塊:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Date;
import javax.imageio.ImageIO;
public class gz1 {
//圖片寬度
private static final int WIDTH = 340;
//圖片高度
private static final int HEIGHT = 340;
private static String message = " 安徽省黃山市屯溪區荷花池小學";
private static String centerName = "期末考試章";
public static void main(String[] args) throws Exception{
BufferedImage image = startGraphics2D();
try {
//下載的路徑
String filePath = "E:\\"+new Date().getTime()+".png";
ImageIO.write(image, "png", new File(filePath));
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static BufferedImage startGraphics2D(){
// 定義圖像buffer:高度,寬度,半透明
BufferedImage buffImg = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
//繪圖
Graphics2D g = buffImg.createGraphics();
g.setColor(Color.RED);
//線條的寬度
g.setStroke(new BasicStroke(5));
//設置鋸齒圓滑
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//繪製圓
//周半徑
int radius = HEIGHT/3;
//畫圖所出位置
int CENTERX = WIDTH/2;
//畫圖所處位置
int CENTERY = HEIGHT/2;
//畫圓
Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(CENTERX, CENTERY, CENTERX + radius, CENTERY + radius);
g.draw(circle);
//繪製中間的五角星
g.setFont(new Font("宋體", Font.BOLD, 80));
g.drawString("★", CENTERX-(80/2), CENTERY+(80/3));
//寫入簽名
g.setFont(new Font("宋體", Font.BOLD, 20));// 寫入簽名
g.drawString(centerName, CENTERX -(47), CENTERY +(30+50));
//根據輸入字符串得到字符數組
String[] messages2 = message.split("",0);
String[] messages = new String[messages2.length-1];
System.arraycopy(messages2,1,messages,0,messages2.length-1);
//輸入的字數
int ilength = messages.length;
//設置message字體屬性
int fontsize = 28;
Font f = new Font("Serif", Font.BOLD, fontsize);
//正確測量文本所需的信息容器
FontRenderContext context = g.getFontRenderContext();
//規定的位置(X,Y)和尺寸(寬x高)定義的字體
Rectangle2D bounds = f.getStringBounds(message, context);
//字符寬度=字符串長度/字符數
double char_interval = (bounds.getWidth() / ilength);
//上坡度
double ascent = -bounds.getY();
int first = 0,second = 0;
boolean odd = false;
if (ilength%2 == 1)
{
first = (ilength-1)/2;
odd = true;
}
else
{
first = (ilength)/2-1;
second = (ilength)/2;
odd = false;
}
double radius2 = radius - ascent;
double x0 = CENTERX;
double y0 = CENTERY - radius + ascent;
//旋轉角度
double a = 2*Math.asin(char_interval/(2*radius2));
if (odd)
{
g.setFont(f);
g.drawString(messages[first], (float)(x0 - char_interval/2), (float)y0);
//中心點的右邊打印
for (int i=first+1;i<ilength;i++)
{
double aa = (i - first) * a;
double ax = radius2 * Math.sin(aa);
double ay = radius2 - radius2 * Math.cos(aa);
AffineTransform transform = AffineTransform.getRotateInstance(aa);
Font f2 = f.deriveFont(transform);
g.setFont(f2);
g.drawString(messages[i], (float)(x0 + ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay - char_interval/2* Math.sin(aa)));
}
//中心點的左邊打印
for (int i=first-1;i>-1;i--)
{
double aa = (first - i) * a;
double ax = radius2 * Math.sin(aa);
double ay = radius2 - radius2 * Math.cos(aa);
AffineTransform transform = AffineTransform.getRotateInstance(-aa);
Font f2 = f.deriveFont(transform);
g.setFont(f2);
g.drawString(messages[i], (float)(x0 - ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay + char_interval/2* Math.sin(aa)));
}
}
else
{
//中心點的右邊打印
for (int i=second;i<ilength;i++)
{
double aa = (i - second + 0.5) * a;
double ax = radius2 * Math.sin(aa);
double ay = radius2 - radius2 * Math.cos(aa);
AffineTransform transform = AffineTransform.getRotateInstance(aa);
Font f2 = f.deriveFont(transform);
g.setFont(f2);
g.drawString(messages[i], (float)(x0 + ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay - char_interval/2* Math.sin(aa)));
}
//中心點的左邊打印
for (int i=first;i>-1;i--)
{
double aa = (first - i + 0.5) * a;
double ax = radius2 * Math.sin(aa);
double ay = radius2 - radius2 * Math.cos(aa);
AffineTransform transform = AffineTransform.getRotateInstance(-aa);
Font f2 = f.deriveFont(transform);
g.setFont(f2);
g.drawString(messages[i], (float)(x0 - ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay + char_interval/2* Math.sin(aa)));
}
}
return buffImg;
}
}