我有一個文本文檔(.txt)。我想將其轉換爲圖像(.png或.jpg)。例如,在白色背景上的黑色文字。我怎樣才能做到這一點編程?將文本轉換爲Android上的圖像文件
回答
這個(未經測試的)代碼應該讓你走上正確的軌道。
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();
}
這是你所需要的:
http://mvnrepository.com/artifact/org.apache.xmlgraphics/xmlgraphics-commons/1.3.1
,如果你想我可以爲您提供的示例代碼。
編輯:簡單示例: 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);
}
}
}
}
雅我想樣本可以üPLZ提供 – NareshRavva 2012-09-04 11:50:26
我想多行文本的正確方法是這樣的:
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();
StaticLayout似乎只是Android ... – 2013-12-19 15:01:20
問題被標記爲Android所以... :) – 2013-12-19 16:39:04
是的,留下評論作爲一個警告像我這樣的人誰谷歌帶來一個非機器人搜索:) – 2013-12-19 16:56:48
- 1. 如何將圖像轉換爲android中的文本轉換器?
- 2. 圖像識別將圖像轉換爲android中的文本
- 3. 將文本從圖像轉換爲文本文件
- 4. 如何將圖像文件轉換爲Android中的pdf文件
- 5. 在Android上將文本轉換爲位圖(像素)
- 6. 將文本文件轉換爲圖形
- 7. SPen將圖像轉換爲文本
- 8. 將文本轉換爲圖像
- 9. 將動態文本轉換爲圖像
- 10. 將圖像轉換爲文本 - Java
- 11. ABCpdf將文本轉換爲圖像
- 12. WPF將圖像轉換爲文本
- 13. 將文本轉換爲圖像php
- 14. 如何將文本轉換爲圖像?
- 15. 將圖像轉換爲文本
- 16. 如何將文本轉換爲圖像?
- 17. 將文本轉換爲圖像
- 18. HTML文件將其轉換爲本地圖像文件
- 19. 將文本文件中的矩陣轉換爲灰度圖像
- 20. 將圖像轉換爲文本形式(壓縮),然後從文本轉換爲圖像在Android
- 21. 如何將E01圖像文件轉換爲dd圖像文件?
- 22. 使用Ajax上傳DOM圖像 - 將圖像轉換爲文件?
- 23. 將HTML錶轉換爲圖像文件
- 24. 將圖像byte []轉換爲文件
- 25. 將.html文件轉換爲圖像
- 26. pdfbox將圖像轉換爲pdf文件
- 27. 將STL文件轉換爲圖像
- 28. 如何將文本文件轉換爲圖像
- 29. 將包含文本的圖像轉換爲可編輯文本
- 30. 文本文件,圖像轉換
好,謝謝。我會測試它 – 2012-04-02 08:37:16
它工作像火箭 – 2012-04-02 09:28:20
問題是我不能給「\ n」在文本中,使其來到下一行 – 2012-04-02 09:38:58