I want to draw text on image ( for saving that image with text ). i have image view i set bitmap to that image i want to Draw the text on image (text entered by user ). i tried this before saving.....
void saveImage() { File myDir=new File("/sdcard/saved_images"); myDir.mkdirs(); Random generator = new Random(); int n = 10000; n = generator.nextInt(n); String fname = "Image-"+ n +".jpg"; File file = new File (myDir, fname); if (file.exists ()) file.delete (); try { FileOutputStream out = new FileOutputStream(file); originalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } }
Xml code is..
<FrameLayout android:id="@+id/framelayout" android:layout_marginTop="30dip" android:layout_height="fill_parent" android:layout_width="fill_parent"> <ImageView android:id="@+id/ImageView01" android:layout_alignParentTop="true" android:layout_height="wrap_content" android:layout_width="wrap_content"/> <TextView android:id="@+id/text_view2" android:layout_marginTop="20dip" android:layout_width="wrap_content" android:text="SampleText" android:textSize="12pt" android:layout_alignTop="@+id/ImageView01" android:layout_height="wrap_content"/> </FrameLayout>
On the Insert tab, in the Text group, click Text Box, click anywhere near the picture, and then type your text. To change the font or style of the text, highlight the text, right-click it, and then select the text formatting you want on the shortcut menu.
(x, y): This X and Y denotes the starting position(in pixels)/coordinate of adding the text on an image. Text: A Text or message that we want to add to the Image. Font: specific font type and font size that you want to give to the text. Fill: Fill is for to give the Font color to your text.
As suggested by Vladislav Skoumal, try this method:
public Bitmap drawTextToBitmap(Context mContext, int resourceId, String mText) { try { Resources resources = mContext.getResources(); float scale = resources.getDisplayMetrics().density; Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId); android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig(); // set default bitmap config if none if(bitmapConfig == null) { bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888; } // resource bitmaps are imutable, // so we need to convert it to mutable one bitmap = bitmap.copy(bitmapConfig, true); Canvas canvas = new Canvas(bitmap); // new antialised Paint Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // text color - #3D3D3D paint.setColor(Color.rgb(110,110, 110)); // text size in pixels paint.setTextSize((int) (12 * scale)); // text shadow paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY); // draw text to the Canvas center Rect bounds = new Rect(); paint.getTextBounds(mText, 0, mText.length(), bounds); int x = (bitmap.getWidth() - bounds.width())/6; int y = (bitmap.getHeight() + bounds.height())/5; canvas.drawText(mText, x * scale, y * scale, paint); return bitmap; } catch (Exception e) { // TODO: handle exception return null; } }
call this method
Bitmap bmp =drawTextToBitmap(this,R.drawable.aa,"Hello Android"); img.setImageBitmap(bmp);
the output
Updated SaveImage() method, to support text drawing.
void saveImage() { File myDir=new File("/sdcard/saved_images"); myDir.mkdirs(); Random generator = new Random(); int n = 10000; n = generator.nextInt(n); String fname = "Image-"+ n +".jpg"; File file = new File (myDir, fname); if (file.exists ()) file.delete (); try { FileOutputStream out = new FileOutputStream(file); // NEWLY ADDED CODE STARTS HERE [ Canvas canvas = new Canvas(originalBitmap); Paint paint = new Paint(); paint.setColor(Color.WHITE); // Text Color paint.setTextSize(12); // Text Size paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern // some more settings... canvas.drawBitmap(originalBitmap, 0, 0, paint); canvas.drawText("Testing...", 10, 10, paint); // NEWLY ADDED CODE ENDS HERE ] originalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } }
Let me know if this works for you.
Shash
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With