Write text onto image in Java

时光毁灭记忆、已成空白 提交于 2019-12-03 06:53:23

问题


Is there a Java library to write text to images, same as PHP's GD library.


回答1:


Here's is yet another example.




回答2:


Try the below code

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Testing {
    public static void main(String arg[]) throws IOException {
        String key = "Sample";
        BufferedImage bufferedImage = new BufferedImage(170, 30,
                BufferedImage.TYPE_INT_RGB);
        Graphics graphics = bufferedImage.getGraphics();
        graphics.setColor(Color.LIGHT_GRAY);
        graphics.fillRect(0, 0, 200, 50);
        graphics.setColor(Color.BLACK);
        graphics.setFont(new Font("Arial Black", Font.BOLD, 20));
        graphics.drawString(key, 10, 25);
        ImageIO.write(bufferedImage, "jpg", new File(
                "C:/Users/admin/desktop/image.jpg"));
        System.out.println("Image Created");
    }
}



回答3:


Sure. First load the image, probably using a method of ImageIO. Then, using a Graphics object representing the image itself, call the drawString method.




回答4:


Take a look at Graphics2D.drawString




回答5:


yes, java.awt.*

Here's one example; there are hundreds out there.



来源:https://stackoverflow.com/questions/2736320/write-text-onto-image-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!