Jibran Khan
Jibran Khan

Reputation: 3256

Android text to image

I want to convert the text in a string to image and display it in a ImageView and in addition i want to get the dimensions of the image created at run time for further use. Here is what i have done and used from search.

tv = new TextView(this);
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 60);

    tv.setLayoutParams(layoutParams);

    tv.setText("Some Text, can be long as much as required");
    tv.setTextColor(Color.BLACK);
    tv.setBackgroundColor(Color.WHITE);

    Bitmap testB;
    timer = new Timer();
    timer.schedule(new TickerTask(), 1000,25);
    testB = Bitmap.createBitmap(600, 20, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(testB);

    tv.layout(0, 0, 380, 100);
    tv.draw(c);

    iv = (ImageView) findViewById(R.id.menuIcon);
    iv.setLayoutParams(layoutParams);
    iv.setBackgroundColor(Color.GREEN);
    iv.setImageBitmap(testB);

Problems: The parameters are not working as set in the code. The complete text is not shown after the image is converted.

Upvotes: 2

Views: 4661

Answers (3)

lvapp me
lvapp me

Reputation: 1

i have tried Text2Jpg which look awesome and convert any text to a jpg image with or without background custom image.

Use StaticLayout:

    TextPaint mTextPaint = new TextPaint();
    mTextPaint.setColor(bordercolor);
    mTextPaint.setTextSize(textSize);
    StaticLayout mTextLayout = new StaticLayout(text, mTextPaint, (int) (screen_width * .75f), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    // DynamicLayout mTextLayout = new DynamicLayout(text, mTextPaint,(int)(screen_width * .75f), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    //mTextPaint.setStrokeWidth(screen_width * .70f);
    canvas.translate(screen_width * .12f, screen_height * .06f); //position the text
    mTextLayout.draw(canvas);

Upvotes: 0

Jibran Khan
Jibran Khan

Reputation: 3256

This worked for me, for those who want the same thing to acheive

 BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
    Bitmap bitmap = drawable.getBitmap();
    //Bitmap bitmap = testB;
    File sdCardDirectory = Environment.getExternalStorageDirectory();
    File image = new File(sdCardDirectory, "test.png");

    boolean success = false;

    // Encode the file as a PNG image.
    FileOutputStream outStream;
    try {

        outStream = new FileOutputStream(image);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
        /* 100 to keep full quality of the image */

        outStream.flush();
        outStream.close();
        success = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (success) {
        Toast.makeText(getApplicationContext(), "Image saved with success",
                Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(getApplicationContext(),
                "Error during image saving", Toast.LENGTH_LONG).show();
    }

Upvotes: 0

Furqi
Furqi

Reputation: 2403

I believe its taking up the whole screen because you don't have a container such as a Linear Layout which then contains an ImageView with layout constraints, so the ImageView expands to fill the available screen. Try this:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView tv = new TextView(this);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(80, 100);
    tv.setLayoutParams(layoutParams);
    tv.setText("testing 1 2 3");
    tv.setTextColor(Color.BLACK);
    tv.setBackgroundColor(Color.TRANSPARENT);

    Bitmap testB;

    testB = Bitmap.createBitmap(80, 100, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(testB);
    tv.layout(0, 0, 80, 100);
    tv.draw(c);

    ImageView iv = (ImageView)findViewById(R.id.menuIcon);
    iv.setLayoutParams(layoutParams);
    iv.setBackgroundColor(Color.GRAY);
    iv.setImageBitmap(testB);
    iv.setMaxHeight(80);
    iv.setMaxWidth(80);
}

And in your main.xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

  <ImageView android:id="@+id/menuIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


</LinearLayout>

Upvotes: 1

Related Questions