Java 使用Graphics2D图片上写字,计算宽和高 以及字体的位置

如上图,这个是一个有中文与英文的字符串。 中文与英文字符的宽高是不一样的,如果想要生成一张宽高刚的图片,这样我就需要计算每一个字符的宽度,及合适的高;

java.awt.FontMetrics 这个类对文字的宽高位置有详细的介绍;

 

计算使用字体的一段字符串的宽

public static int getWordWidth(Font font, String content) {
        FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
        int width = 0;
        for (int i = 0; i < content.length(); i++) {
            width += metrics.charWidth(content.charAt(i));
        }
        return width;
}

计算使用字体的最大的高度

   FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
   int height = metrics.getHeight();

在图片是写文字时最合适的位置 

Ascent是从基线到顶部最大的高,也可以当做一个种字体画图时最有可以点用的高度

graphics.drawString(content, 0, metrics.getAscent());

测试代码

import sun.font.FontDesignMetrics;

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

/**
 * Created by zengrenyuan on 18/5/11.
 */
public class ImageTest {

    public static void main(String[] args) throws IOException {
        Font font = new Font("微软雅黑", Font.BOLD, 32);
        String content = "你好Java!";
        FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
        int width = getWordWidth(font, content);//计算图片的宽
        int height = metrics.getHeight();//计算高
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = bufferedImage.createGraphics();
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
        //设置背影为白色
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
        graphics.setFont(font);
        graphics.setColor(Color.BLACK);
        graphics.drawString(content, 0, metrics.getAscent());//图片上写文字
        graphics.dispose();
        write(bufferedImage, "/data/test.png");


    }

    public static int getWordWidth(Font font, String content) {
        FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
        int width = 0;
        for (int i = 0; i < content.length(); i++) {
            width += metrics.charWidth(content.charAt(i));
        }
        return width;
    }

    public static void write(BufferedImage bufferedImage, String target) throws IOException {
        File file = new File(target);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        try (OutputStream os = new FileOutputStream(target)) {
            ImageIO.write(bufferedImage, "PNG", os);
        }
    }

}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用JavaGraphics2D类压缩图片,你可以按照以下步骤: 1. 加载要压缩的图片,可以使用ImageIO类: ```java BufferedImage originalImage = ImageIO.read(new File("path/to/image")); ``` 2. 创建一个新的BufferedImage对象,设置它的度和度为原始图像的一半,或者其他你想要的压缩比例: ```java int originalWidth = originalImage.getWidth(); int originalHeight = originalImage.getHeight(); int newWidth = originalWidth / 2; // 压缩一半 int newHeight = originalHeight / 2; // 压缩一半 BufferedImage compressedImage = new BufferedImage(newWidth, newHeight, originalImage.getType()); ``` 3. 获取Graphics2D对象,用它来绘制压缩后的图像: ```java Graphics2D g2d = compressedImage.createGraphics(); g2d.drawImage(originalImage, 0, 0, newWidth, newHeight, null); g2d.dispose(); ``` 4. 最后,将压缩后的图像保存到文件或输出流中,可以使用ImageIO类: ```java ImageIO.write(compressedImage, "jpg", new File("path/to/compressed/image.jpg")); ``` 完整的代码: ```java import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class ImageCompressor { public static void main(String[] args) throws Exception { BufferedImage originalImage = ImageIO.read(new File("path/to/image")); int originalWidth = originalImage.getWidth(); int originalHeight = originalImage.getHeight(); int newWidth = originalWidth / 2; // 压缩一半 int newHeight = originalHeight / 2; // 压缩一半 BufferedImage compressedImage = new BufferedImage(newWidth, newHeight, originalImage.getType()); Graphics2D g2d = compressedImage.createGraphics(); g2d.drawImage(originalImage, 0, 0, newWidth, newHeight, null); g2d.dispose(); ImageIO.write(compressedImage, "jpg", new File("path/to/compressed/image.jpg")); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xuxizhou1994

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

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

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

打赏作者

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

抵扣说明:

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

余额充值