gpt4 book ai didi

java - 如何在读取文件并创建 .bmp 图像时继续下一行?

转载 作者:行者123 更新时间:2023-12-02 03:34:40 24 4
gpt4 key购买 nike

我正在通过我的 java 代码将 .txt 文件转换为 .bmp 文件。我的 .txt 文件包含多行。我正在阅读每一行并将其打印在图像上。

** 我的文本文件**

45

我们

abc abc abc

xxx

** 正在创建的图像(.bmp) **

所有内容都打印在一行中,如下所示:

45werwerwrabc abc abcxxxx

我的代码:

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.imageio.ImageIO;

public class TextToGraphics {

public static void main(String[] args) {
try {
String storeData = "";
String data = "";
File f = new File("/home/vipul/Desktop/123.txt");
BufferedReader br = new BufferedReader(new FileReader(f));

while ((data = br.readLine()) != null) {
storeData =storeData + data +System.lineSeparator();
}


BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g2d = img.createGraphics();
Font font = new Font("Arial", Font.PLAIN, 24);
g2d.setFont(font);

FontMetrics fm = g2d.getFontMetrics();

int width = fm.stringWidth(storeData);
int height = fm.getHeight();
g2d.dispose();

img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
g2d = img.createGraphics();
g2d.setPaint ( new Color ( 255, 255, 255 ) );
g2d.fillRect ( 0, 0, img.getWidth(), img.getHeight() );
System.out.println(img.getWidth()+" "+img.getHeight());
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setFont(font);
fm = g2d.getFontMetrics();


g2d.setColor(Color.BLACK);


g2d.drawString(storeData, 0, fm.getAscent());
g2d.dispose();

ImageIO.write(img, "bmp", new File("/home/vipul/Desktop/123.bmp"));

} catch (Exception ex) {
ex.printStackTrace();
}

}

}

我想让图像与文本文件相同。如果文本文件转到新行,则图像也应该在新行上具有相同的单词。

请帮忙。提前致谢!!!

最佳答案

您正在将文件保存在字符串中。即使您在每行后面附加一个 lineSeparator,img 也不知道换行符是什么。您必须自己分割数据并为每一行调用drawString。示例:

public class TextToGraphics {

public static void main(String[] args) {
try {
String storeData = "";
String data = "";
File f = new File("/home/vipul/Desktop/123.txt");
BufferedReader br = new BufferedReader(new FileReader(f));

while ((data = br.readLine()) != null) {
storeData =storeData + data +System.lineSeparator();
}
String [] sArr = storeData.split("\n");
int longest =0;
int lines = 0;
for (String s : sArr){ //find the longest string
if(s.length()>longest){
longest = s.length();
}
lines++; //count lines
}

BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g2d = img.createGraphics();
Font font = new Font("Arial", Font.PLAIN, 24);
g2d.setFont(font);

FontMetrics fm = g2d.getFontMetrics();

int width = fm.stringWidth(storeData);
int height = fm.getHeight();
g2d.dispose();

img = new BufferedImage(longest*font.getSize(), lines*font.getSize(), BufferedImage.TYPE_3BYTE_BGR); // set width and height in accordance with longest string and number of lines
g2d = img.createGraphics();
g2d.setPaint ( new Color ( 255, 255, 255 ) );
g2d.fillRect ( 0, 0, img.getWidth(), img.getHeight() );
System.out.println(img.getWidth()+" "+img.getHeight());
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setFont(font);
fm = g2d.getFontMetrics();


g2d.setColor(Color.BLACK);
int i = 0;
for(String s : storeData.split("\n")){
g2d.drawString(s, 0, fm.getAscent()+i); // call drawString for each line
i+=10;
}
g2d.dispose();

ImageIO.write(img, "bmp", new File("/home/vipul/Desktop/123.bmp"));

} catch (Exception ex) {
ex.printStackTrace();
}

}

}

关于java - 如何在读取文件并创建 .bmp 图像时继续下一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37588892/

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