JAVA生成数字印章图片

废话少说直接上代码,看不懂复制本地跑一下就完事了。

package cn.xxx.xxx.utils;

import org.apache.commons.codec.binary.Base64;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

public class GraphicsUtil {

    public static void main(String[] args) {
        GraphicsUtil.sealToFile("1000",new File("D:\\Config\\1000.png"));
        GraphicsUtil.sealToFile("1111",new File("D:\\Config\\1111.png"));

        String s = GraphicsUtil.sealToBase64("56");
        System.out.println(s);
    }
    /**
     * 生成数字印章到文件
     * @param text
     * @return
     */
    public static String sealToBase64(String text){
        BufferedImage bi = seal(text);
        try {
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            //bufferedImage转为byte数组
            ImageIO.write(bi, "png", outStream);
            byte[] bytes = outStream.toByteArray();

            return Base64.encodeBase64String(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 生成数字印章到文件
     * @param text
     * @param file
     * @return
     */
    public static File sealToFile(String text,File file){
        BufferedImage bi = seal(text);
        try {
            ImageIO.write(bi, "png", file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    /***
     * 生成数字印章
     * @param text
     * @return
     */
    public  static BufferedImage seal(String text){
        int width = 95;
        int height = 44;
        double rate = 1.5;//文字间距,重要
        if (text.length() > 2){
            rate = 1;
        }
        //1.创建对象并绘制背景
        BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
        //创建画笔
        Graphics2D g2d = bi.createGraphics();
        //背景颜色
        g2d.setColor(Color.WHITE);
        //背景
        g2d.fill3DRect(0, 0, width, height, true);
        //2.绘制边框
        int stroke = 4;
        g2d.setPaint(Color.RED);
        //设置画笔的粗度
        g2d.setStroke(new BasicStroke(stroke));
        g2d.drawRect(stroke/2, stroke/2, width-stroke, height-stroke);
        //3.绘制文件内容
        //文字颜色
        g2d.setColor(Color.RED);
        //设置字体
        g2d.setFont(new Font("宋体",Font.BOLD,40));
        //居中显示
        int x = (int)(width / 2 - rate * g2d.getFontMetrics().stringWidth(text) / 2);
        int y = height / 2 + g2d.getFontMetrics().getHeight() / 3;
        //4.重新设置文字间距
        rate(text, x, y, rate, g2d);
        g2d.dispose();
        return bi;
    }

    /***
     * 设置文字间距
     * @param str
     * @param x
     * @param y
     * @param rate
     * @param g
     */
    private static void rate(String str,int x,int y,double rate,Graphics g){
        String tempStr = null;
        int orgStringWight = g.getFontMetrics().stringWidth(str);
        int orgStringLength = str.length();
        int tempx = x;
        int tempy = y;
        while(str.length()>0){
            tempStr = str.substring(0, 1);
            str = str.substring(1, str.length());
            g.drawString(tempStr, tempx, tempy);
            tempx = (int)(tempx + (double)orgStringWight / (double)orgStringLength * rate);
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,可以看出生成椭圆印章的方法主要有两种:批量生成和椭圆拟合。以下是两种方法的具体实现: 1. 批量生成椭圆印章 可以参考引用中的文章《毕设之opencv批量生BMP【圆】》,将其中的圆生成代码进行修改,改为生成椭圆即可。具体实现步骤如下: - 导入OpenCV库 - 设置生成椭圆的参数,包括长轴、短轴、中心点坐标、旋转角度等 - 循环生成椭圆,并保存为BMP格式的图片 以下是示例代码: ```python import cv2 import numpy as np # 设置生成椭圆的参数 a = 200 # 长轴 b = 100 # 短轴 center = (300, 300) # 中心点坐标 angle = 30 # 旋转角度 # 循环生成椭圆,并保存为BMP格式的图片 for i in range(10): img = np.zeros((600, 600, 3), np.uint8) cv2.ellipse(img, center, (a, b), angle, 0, 360, (255, 255, 255), -1) cv2.imwrite('ellipse{}.bmp'.format(i), img) ``` 2. 椭圆拟合生成椭圆印章 可以参考引用中的文章《毕设之Opencv批量圆拟合》,将其中的圆拟合代码进行修改,改为椭圆拟合即可。具体实现步骤如下: - 导入OpenCV库 - 读取图片并转为灰度图 - 进行边缘检测 - 进行椭圆拟合 - 绘制椭圆并保存图片 以下是示例代码: ```python import cv2 # 读取图片并转为灰度图 img = cv2.imread('test.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 进行边缘检测 edges = cv2.Canny(gray, 50, 150, apertureSize=3) # 进行椭圆拟合 ellipse = cv2.fitEllipse(cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]) # 绘制椭圆并保存图片 cv2.ellipse(img, ellipse, (0, 255, 0), 2) cv2.imwrite('ellipse.jpg', img) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值