gpt4 book ai didi

java - 将字符串写入图像时,我得到一个又长又窄的图像 - 如何断线?

转载 作者:行者123 更新时间:2023-11-30 09:34:06 24 4
gpt4 key购买 nike

我正在尝试将字符串写入图像,因此复制文本并通过翻译器运行它比较困难。

我的代码工作正常,但我总是得到一个非常长的图像 - 我宁愿在写入字符串的地方有一个更具可读性的框。我的方法“StringDiver”确实添加了“\n”,但在将字符串写入图像时它没有帮助。

现在我得到 this输出。

有什么提示吗?

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class writeToImage {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

String newString = "Mein Eindruck ist, dass die politische und öffentliche Meinung in Deutschland anfängt, die wirtschaftliche Zerstörung im Inland und in Europa zu erkennen, die auf einen eventuellen Zusammenbruch des Euro folgen würde.";
String sampleText = StringDivider(newString);

//Image file name
String fileName = "Image";

//create a File Object
File newFile = new File("./" + fileName + ".jpg");

//create the font you wish to use
Font font = new Font("Tahoma", Font.PLAIN, 15);

//create the FontRenderContext object which helps us to measure the text
FontRenderContext frc = new FontRenderContext(null, true, true);

//get the height and width of the text
Rectangle2D bounds = font.getStringBounds(sampleText, frc);
int w = (int) bounds.getWidth();
int h = (int) bounds.getHeight();

//create a BufferedImage object
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

//calling createGraphics() to get the Graphics2D
Graphics2D g = image.createGraphics();

//set color and other parameters
g.setColor(Color.WHITE);
g.fillRect(0, 0, w, h);
g.setColor(Color.BLACK);
g.setFont(font);

g.drawString(sampleText, (float) bounds.getX(), (float) -bounds.getY());

//releasing resources
g.dispose();

//creating the file
try {
ImageIO.write(image, "jpg", newFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static String StringDivider(String s){

StringBuilder sb = new StringBuilder(s);

int i = 0;
while ((i = sb.indexOf(" ", i + 30)) != -1) {
sb.replace(i, i + 1, "\n");
}

return sb.toString();



}
}

最佳答案

g.drawString(sampleText, (float) bounds.getX(), (float) -bounds.getY());

拆分文本并将每个部分写入图像。

Rectangle2D bounds = font.getStringBounds(sampleText, frc);
int w = (int) bounds.getWidth();
int h = (int) bounds.getHeight();

String[] parts = sampleText.split("\n");
//create a BufferedImage object
BufferedImage image = new BufferedImage(w, h * parts.length, BufferedImage.TYPE_INT_RGB);

int index = 0;
for(String part : parts){
g.drawString(part, 0, h * index++);
}

例如:

first part:  x=0 ; y=0
second part: x=0 ; y=5
third part: x=0 ; y=10;

高度文本 = h

关于java - 将字符串写入图像时,我得到一个又长又窄的图像 - 如何断线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11900253/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com