开发者

Write text onto image in Java

开发者 https://www.devze.com 2022-12-28 19:27 出处:网络
Is there a Java library to writ开发者_JS百科e text to images, same as PHP\'s GD library.Try the below code

Is there a Java library to writ开发者_JS百科e text to images, same as PHP's GD library.


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");
    }
}


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


Take a look at Graphics2D.drawString


yes, java.awt.*

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

0

精彩评论

暂无评论...
验证码 换一张
取 消