0

I have application which uses camera intent and saves picture taken into specific folder. Now i am missing one more functionality and that is that after picture is taken i should add plain text (timestamp, few more infos on picture lower/upper border) and save it.

How to achieve this? My code is below

public void takePicture( View view)
{
    Intent intentCamera = new Intent();
    intentCamera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

    File photofile = null;
    try{
        photofile=createImageFile();
        intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photofile));
        startActivityForResult(intentCamera, ActivityStartamera);
        //Toast.makeText(this,Uri.fromFile(photofile).toString(), Toast.LENGTH_LONG).show();
    }
    catch (IOException e)
    {
        e.printStackTrace();
        Toast.makeText(this, "Error happened", Toast.LENGTH_LONG).show();
        imageFileName="";
    }
}

protected  void onActivityResult (int requestCode, int resultCode, Intent data)
{
    if(requestCode == ActivityStartamera && resultCode == RESULT_OK)
    {
        //
        //here i should add plain text to taken picture
        //
        Toast.makeText(this, "Picture taken", Toast.LENGTH_LONG).show();
    }
    else{
        image.delete();
    }
}

File createImageFile () throws IOException{
    String timeStamp = new SimpleDateFormat("MMyyyydd_HHmmss").format(new Date());
    imageFileName = "picture_"+timeStamp;

    storage = Environment.getExternalStoragePublicDirectory("PicturesForApp");
    if (!storage.exists())
    {
        Toast.makeText(this, "Folder made for pictures", Toast.LENGTH_LONG).show();
        storage.mkdirs();
    }
    image = new File(storage + "/" +imageFileName +".jpg");
    return image;
}

I have tried something like this but was unsucesfull.

Community
  • 1
  • 1
KuKeC
  • 4,392
  • 5
  • 31
  • 60

1 Answers1

2

Please use this method I am sure it will help you.

 public Bitmap addTextToImage(Bitmap src, String textToAddOnImage, int x, int y, int color, int alpha, int size, boolean underline) {
    int w = src.getWidth();
    int h = src.getHeight();
    Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());

    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(src, 0, 0, null);

    Paint paint = new Paint();
    paint.setColor(color);
    paint.setAlpha(alpha);
    paint.setTextSize(size);
    paint.setAntiAlias(true);
    paint.setUnderlineText(underline);
    canvas.drawText(textToAddOnImage, x, y, paint);

    return result;
}

Call this method like this

// use this bitmap to show in imageView and you can also save the bitmap.

Bitmap bmp = addTextToImage(srcBitmap, "Mustanser Iqbal", 200, 200, Color.GREEN, 80, 24, false);

File f = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + fileName + ".png");
FileOutputStream fos = new FileOutputStream(f);
bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);
Mustanser Iqbal
  • 5,017
  • 4
  • 18
  • 36
  • Can you add me one example of calling this method which should put "some text" into my picture. Did used your code, but still picture is not changed – KuKeC Nov 21 '16 at 19:00
  • Thanks it helped me. Can you tell me how to add background colour on text on picture? For example i would like to have white text in black background. – KuKeC Nov 30 '16 at 13:04
  • well just set the text color to white. – Mustanser Iqbal Dec 01 '16 at 10:35
  • You didn't understand me. I would like that my text have for example like on SO code formatting, `black letters with grey background`. How to achieve that? – KuKeC Dec 01 '16 at 12:56