Java 自定义文字图片

package com.study.util;


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


public class Test {


    public static void main(String[] args) throws IOException {
        byte[] data = graphicsGeneration("哈哈哈");


        OutputStream outputStream = new FileOutputStream("D:\\test.png");
        outputStream.write(data);
        outputStream.close();
    }


    public static byte[] graphicsGeneration(String name) {
        byte[] b = null;
        int fontSize = 60; // 字体大小
        int imageX = 60; // 字的x轴
        int imageY = 75; // 字的y轴
        int imageWidth = 300;// 图片的宽度
        int imageHeight = 150;// 图片的高度
        Font font = new Font("楷体", Font.CENTER_BASELINE, fontSize);


        BufferedImage result = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D graphics = result.createGraphics();
        graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 0));
        graphics.fillRect(0, 0, imageWidth, imageHeight);// 填充整个屏幕
        graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1));
        graphics.setColor(Color.BLACK);
        graphics.setFont(font);
        graphics.drawString(name, imageX, imageY);
        // 将签名图像放入这个文件夹中去,并调整大小
        graphics.drawImage(result.getScaledInstance(result.getWidth(), result.getHeight(), Image.SCALE_SMOOTH), 0, 0, null);
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(result, "png", out);
            b = out.toByteArray();
            out.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return b;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值