3

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;
}
;
};
Gidil
  • 4,079
  • 2
  • 31
  • 48
alvin890101
  • 57
  • 1
  • 7

1 Answers1

1

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.

Community
  • 1
  • 1
Geobits
  • 21,723
  • 6
  • 56
  • 102
  • what should I put in myPaint? – alvin890101 Aug 01 '13 at 19:35
  • [`Paint`](http://developer.android.com/reference/android/graphics/Paint.html) lets you define how the text will look. Alignment, color, font, etc. You could probably just pass `null`, but I have no idea what the default paint looks like. I've never used it for text. – Geobits Aug 01 '13 at 19:36
  • @Geobits What is `cameraBitmap`? – oo92 Feb 02 '19 at 22:32