将重复的文本水印添加到代号为 One 的图像

Add a repeated text watermark to an image with Codename One

是否可以使用代号一 API 将重复文本作为水印添加到图像中?

例如,给定一张图片和一个或多个单词的文本,我想创建一个这样的新图片:

当然可以使用可变图像:

Image watered = Image.create(sourceImg.getWidth(), sourceImg.getHeight());
Graphics g = watered.getGraphics();
g.drawImage(sourceImg, 0, 0);
g.setAlpha(30);
g.setColor(0xcccccc);
g.rotate(Math.PI / 2, sourceImg.getWidth() / 2, sourceImg.getHeight() / 2);


// here you can loop and do draw String a lot and just move with string width/height

// or you can use multiple drawImage calls and have a ready made watermark
// this might actually look better and won't require the alpha/rotation code

我参考了这个答案中的代码(Java Graphics2D - Image Watermark with Randomly Located Tag Cloud),修改成这样

    public static void main(String[] args) throws IOException {
    addTextWatermark("Photopea",

            new File("E:\sourceImageFile.jpg"),

            new File("E:\destImageFile.jpg"));

}

static void addTextWatermark(String text, File sourceImageFile, File destImageFile) {

    try {
        int fontSize = 60;
        BufferedImage image = ImageIO.read(sourceImageFile);
        Graphics2D g = (Graphics2D) image.getGraphics();

        AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f);
        g.setComposite(ac);
        g.setFont(new Font("Courier", Font.PLAIN, fontSize));
        g.setColor(Color.blue);
        g.rotate(Math.PI / -10);

        final String[] splitStr = text.split("\n");
        int maxMsgLength = Arrays.stream(splitStr).mapToInt(String::length).max().orElse(0);

        final int enterTotal = splitStr.length;

        for (int i = -image.getHeight(); i <= image.getHeight() * 2; i = i + (fontSize * enterTotal + 200)) {
            for (int j = -image.getWidth(); j <= image.getWidth() * 2; j = j + (fontSize * maxMsgLength)) {
                for (int subIndex = 0; subIndex < splitStr.length; subIndex++) {
                    g.drawString(splitStr[subIndex], j, i + ((fontSize) * subIndex));
                }
            }
        }

        ImageIO.write(image, "jpg", destImageFile);
    } catch (Exception e) {
        e.printStackTrace();
    }
}