Convert Text To Image File On Android
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?
Solution 1:
I think the proper way for multi-line text is this:
Stringtext="This \nis \nmultiline";
finalRectbounds=newRect();
TextPainttextPaint=newTextPaint() {
{
setColor(Color.WHITE);
setTextAlign(Paint.Align.LEFT);
setTextSize(20f);
setAntiAlias(true);
}
};
textPaint.getTextBounds(text, 0, text.length(), bounds);
StaticLayoutmTextLayout=newStaticLayout(text, textPaint,
bounds.width(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
intmaxWidth= -1;
for (inti=0; i < mTextLayout.getLineCount(); i++) {
if (maxWidth < mTextLayout.getLineWidth(i)) {
maxWidth = (int) mTextLayout.getLineWidth(i);
}
}
finalBitmapbmp= Bitmap.createBitmap(maxWidth , mTextLayout.getHeight(),
Bitmap.Config.ARGB_8888);
bmp.eraseColor(Color.BLACK);// just adding black backgroundfinalCanvascanvas=newCanvas(bmp);
mTextLayout.draw(canvas);
FileOutputStreamstream=newFileOutputStream(...); //create your FileOutputStream here
bmp.compress(CompressFormat.PNG, 85, stream);
bmp.recycle();
stream.close();
Solution 2:
this (untested) code should get you on the right track.
voidfoo(final String text)throws IOException{
finalPainttextPaint=newPaint() {
{
setColor(Color.WHITE);
setTextAlign(Paint.Align.LEFT);
setTextSize(20f);
setAntiAlias(true);
}
};
finalRectbounds=newRect();
textPaint.getTextBounds(text, 0, text.length(), bounds);
finalBitmapbmp= Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.RGB_565); //use ARGB_8888 for better qualityfinalCanvascanvas=newCanvas(bmp);
canvas.drawText(text, 0, 20f, textPaint);
FileOutputStreamstream=newFileOutputStream(...); //create your FileOutputStream here
bmp.compress(CompressFormat.PNG, 85, stream);
bmp.recycle();
stream.close();
}
Solution 3:
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;
publicclassDeneme {
publicstaticvoidmain(String[]args){
JFramejf=newJFrame();
jf.setVisible(true);
JPaneljp=newJPanel();
jf.add(jp);
JLabeljl=newJLabel("trial text");
jf.add(jl);
jf.setSize(300, 200);
JFileChooserjfc=newJFileChooser();
inttemp= jfc.showSaveDialog(jfc);
if (temp == JFileChooser.APPROVE_OPTION) {
System.out.println(jfc.getSelectedFile());
ComponentmyComponent= jf;
Dimensionsize= myComponent.getSize();
BufferedImagemyImage=newBufferedImage(size.width,
size.height, BufferedImage.TYPE_INT_RGB);
Graphics2Dg2= myImage.createGraphics();
myComponent.paint(g2);
try {
OutputStreamout=newFileOutputStream(jfc
.getSelectedFile().getAbsolutePath()
+ ".png");
PNGImageEncoderencoder=newPNGImageEncoder(out, null);
encoder.encode(myImage);
out.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
}
Post a Comment for "Convert Text To Image File On Android"