How to write text on an image [duplicate]
Asked Answered
P

3

7

I can do this in Objective-C on the iPhone, but now I'm looking for the equivalent Android Java code. I can also do it in plain Java, but I don't know what the Android specific classes are. I want to generate a PNG image on the fly that has some text centered in the image.

public void createImage(String word, String outputFilePath){ 
    /* what do I do here? */ 
}

Relevant threads:

Pedal answered 3/7, 2012 at 19:54 Comment(0)
L
23

What about something like:

Bitmap bitmap = ... // Load your bitmap here
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(); 
paint.setColor(Color.BLACK); 
paint.setTextSize(10); 
canvas.drawText("Some Text here", x, y, paint);
Lepidolite answered 3/7, 2012 at 19:59 Comment(0)
T
7

You don't need to use graphics.

A simpler approach would be to create a FrameLayout with two elements- the ImageView for the image, and another view for whatever you want drawn on top.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</FrameLayout>

Of course, the thing on top of the image doesn't need to be a simple TextView, it can be another image, or another layout containing whatever arbitrary elements you like.

Tentmaker answered 3/7, 2012 at 20:6 Comment(6)
This is easiest way, and works not only with static images but also surface views or whatever elseDuero
I'll give you an upvote because I agree with you in most circumstances. But I specifically asked a different question because I have a requirement that it needs to be embedded in the image.Pedal
where in your question did you say that it needs to be embedded? anyway.Tentmaker
Good point, I apologize. I thought I did, but I should have been more explicit.Pedal
It might not have been said explicitly but I did understand it that he wants the text embedded. You can also see that from the two provided links as well. So although he was nice to give you upvote because he is afraid of confrontation he should not have.Carpentaria
Hi friend. This answer wasn't accepted, but clearly people find it useful. Sorry this offends you enough to spend your karma downvoting it.Tentmaker
M
0

GETah's answer wasn't working for me so I tweeked it a bit, here's a Kotlin version, which works, using TextPaint:

val canvas = Canvas(bm)
val textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG or Paint.LINEAR_TEXT_FLAG)
textPaint.style = Paint.Style.FILL
textPaint.color = Color.BLACK
textPaint.textSize = 30f
canvas.drawText("Hello", 50f, 50f, textPaint)
Maniple answered 14/1, 2020 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.