java 图片上绘制文字(综合) Graphics2D

图片上绘制文字, 设置颜色, 字体尺寸大小, 位置等
注: 文字框对应坐标点默认是以左下顶点为坐标原点


import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.output.ByteArrayOutputStream;

import javax.imageio.ImageIO;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.GlyphVector;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;

@Slf4j
public class PrintImage {

    private static final String FONTNAME = "思源黑体";
    private Font font = new Font(FONTNAME, Font.PLAIN, 25); // 添加字体的属性设置
    private Graphics2D g = null;

    /**
     * 设定文字的字体等
     */
    public void setFont(Font font) {
        this.font = font;
    }

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

    /**
     * 导入网络图片到缓冲区
     */
    public BufferedImage loadImageUrl(String imgName) {
        try {
            URL url = new URL(imgName);
            return ImageIO.read(url);
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        return null;
    }

    /**
     * 生成新图片到本地
     */
    public void writeImageLocal(String newImage, BufferedImage img) {
        if (newImage != null && img != null) {
            try {
                File outputfile = new File(newImage);
                ImageIO.write(img, "jpg", outputfile);
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        }
    }
    /**
     * 批量生成图片并返回图片流,用于上传到远程服务器
     * @param imgUrl
     * @param list
     * @return
     * @throws IOException
     */
    private ByteArrayInputStream writeImageStrongest(String imgUrl, List<ModifyImageDto> list) throws IOException {
        BufferedImage bufferedImage = this.printImage(imgUrl, list);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "jpg", output);
        return new ByteArrayInputStream(output.toByteArray());
    }
    /**
     * 修改图片,返回修改后的图片缓冲区(只输出一行文本)
     */
    public BufferedImage modifyImage(BufferedImage img, Object content, int x, int y,Color color) {
        try {
            int w = img.getWidth();
            int h = img.getHeight();
            g = img.createGraphics();
            g.setBackground(Color.BLUE);
            //g.setColor(new Color(120, 120, 110));//设置字体颜色
            g.setColor(color);//设置字体颜色
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
            g.setStroke(new BasicStroke(3));
            if (this.font != null)
                g.setFont(this.font);
            if (content != null) {
                g.translate(w / 2, h / 2);
                //g.rotate(8 * Math.PI / 180);
                g.drawString(content.toString(), x, y);
            }
            g.dispose();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return img;
    }
    /**
     * 修改图片,返回修改后的图片缓冲区(只输出一行文本)
     * @param img
     * @return
     */
    public BufferedImage modifyImageYe(BufferedImage img) {
        try {
            int w = img.getWidth();
            int h = img.getHeight();
            g = img.createGraphics();
            g.setBackground(Color.WHITE);
            g.setColor(Color.blue);//设置字体颜色
            if (this.font != null)
                g.setFont(this.font);
            g.drawString("www.hi.baidu.com?xia_mingjian", w - 85, h - 5);
            g.dispose();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return img;
    }

    public BufferedImage modifyImagetogeter(BufferedImage b, BufferedImage d) {
        try {
            int w = b.getWidth();
            int h = b.getHeight();
            g = d.createGraphics();
            g.drawImage(b, 100, 10, w, h, null);
            g.dispose();
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        return d;
    }
    /***
     * 插入描边的字体
     * @param img
     * @param content
     * @param w
     * @param h
     * @return
     */
    public BufferedImage modifyShapImg(BufferedImage img, String content, int w, int h) {
        //int w = img.getWidth();
        //int h = img.getHeight();
        g = img.createGraphics();
        //Font f = new Font("Courier New", Font.BOLD, 140);
        GlyphVector v = font.createGlyphVector(g.getFontMetrics(font).getFontRenderContext(), content);
        Shape shape = v.getOutline();
        if (content != null) {
            g.translate(w, h);
            //g.rotate(8 * Math.PI / 180);
            //g.drawString(content.toString(), x, y);
        }
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
        g.setColor(new Color(0,90,160));
        g.fill(shape);
        g.setColor(new Color(248,248,255));
        g.setStroke(new BasicStroke(2));
        g.draw(shape);
        return img;
    }
    /**
     * 图片更新多处文字
     * @param d
     * @param list
     */
    public BufferedImage batchModifyImage(BufferedImage img, List<ModifyImageDto> list) {
        try {
            Graphics2D g = img.createGraphics();
            for (ModifyImageDto modifyImageDto : list) {
                g.setColor(toColorFromString(modifyImageDto.getColour()));//设置字体颜色
                if (modifyImageDto.getCharacterSize() != null && modifyImageDto.getCharacterSize() > 0){
                    g.setFont(new Font(FONTNAME, Font.PLAIN, modifyImageDto.getCharacterSize()));
                }else{
                    g.setFont(this.font);
                }
                if (modifyImageDto.getContent() != null) {
                    g.translate(0, 0);
                    g.drawString(modifyImageDto.getContent(), modifyImageDto.getAxisX(), modifyImageDto.getAxisY());
                }
            }
            g.dispose();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return img;
    }
    /**
     * 批量处理图片
     * @param imgUrl
     * @param list
     * @return
     */
    public BufferedImage printImage(String imgUrl, List<ModifyImageDto> list){
        PrintImage tt = new PrintImage();
        BufferedImage bufferedImage = tt.loadImageUrl(imgUrl);
        tt.batchModifyImage(bufferedImage, list);
        return tt.batchModifyImage(bufferedImage, list);
    }

    /**
     * 16进制字符串转换成Color对象
     * @param colorStr 16进制颜色字符串
     * @return Color对象
     * */
    public static Color toColorFromString(String colorStr){
        if (colorStr.length() == 4){//兼容#000,#FFF等
            colorStr += colorStr.substring(1);
        }
        return new Color(
                Integer.valueOf( colorStr.substring( 1, 3 ), 16 ),
                Integer.valueOf( colorStr.substring( 3, 5 ), 16 ),
                Integer.valueOf( colorStr.substring( 5, 7 ), 16 ));
    }

    /**
     * <p>绘制图片对象</p>
     *
     * @author lcx
     * @since 2021/1/18 15:08
     **/
    @Data
    public class ModifyImageDto {
        /**
         * 文字内容
         */
        private String content;

        /**
         * 字体大小
         */
        private Integer characterSize;

        /**
         * 颜色(默认值:)
         */
        private String colour;

        /**
         * 横坐标
         */
        private Integer axisX;

        /**
         * 纵坐标
         */
        private Integer axisY;
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Graphics2DJava 2D API 中最常用的类之一。它提供了许多方法来绘制各种 2D 图形,如线条、矩形、椭圆和文本等。下面是 Graphics2D 绘制的基本步骤: 1. 创建一个 Graphics2D 对象。 ``` Graphics2D g2d = (Graphics2D) g; ``` 2. 设置绘制属性,如颜色、线条宽度等。 ``` g2d.setColor(Color.RED); g2d.setStroke(new BasicStroke(3)); ``` 3. 绘制图形。 ``` g2d.drawLine(x1, y1, x2, y2); g2d.drawRect(x, y, width, height); g2d.drawOval(x, y, width, height); g2d.drawString(text, x, y); ``` 4. 清除绘制对象。 ``` g2d.dispose(); ``` 完整的代码示例: ``` import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.BasicStroke; import javax.swing.JFrame; import javax.swing.JPanel; public class Graphics2DDemo extends JPanel { @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.RED); g2d.setStroke(new BasicStroke(3)); g2d.drawLine(10, 10, 100, 100); g2d.drawRect(50, 50, 100, 100); g2d.drawOval(200, 50, 100, 100); g2d.drawString("Hello, World!", 150, 150); g2d.dispose(); } public static void main(String[] args) { JFrame frame = new JFrame("Graphics2D Demo"); frame.add(new Graphics2DDemo()); frame.setSize(400, 300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } ``` 运行代码,可以看到绘制的图形。这只是 Graphics2D 的基础,它还有许多高级用法,如渐变、纹理等。你可以查看 Java 官方文档来了解更多细节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值