java生成电子印章的方法

本文介绍如何使用Java生成电子印章,包括印章样式定义、文字排列算法等关键步骤,并提供了实用的代码示例。
该文章已生成可运行项目,

趁着午休把代码整理整理然后写了这篇博客(。・∀・)ノ゙。之前更了一篇java使用itext编辑pdf,动态生成pdf文件(从利用Adobe创建pdf模板开始一步步详细介绍)
,想到里面有盖电子印章功能,之前做的时候搜的好多也不完善,所以记录一下给需要的人,你将要踩的坑俺都给你踩过了,前面的几个类的代码都可以直接复制,把utils的代码改一下就可以使用啦。👍
在这里插入图片描述

码云地址: 殷桃小狗子 / SealKit

1.utils里面写的几个生成印章的方法样式展示:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.SealCircle.java实体类

package com.gl.common.core.domain.seal;
import lombok.Builder;
import lombok.Data;
@Builder
@Data
public class SealCircle {
   
   
    private Integer line;
    private Integer width;
    private Integer height;
}

3.SealFont.java实体类

package com.gl.common.core.domain.seal;
import lombok.Builder;
import lombok.Data;
@Builder
@Data
public 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;
    }
}

4.Seal.java

package com.gl.common.core.utils.seal;


import com.gl.common.core.domain.seal.SealCircle;
import com.gl.common.core.domain.seal.SealFont;
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 draw2(pngPath); // 画私章
        }

        //1.画布
        BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_4BYTE_ABGR);

        //2.画笔
        Graphics2D g2d = bi.createGraphics();

        //2.1抗锯齿设置
        //文本不抗锯齿,否则圆中心的文字会被拉长
        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);

        //2.2设置背景透明度
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 0));

        //2.3填充矩形
        g2d.fillRect(0, 0, size, size);

        //2.4重设透明度,开始画图
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1));

        //2.5设置画笔颜色
        g2d.setPaint(color == null ? Color.RED : color);

        //3.画边线圆
        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();

        //4.画内边线圆
        if (borderInnerCircle != null) {
   
   
            int x = INIT_BEGIN + borderCircleWidth - borderInnerCircle.getWidth();
            int y = INIT_BEGIN + borderCircleHeight - borderInnerCircle.getHeight();
            drawCircle(g2d, borderInnerCircle, x, y);
        }

        //5.画内环线圆
        if (innerCircle != null) {
   
   
            int x = INIT_BEGIN + borderCircleWidth - innerCircle.getWidth();
            int y = INIT_BEGIN + borderCircleHeight - innerCircle.getHeight();
            drawCircle(g2d, innerCircle, x, y);
        }

        //6.画弧形主文字
        if (borderCircleHeight != borderCircleWidth) {
   
   
            drawArcFont4Oval(g2d, borderCircle, mainFont, true);
        } else {
   
   
            drawArcFont4Circle(g2d, borderCircleHeight, mainFont, true);
        }

        //7.画弧形副文字
        if (borderCircleHeight != borderCircleWidth) {
   
   
            drawArcFont4Oval(g2d, borderCircle, viceFont, false);
        } else {
   
   
            drawArcFont4Circle(g2d, borderCircleHeight, viceFont, false);
        }

        //8.画中心字
        drawFont(g2d, (borderCircleWidth + INIT_BEGIN) * 2, (borderCircleHeight + INIT_BEGIN) * 2, centerFont);

        //9.画抬头文字
        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;
        }

        //1.字体长度
        int textLen = font.getText().length();

        //2.字体大小,默认根据字体长度动态设定
        int size = font.getSize() == null ? (55 - textLen * 2) : font.getSize();

        //3.字体样式
        int style = font.getBold() ? Font.BOLD : Font.PLAIN;

        //4.构造字体
        Font f = new Font(font.getFamily(), style, size);

        FontRenderContext context = g2d.getFontRenderContext();
        Rectangle2D rectangle = f.getStringBounds(font.getText(), context);

        //5.文字之间间距,默认动态调整
        double space;
        if (font.getSpace() != null) {
   
   
            space = font.getSpace();
        } else {
   
   
            if (textLen == 1) {
   
   
                space = 0;
            } else {
   
   
                space = rectangle.getWidth() / (textLen - 1) * 0.9;
            }
        }

        //6.距离外圈距离
        int margin = font.getMargin() == null ? INIT_BEGIN : font.getMargin();

        //7.写字
        double newRadius = circleRadius + rectangle.getY() - margin;
        double radianPerInterval = 2 * Math.asin(space / (2 *<
本文章已经生成可运行项目
评论 18
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值