Java 创建背景透明的文字图片、将文本写到图片上

Java 创建背景透明的文字图片、将文本写到图片上:

import sun.font.FontDesignMetrics;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Img {

    /**
     * 导入本地图片到缓冲区
     */
    private static BufferedImage loadImageLocal(String imgName) {
        try {
            return ImageIO.read(new File(imgName));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    /**
     * 创建背景透明的文字图片
     *
     * @param str       文本字符串
     * @param width     图片宽度
     * @param height    图片高度
     * @param font      设置字体
     * @param fontColor 字体颜色
     * @param alpha     文字透明度,值从0.0f-1.0f,依次变得不透明
     */
    private static BufferedImage createImageWithText(String str, int width, int height, Font font, Color fontColor, float alpha) {
        BufferedImage textImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = textImage.createGraphics();

        //设置背景透明
        textImage = g2.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
        g2.dispose();
        g2 = textImage.createGraphics();

        //开启文字抗锯齿
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //设置字体
        g2.setFont(font);
        //设置字体颜色
        g2.setColor(fontColor);
        //设置透明度:1.0f为透明度 ,值从0-1.0,依次变得不透明
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        //计算字体位置:上下左右居中
        FontRenderContext context = g2.getFontRenderContext();
        LineMetrics lineMetrics = font.getLineMetrics(str, context);
        FontMetrics fontMetrics = FontDesignMetrics.getMetrics(font);
        float offset = (width - fontMetrics.stringWidth(str)) / 2;
        float y = (height + lineMetrics.getAscent() - lineMetrics.getDescent() - lineMetrics.getLeading()) / 2;
        //绘图
        g2.drawString(str, (int) offset, (int) y);
        //释放资源
        g2.dispose();
        return textImage;
    }

    private static void createImageWithText(String destImgPath, String imgType, String str, int width, int height, Font font, Color fontColor, float alpha) {
        BufferedImage image = Img.createImageWithText(str, width, height, font, fontColor, alpha);
        try {
            ImageIO.write(image, imgType, new File(destImgPath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将文本写到图片上
     *
     * @param srcImgPath  源图片路径
     * @param destImgPath 处理结果图片路径
     * @param imgType     图片格式:png,jpg等
     * @param text        文本字符串
     * @param font        字体
     * @param fontColor   字体颜色:Color.RED等
     */
    private static boolean writeTextOnImage(String srcImgPath, String destImgPath, String imgType, String text, Font font, Color fontColor) {
        BufferedImage image = Img.loadImageLocal(srcImgPath);
        int width = image.getWidth();
        int height = image.getHeight();

        Graphics2D g2 = image.createGraphics();

        //开启文字抗锯齿
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //设置字体
        g2.setFont(font);
        //设置字体颜色
        g2.setColor(fontColor);

        //设置透明度:1.0f为透明度 ,值从0-1.0,依次变得不透明
        //g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));

        //计算字体位置:上下左右居中
        FontRenderContext context = g2.getFontRenderContext();
        LineMetrics lineMetrics = font.getLineMetrics(text, context);
        FontMetrics fontMetrics = FontDesignMetrics.getMetrics(font);
        float offset = (width - fontMetrics.stringWidth(text)) / 2;
        float y = (height + lineMetrics.getAscent() - lineMetrics.getDescent() - lineMetrics.getLeading()) / 2;
        //绘图
        g2.drawString(text, (int) offset, (int) y);
        //释放资源
        g2.dispose();
        //写入文件
        boolean ret = false;
        try {
            ret = ImageIO.write(image, imgType, new File(destImgPath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ret;
    }


    public static void main(String[] args) {
        String imgPath1 = "images/green.png";
        Font font = new Font("fonts/MSYH.TTF", Font.BOLD, 80);

        String destPath = "images/pic.gif";
        Img.createImageWithText(destPath, "gif", "认真的头像", 500, 500, font, Color.BLACK, 1.0f);

        String destPath3 = "images/pic31.png";
        Img.writeTextOnImage(imgPath1, destPath3, "png", "aaAA123", font, Color.RED);
    }
}

效果图:

背景透明的图片
背景透明的图片
将文字写在图片上

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LuckyNiuJY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值