如何使用 Java 加载图像并向其写入文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6540376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:16:39  来源:igfitidea点击:

How can I load an image and write text to it using Java?

javaimagetextmergebytearray

提问by mohamede1945

I've an image located at images/image.png in my java project. I want to write a method its signature is as follow

我的 java 项目中有一个位于 images/image.png 的图像。我想写一个方法,它的签名如下

byte[] mergeImageAndText(String imageFilePath, String text, Point textPosition);

byte[] mergeImageAndText(String imageFilePath, String text, Point textPosition);

This method will load the image located at imageFilePathand at position textPositionof the image (left upper) I want to write the text, then I want to return a byte[] that represents the new image merged with text.

此方法将加载位于我想写的图像(左上)的imageFilePath位置和位置textPosition的图像text,然后我想返回一个字节 [] 表示与文本合并的新图像。

回答by camickr

Use ImageIOto read the image into a BufferedImage.

用于ImageIO将图像读入BufferedImage.

Use the getGraphics()method of BufferedImageto get the Graphics object.

使用 的getGraphics()方法BufferedImage获取Graphics 对象。

Then you can use the drawString()method of the Graphics object.

然后就可以使用drawString()Graphics对象的方法了。

You can use ImageIOto save the image.

您可以使用ImageIO来保存图像。

回答by Rekin

Try this way:

试试这个方法:

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;

public class ImagingTest {

    public static void main(String[] args) throws IOException {
        String url = "http://icomix.eu/gr/images/non-batman-t-shirt-gross.jpg";
        String text = "Hello Java Imaging!";
        byte[] b = mergeImageAndText(url, text, new Point(200, 200));
        FileOutputStream fos = new FileOutputStream("so2.png");
        fos.write(b);
        fos.close();
    }

    public static byte[] mergeImageAndText(String imageFilePath,
            String text, Point textPosition) throws IOException {
        BufferedImage im = ImageIO.read(new URL(imageFilePath));
        Graphics2D g2 = im.createGraphics();
        g2.drawString(text, textPosition.x, textPosition.y);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(im, "png", baos);
        return baos.toByteArray();
    }
}

回答by Andreas Holstenson

I'm just going to point you in the general direction of image manipulation in Java.

我只是要向您指出 Java 中图像处理的一般方向。

To load images you can use ImageIO. You can also use ImageIO to output images to different formats.

要加载图像,您可以使用ImageIO。您还可以使用 ImageIO 将图像输出为不同的格式。

The easiest way to create an image is to use BufferedImageand then paint on it via Graphics2D. You can use Graphics2D to paint your loaded image and then paint your text on top of it.

创建图像的最简单方法是使用BufferedImage然后通过Graphics2D在其上绘画。您可以使用 Graphics2D 绘制加载的图像,然后在其上绘制文本。

When you're done you use ImageIO to output the image in a suitable format (PNG, JPG, etc).

完成后,您可以使用 ImageIO 以合适的格式(PNG、JPG 等)输出图像。