How to convert text of a TextView to bitmap in Android?
Asked Answered
R

3

5

I'm a beginner in Android development and am trying to develop a program where the user can convert the text being displayed in the TextView (TextView is displaying a code 39 barcode font text which I've imported from assets) to bitmap after pressing the "Convert to Bitmap!" button. I've tried searching around google but I've only managed to get answers like converting string to bitmap without guides on where to type the codes so I'm rather confuse on that. I've tried running the program with the codes I tried typing out after googling around but it crashes everytime I press the convert button.

Really hope that you can help! Thank you in advance! :D

The following are my codes so far:-

*Edited with respect to Simon's code

At java:

public class MainActivity extends Activity 
implements OnClickListener {
    //Called when activity is first created

    TextView tv1;
    ImageView iv;
    Button b;

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

        TextView tv1 = (TextView) findViewById(R.id.txtV);  
        tv1.setDrawingCacheEnabled(true); 

        //To change to code 39 barCode font
        Typeface barcodefont = Typeface.createFromAsset(getAssets(),                 
                "fonts/IDAutomationHC39M_FREE.otf");         
        TextView tv = (TextView) findViewById(R.id.txtV);         
        tv.setTypeface(barcodefont);
    }

    public void onClick(View v) {

        tv1.buildDrawingCache(); 
        iv.setImageBitmap(tv1.getDrawingCache()); 
    } 

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Logcat error:

10-04 06:33:25.076: E/AndroidRuntime(1423): FATAL EXCEPTION: main
10-04 06:33:25.076: E/AndroidRuntime(1423): java.lang.IllegalStateException: Could not find a method ConvertText(View) in the activity class com.example.txtvbitmapconverter.MainActivity for onClick handler on view class android.widget.Button with id 'btnConvert'
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.view.View$1.onClick(View.java:3578)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.view.View.performClick(View.java:4084)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.view.View$PerformClick.run(View.java:16966)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.os.Handler.handleCallback(Handler.java:615)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.os.Looper.loop(Looper.java:137)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at java.lang.reflect.Method.invokeNative(Native Method)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at java.lang.reflect.Method.invoke(Method.java:511)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at dalvik.system.NativeStart.main(Native Method)
10-04 06:33:25.076: E/AndroidRuntime(1423): Caused by: java.lang.NoSuchMethodException: ConvertText [class android.view.View]
10-04 06:33:25.076: E/AndroidRuntime(1423):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at java.lang.Class.getMethod(Class.java:915)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.view.View$1.onClick(View.java:3571)
10-04 06:33:25.076: E/AndroidRuntime(1423):     ... 11 more
Rundown answered 3/10, 2012 at 6:27 Comment(0)
I
11

Try this

Add to your onCreate()

tv1.setDrawingCacheEnabled(true);

Then in your onClick()

tv1.buildDrawingCache();
iv.setImageBitmap(tv1.getDrawingCache());

http://developer.android.com/reference/android/view/View.html#buildDrawingCache(boolean)

http://developer.android.com/reference/android/view/View.html#getDrawingCache(boolean)

[EDIT]

The problem you now have is that you are trying to enable the drawing cache before tv1 exists.

You should do this:

TextView tv1 = (TextView) findViewById(R.id.txtV); 
tv1.setDrawingCacheEnabled(true);
Inhaler answered 3/10, 2012 at 6:36 Comment(12)
I've tried but when I add the tv.setDrawingCacheEnabled(true); into my onCreate() method, the program crashes after debugging. Do I have to remove my if-else from the onClick() method after putting in the codes you provided?Rundown
Please post the logcat stack trace for the exception. And, with respect, I think you need to read some Android tutorials.Inhaler
Do you have any suggestions on where I can read them? I've tried reading from the developers website weeks ago but it somehow just doesn't make sense to me. So sorry for the redundant questions I've asked.Rundown
Try this vogella.com/articles/Android/article.html but please post your logcat for the exception you are gettingInhaler
Thank you for the link (: Heres the logcat error report I'm getting: 10-03 09:48:57.249: E/Trace(709): error opening trace file: No such file or directory (2) 10-03 09:48:57.849: D/AndroidRuntime(709): Shutting down VM 10-03 09:48:57.849: W/dalvikvm(709): threadid=1: thread exiting with uncaught exception (group=0x40a13300) 10-03 09:48:57.869: E/AndroidRuntime(709): FATAL EXCEPTION: mainRundown
Just edit your question to include the logcat, no need for the comments :) The problem is here Caused by: java.lang.NullPointerException 10-03 09:48:57.869: E/AndroidRuntime(709): at com.example.txtvbitmapconverter.MainActivity.onCreate(MainActivity.java:26) Have a look at line 26 in your onCreate. If you have changed the code please replace it in your question.Inhaler
Line 26 is "tv.setDrawingCacheEnabled(true); ". I've updated my question (: sorry for the spam previously!Rundown
I've edited my question with the new logcat error and java. I managed to start it up at the emulator right now but the program still crashes when I click on the "Convert to Bitmap!" button.Rundown
OK. We're starting to debug your code line by line which is not what SO is for. You should read some tutorials and search for answers here. Step through your code with the debugger and find concrete questions to come back with. But please start a new question. For now, you need to learn how to set an onClickListener on a button. You've got what you need to convert text to bitmap, now you need to learn how Android works. Please accept my answer then start a new question if you need it. CheersInhaler
Update I managed to get the program working after setting the onClickListener (: Thank you so much for all your help!Rundown
Ah; Sorry bout that! Just corrected it, thank you so much once again!Rundown
getDrawingCache() is currently deprecated.Slipper
S
0

the line in onCreate():

TextView tv1 = (TextView) findViewById(R.id.txtV);

should be

tv1 = (TextView) findViewById(R.id.txtV);
Shortfall answered 14/5, 2020 at 18:1 Comment(1)
This is the solution to the original post. The tv1 member was shadowed in onCreate.Stull
T
0

buildDrawingCache() is deprecated now. Use updated version:

Bitmap bitmap = Bitmap.createBitmap(tv1.getWidth(), tv1.getHeight(), Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(bitmap);

tv1.draw(canvas);

ImageView img = findViewById(R.id.imageview);

img.setImageBitmap(bitmap);
Trevatrevah answered 7/2 at 20:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.