Search code examples
androidandroid-camera-intent

Add plain text to picture taken from camera intent


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.


Solution

  • 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);