2012-01-10 45 views
13

Có thể chuyển đổi chuỗi văn bản nằm trong một hộp EditText thành một ảnh bitmap không? Nói cách khác, có cách nào để chuyển đổi chuỗi văn bản thành một bitmap có nghĩa là văn bản sẽ hiển thị dưới dạng hình ảnh không?Chuyển chuỗi văn bản thành Bitmap

Dưới đây là Mã của tôi:

class TextToImage extends Activity { 

    protected void onCreate(Bundle savedInstanceState) { 
     //create String object to be converted to image 
     String sampleText = "SAMPLE TEXT"; 
     String fileName = "Image"; 

     //create a File Object 
     File newFile = new File("./" + fileName + ".jpeg"); 

     //create the font you wish to use 
     Font font = new Font("Tahoma", Font.PLAIN, 11); 

     //create the FontRenderContext object which helps us to measure the text 
     FontRenderContext frc = new FontRenderContext(null, true, true); 
    } 
} 

Trả lời

56

Bạn có thể tạo một Bitmap kích thước phù hợp, tạo ra một Canvas cho Bitmap, và sau đó vẽ văn bản của bạn vào đó. Bạn có thể sử dụng đối tượng Paint để đo văn bản để bạn biết kích thước cần thiết cho bitmap. Bạn có thể làm một cái gì đó như thế này (chưa được kiểm tra):

public Bitmap textAsBitmap(String text, float textSize, int textColor) { 
    Paint paint = new Paint(ANTI_ALIAS_FLAG); 
    paint.setTextSize(textSize); 
    paint.setColor(textColor); 
    paint.setTextAlign(Paint.Align.LEFT); 
    float baseline = -paint.ascent(); // ascent() is negative 
    int width = (int) (paint.measureText(text) + 0.5f); // round 
    int height = (int) (baseline + paint.descent() + 0.5f); 
    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(image); 
    canvas.drawText(text, 0, baseline, paint); 
    return image; 
} 
+0

Ted Hopp khi tôi đọc trên internet awt không hỗ trợ trong hệ điều hành android ... – shyam

+2

@shyam - đây là tất cả các lớp Android, không phải lớp AWT. Họ đang trong gói 'android.graphics' –

+0

hey Ted Hopp tôi sử dụng canvas.drawText() để vẽ văn bản trên canvas bây giờ tôi muốn di chuyển nó trên liên lạc của văn bản có nghĩa là di chuyển văn bản trên chuyển động ngón tay..i có mã để di chuyển hình ảnh trên bề mặt của vải .. nếu nó có thể làm cho văn bản như hình ảnh sau đó tôi có thể sử dụng nó như hình ảnh và di chuyển văn bản trên màn hình .. làm thế nào để chuyển đổi văn bản vào BitMap để di chuyển nó trên màn hình? – shyam

0

Đối với chỉ Chuỗi Tôi không biết nhưng,

Bạn sẽ nhận được Bitmap image of the whole EditText không chỉ Chuỗi với điều này,

mEditText.setCursorVisible(false); 
mEditText.buildDrawingCache(); 
Bitmap bmp = Bitmap.createBitmap(mEditText.getDrawingCache()); 
1
String text = "Hello world!"; 
Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
Canvas c = new Canvas(b); 
c.drawBitmap(b, 0, 0, null); 
TextPaint textPaint = new TextPaint(); 
textPaint.setAntiAlias(true); 
textPaint.setTextSize(16.0F); 
StaticLayout sl= new StaticLayout(text, textPaint, b.getWidth()-8, Alignment.ALIGN_CENTER, 1.0f, 0.0f, false); 
c.translate(6, 40); 
sl.draw(c); 
return b 
0

hãy thử cách này:

public static Bitmap drawText(String text, int textWidth, int textSize) { 
// Get text dimensions 
TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG 
| Paint.LINEAR_TEXT_FLAG); 
textPaint.setStyle(Paint.Style.FILL); 
textPaint.setColor(Color.BLACK); 
textPaint.setTextSize(textSize); 
StaticLayout mTextLayout = new StaticLayout(text, textPaint, 
textWidth, Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); 

// Create bitmap and canvas to draw to 
Bitmap b = Bitmap.createBitmap(textWidth, mTextLayout.getHeight(), Config.RGB_565); 
Canvas c = new Canvas(b); 

// Draw background 
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG 
| Paint.LINEAR_TEXT_FLAG); 
paint.setStyle(Paint.Style.FILL); 
paint.setColor(Color.WHITE); 
c.drawPaint(paint); 

// Draw text 
c.save(); 
c.translate(0, 0); 
mTextLayout.draw(c); 
c.restore(); 

return b; 
} 
0

Tôi đã ada Câu trả lời của pted @TedHopp để đảm bảo hình ảnh được tạo là luôn là hình vuông, có thể hữu ích tùy thuộc vào vị trí hình ảnh sẽ được hiển thị, chẳng hạn như biểu tượng NavigationDrawer hoặc tương tự. Văn bản nằm ở giữa hình ảnh ở giữa hình ảnh.

public static Bitmap textAsBitmap(String text, float textSize, int textColor) { 
    // adapted from https://stackoverflow.com/a/8799344/1476989 
    Paint paint = new Paint(ANTI_ALIAS_FLAG); 
    paint.setTextSize(textSize); 
    paint.setColor(textColor); 
    paint.setTextAlign(Paint.Align.LEFT); 
    float baseline = -paint.ascent(); // ascent() is negative 
    int width = (int) (paint.measureText(text) + 0.0f); // round 
    int height = (int) (baseline + paint.descent() + 0.0f); 

    int trueWidth = width; 
    if(width>height)height=width; else width=height; 
    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 

    Canvas canvas = new Canvas(image); 
    canvas.drawText(text, width/2-trueWidth/2, baseline, paint); 
    return image; 
} 
Các vấn đề liên quan