Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android add text to Picture and save

I want to place a Textiew to my picture and save it, but I'm not sure how to insert the text into the picture.

I can attach an image on my picture save it, and it works but now I want to insert a Textiew into the picture.

Here is my code:

PictureCallback cameraPictureCallbackJpeg = new PictureCallback() 
{  
@Override
public void onPictureTaken(byte[] data, Camera camera) 
{
  // TODO Auto-generated method stub   
  Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

  wid = cameraBitmap.getWidth();
  hgt = cameraBitmap.getHeight();

  Bitmap newImage = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(newImage);
  canvas.drawBitmap(cameraBitmap, 0f, 0f, null);
  Drawable drawable = getResources().getDrawable(R.drawable.love);
  drawable.setBounds(20, 20, 260, 160);
  drawable.draw(canvas);

  File storagePath = new File(Environment.getExternalStorageDirectory() + "/MyPicture/"); 
  storagePath.mkdirs(); 

  File myImage = new File(storagePath,Long.toString(System.currentTimeMillis()) + ".jpg");

  try
  {
    FileOutputStream out = new FileOutputStream(myImage);
    newImage.compress(Bitmap.CompressFormat.JPEG, 80, out);


    out.flush();
    out.close();
  }
  catch(FileNotFoundException e)
  {
    Log.d("In Saving File", e + "");    
  }
  catch(IOException e)
  {
    Log.d("In Saving File", e + "");
  }

  camera.startPreview();

  drawable = null;

  newImage.recycle();
  newImage = null;

  cameraBitmap.recycle();
  cameraBitmap = null;
}
;
};
like image 284
alvin890101 Avatar asked Nov 30 '25 21:11

alvin890101


1 Answers

If you just want "text", and not necessarily a TextView, you can draw text directly on the Canvas using drawText().

Just change it to something like:

  Canvas canvas = new Canvas(newImage);
  canvas.drawBitmap(cameraBitmap, 0f, 0f, null);
  canvas.drawText("some text here", x, y, myPaint);

  ...

  newImage.compress(Bitmap.CompressFormat.JPEG, 80, out);

If you need it to be a TextView for sure, you can convert the view to a bitmap, then draw it on the canvas with drawBitmap(). See this answer for an example of how to convert it.

like image 157
Geobits Avatar answered Dec 02 '25 13:12

Geobits



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!