我正在通过java代码将.txt文件转换为.bmp文件。我的.txt文件包含多行。我正在阅读每一行并将其打印在图像上。
**我的文本文件**
45
werwerwr
abc abc abc
xxxx
**正在创建的图像(.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();
}
}
}
我已经找到了解决问题的方法:
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 {
int imgWid = 300;
int imgHei = 300;
String text = "Hellonlklkj";
String storeData = "";
String data = "";
File f = new File("/home/vipul/Desktop/123.txt");
BufferedReader br = new BufferedReader(new FileReader(f));
int nextLinePosition=100;
int fontSize = 24;
// BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_GRAY);//png
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g2d = img.createGraphics();
Font font = new Font("Arial", Font.PLAIN, fontSize);
g2d.setFont(font);
FontMetrics fm = g2d.getFontMetrics();
// int width = fm.stringWidth(text);
// int width = fm.stringWidth(storeData);
int width =imgWid;
// int height = fm.getHeight();
int height =imgHei;
g2d.dispose();
img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
// img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
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.setBackground(Color.ORANGE);
// g2d.drawString(text, 0, fm.getAscent());
// for(String line : text.split("n")){
// g2d.drawString(storeData, 0, fm.getAscent());
// }
// private void drawString( Graphics2D g2d, String text, int x, int y) {
// for (String line : storeData.split("n")) // g2d.drawString(line, width, height += g2d.getFontMetrics().getHeight());
// {
while ((data = br.readLine()) != null) {
g2d.drawString(data, 0, nextLinePosition);
// storeData = storeData + data + nextLinePosition;
nextLinePosition=nextLinePosition+fontSize;
//storeData = storeData + data + 'n';
}
// g2d.drawString(line, 0, 12);
// }
//}
// g2d.drawString(storeData, 0, fm.getAscent());
g2d.dispose();
// try {
ImageIO.write(img, "bmp", new File("/home/vipul/Desktop/123.bmp"));
// }
} catch (Exception ex) {
ex.printStackTrace();
}
}
}