栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA生成数字印章图片

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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);
    }
    
    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 "";
    }

    
    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;
    }

    
    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;
    }

    
    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);
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/271535.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号