好用的图片添加文字工具类

这是一个Java实现的工具类,用于在图片的指定位置添加文字。方法`generateActivityImage`接收原图片路径、目标图片路径和文字内容列表,通过Graphics2D进行绘制,实现了文字的居中显示。程序提供了主函数示例,展示如何使用该工具类生成带有文字的图片。
摘要由CSDN通过智能技术生成

好用的图片上固定位置加文字的工具类

package com.zjtx.util;

import com.zjtx.util.exception.ServiceException;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import java.util.*;

/**
 * 图片固定位置加文字工具类
 * @author WangDeyu
 * */
public class ImageIOUtils {

    /**
     * 生成年底活动用的成绩图片
     * @param oldImgPath (原图片地址)
     * @param newImgPath (填充文字后的图片地址)
     * @param list (填充图片的文字内容)
     * */
    public static void generateActivityImage(String oldImgPath, String newImgPath, List<Map<String,Object>> list) throws Exception{

        ImageIcon imgIcon = new ImageIcon(oldImgPath);
        Image img = imgIcon.getImage();
        int width = img.getWidth(null);
        int height = img.getHeight(null);
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        Graphics2D g = bufferedImage.createGraphics();
        g.drawImage(img, 0, 0, null);
        //设置字体和字体颜色
        Font font = new Font("黑体",Font.BOLD, 33);
        g.setBackground(Color.white);
        g.setPaint(new Color(0,18,252));
        g.setFont(font);
        FontMetrics fm = g.getFontMetrics(font);

        for (Map<String,Object> map : list){
            String name = (String) map.get("name");
            Integer x1 = (Integer) map.get("x1");
            Integer x2 = (Integer) map.get("x2");
            Integer y = (Integer) map.get("y");
            //获取文字长度
            int textWidth = fm.stringWidth(name);
            //计算文字根据横坐标居中
            g.drawString(name,(x1+x2 - textWidth) / 2 , y);
        }
        g.dispose();
        try {
            FileOutputStream out = new FileOutputStream(newImgPath);
            ImageIO.write(bufferedImage, "png", out);
            out.close();
        } catch (FileNotFoundException e) {
            throw new ServiceException("生成图片文字失败:",e);
        }
    }


    public static void main(String[] args) throws Exception{
        //文字内容
        List<String> name = Arrays.asList("疼他爹没错就是我", "骚哦", "666", "怎肥事?", "好气哦", "滑稽果");
        //文字x轴坐标1(因为要根据一个范围居中,所以有两个x坐标)
        List<Integer> x1 = Arrays.asList(162, 270, 140, 465, 238, 534);
        //文字x轴坐标2
        List<Integer> x2 = Arrays.asList(270, 356, 225, 550, 311, 608);
        //文字y轴坐标
        List<Integer> y = Arrays.asList(50, 100, 150, 200, 250, 300);

        List<Map<String, Object>> imgList = new ArrayList<>();
        int size = name.size();
        for (int i = 0; i < size; i++) {
            Map<String, Object> map = new HashMap<>(16);
            map.put("name", name.get(i));
            map.put("x1", x1.get(i));
            map.put("x2", x2.get(i));
            map.put("y", y.get(i));
            imgList.add(map);
        }

        generateActivityImage( "D:\\img\\temp.png", "D:\\img\\new.png", imgList);

    }
}

效果图

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值