2012-01-10 22 views
13

क्या EditText बॉक्स के अंदर एक बिटमैप में स्ट्रिंग टेक्स्ट को परिवर्तित करना संभव है? दूसरे शब्दों में, क्या स्ट्रिंग टेक्स्ट को बिटमैप में परिवर्तित करने का कोई तरीका है जिसका अर्थ है कि पाठ एक छवि के रूप में प्रदर्शित होगा?स्ट्रिंग टेक्स्ट को बिटमैप में कनवर्ट करें

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); 
    } 
} 

उत्तर

56

तुम, उचित आकार का एक बिटमैप बना सकते हैं बिटमैप के लिए एक कैनवास बनाने के लिए, और फिर इसे में अपने पाठ आकर्षित:

नीचे मेरी कोड है। आप टेक्स्ट को मापने के लिए एक पेंट ऑब्जेक्ट का उपयोग कर सकते हैं ताकि आप बिटमैप के लिए आवश्यक आकार को जान सकें। आप कुछ इस तरह (untested) कर सकते हैं:

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

टेड हॉप के रूप में मैं इंटरनेट AWT पर पढ़ें एंड्रॉयड ओएस में समर्थन नहीं करता है ... – shyam

+2

@shyam - इन सभी एंड्रॉयड कक्षाएं, नहीं AWT वर्ग हैं। वे पैकेज में हैं 'android.graphics' –

+0

हे टेड होप मैं कैनवास पर पाठ खींचने के लिए canvas.drawText() का उपयोग करता हूं, अब मैं इसे पाठ के स्पर्श पर ले जाना चाहता हूं मतलब है कि उंगली गति पर पाठ ले जाएं .. मेरे पास चलने के लिए कोड है कैनवास की सतह पर छवि .. अगर छवि को छवि के रूप में बनाना संभव है तो मैं इसे छवि के रूप में उपयोग कर सकता हूं और स्क्रीन पर टेक्स्ट ले जा सकता हूं .. तो स्क्रीन पर इसे स्थानांतरित करने के लिए टेक्स्ट को बिटमैप में कैसे परिवर्तित करें? – shyam

0

केवल स्ट्रिंग के लिए मैं नहीं जानता लेकिन,

आप इस बात से Bitmap image of the whole EditText न केवल स्ट्रिंग मिल जाएगा,

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

इसे आजमाएं:

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

मेरे पास ada है बनाई गई छवि सुनिश्चित करने के लिए @TedHopp का उत्तर हमेशा वर्ग है, जो कि छवि को प्रदर्शित करने के तरीके के आधार पर उपयोगी हो सकता है, जैसे NavigationDrawer आइकन या इसी तरह। पाठ क्षैतिज रूप से छवि के बीच में केंद्रित है।

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; 
} 
संबंधित मुद्दे