I have a text document (.txt). I want to convert it to an image (.png or .jpg). For example, black text on white background. How can I do that programmatically?
Asked
Active
Viewed 1.4k times
3 Answers
13
I think the proper way for multi-line text is this:
String text = "This \nis \nmultiline";
final Rect bounds = new Rect();
TextPaint textPaint = new TextPaint() {
{
setColor(Color.WHITE);
setTextAlign(Paint.Align.LEFT);
setTextSize(20f);
setAntiAlias(true);
}
};
textPaint.getTextBounds(text, 0, text.length(), bounds);
StaticLayout mTextLayout = new StaticLayout(text, textPaint,
bounds.width(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
int maxWidth = -1;
for (int i = 0; i < mTextLayout.getLineCount(); i++) {
if (maxWidth < mTextLayout.getLineWidth(i)) {
maxWidth = (int) mTextLayout.getLineWidth(i);
}
}
final Bitmap bmp = Bitmap.createBitmap(maxWidth , mTextLayout.getHeight(),
Bitmap.Config.ARGB_8888);
bmp.eraseColor(Color.BLACK);// just adding black background
final Canvas canvas = new Canvas(bmp);
mTextLayout.draw(canvas);
FileOutputStream stream = new FileOutputStream(...); //create your FileOutputStream here
bmp.compress(CompressFormat.PNG, 85, stream);
bmp.recycle();
stream.close();

M-Wajeeh
- 17,204
- 10
- 66
- 103
-
Question was tagged with Android so ... :) – M-Wajeeh Dec 19 '13 at 16:39
-
Yes, left the comment as a warning to people like me who google brings here on a non-android search :) – Tim B Dec 19 '13 at 16:56
-
The size of text is very small when a line contains many characters. – Anuj Dec 19 '14 at 13:30
-
The best answer after hour of search for suitable solution! Someone recommended ```xmlgraphics-commons``` https://mvnrepository.com/artifact/org.apache.xmlgraphics/xmlgraphics-commons/2.9. Pure Java, but worth to explore. – Gleichmut Aug 31 '23 at 15:55
7
this (untested) code should get you on the right track.
void foo(final String text) throws IOException{
final Paint textPaint = new Paint() {
{
setColor(Color.WHITE);
setTextAlign(Paint.Align.LEFT);
setTextSize(20f);
setAntiAlias(true);
}
};
final Rect bounds = new Rect();
textPaint.getTextBounds(text, 0, text.length(), bounds);
final Bitmap bmp = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.RGB_565); //use ARGB_8888 for better quality
final Canvas canvas = new Canvas(bmp);
canvas.drawText(text, 0, 20f, textPaint);
FileOutputStream stream = new FileOutputStream(...); //create your FileOutputStream here
bmp.compress(CompressFormat.PNG, 85, stream);
bmp.recycle();
stream.close();
}

Renard
- 6,909
- 2
- 27
- 23
-
problem is i could not give "\n" with in text to make it come next line – Seshu Vinay Apr 02 '12 at 09:38
-
Canvas.drawText() does not handle line breaks. There is a (ugly) workaround though. See this [post](http://stackoverflow.com/questions/6756975/draw-multi-line-text-to-canvas) for details. – Renard Apr 02 '12 at 11:39
0
This is what you need:
http://mvnrepository.com/artifact/org.apache.xmlgraphics/xmlgraphics-commons/1.3.1
I can provide you sample code if you want.
Edit: simple example: package v13;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.apache.xmlgraphics.image.codec.png.PNGImageEncoder;
public class Deneme {
public static void main(String[]args){
JFrame jf = new JFrame();
jf.setVisible(true);
JPanel jp = new JPanel();
jf.add(jp);
JLabel jl = new JLabel("trial text");
jf.add(jl);
jf.setSize(300, 200);
JFileChooser jfc = new JFileChooser();
int temp = jfc.showSaveDialog(jfc);
if (temp == JFileChooser.APPROVE_OPTION) {
System.out.println(jfc.getSelectedFile());
Component myComponent = jf;
Dimension size = myComponent.getSize();
BufferedImage myImage = new BufferedImage(size.width,
size.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = myImage.createGraphics();
myComponent.paint(g2);
try {
OutputStream out = new FileOutputStream(jfc
.getSelectedFile().getAbsolutePath()
+ ".png");
PNGImageEncoder encoder = new PNGImageEncoder(out, null);
encoder.encode(myImage);
out.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
}