Creating Watermark While Taking Screenshot Of A Screen In Android Application

In this article you will learn about how to create watermark while taking screenshot of a screen in Android application.

Introduction

Generally we take the screenshot of some screen to capture the data in its actual format like saving screen that has some transaction id, application having some bug in it or it may be anything that we want to capture quickly in its actual looking format and after taking screenshot it is saved in a directory. (\Pictures\Screenshots).

Here, I am sharing how can we get notified after taking screenshot and how can we draw a watermark in taken screenshot.

What we need to know?

  • Creating an listener to get notified when screen shot captured.
  • Drawing a text on the captured image.

Creating an listener to get notified when screen shot captured

As we know there in no any broadcast receiver in Android to notify when screenshot captured so we have to apply some trick to do so. We will use FileObserver to get notified when the changes occurred in specified directory (.\Pictures\Screenshots).

Drawing a text on the captured image

We use Canvas and Paint to draw watermark in the captured image.

Steps to create a small application to take screenshot and create watermark

  1. Create an interface with a method declaration that will be implemented by the Activity that will be captured.
    1. package com.example.atiwari.screenshotdemo;  
    2.   
    3. import android.net.Uri;  
    4.   
    5. /** 
    6.  * Created by atiwari on 4/28/2016. 
    7.  */  
    8. public interface OnScreenshotTakenListener {  
    9.     void onScreenshotTaken(Uri uri);  
    10. }  
  2. Create a class that will extend FileObserver and override onEvent (int event,String path) , stopWatching(), startWatching() methods. In onEvent() method we get a path of the file that is captured and we call listener's callback method onScreenShotTaken(Uri uri).
    1. package com.example.atiwari.screenshotdemo;  
    2. import android.net.Uri;  
    3. import android.os.Environment;  
    4. import android.os.FileObserver;  
    5. import android.util.Log;  
    6.   
    7. import java.io.File;  
    8.   
    9. /** 
    10.  * Created by atiwari on 4/28/2016. 
    11.  */  
    12. public class ScreenshotObserver extends FileObserver {  
    13.   
    14.     private static final String TAG = "ScreenshotObserver";  
    15.     private static final String PATH = Environment.getExternalStorageDirectory().toString() + "/Pictures/Screenshots/";  
    16.   
    17.     private OnScreenshotTakenListener mListener;  
    18.     private String mLastTakenPath;  
    19.   
    20.     public ScreenshotObserver(OnScreenshotTakenListener listener) {  
    21.         super(PATH, FileObserver.CLOSE_WRITE);  
    22.         mListener = listener;  
    23.     }  
    24.   
    25.     @Override  
    26.     public void onEvent(int event, String path) {  
    27.   
    28.         Log.i(TAG, "Event:" + event + "\t" + path);  
    29.           
    30.         mLastTakenPath = path;  
    31.         File file = new File(PATH + path);  
    32.         mListener.onScreenshotTaken(Uri.fromFile(file));  
    33.   
    34.     }  
    35.   
    36.     @Override  
    37.     public void stopWatching() {  
    38.         super.stopWatching();  
    39.     }  
    40.   
    41.     @Override  
    42.     public void startWatching() {  
    43.         super.startWatching();  
    44.     }  
    45.   
    46.   
    47. }  
  3. Now we come to our activity and implement OnScreenShotTakenListener that we have created in first step, and override onScreenshotTaken() method.

    • We create a bitmap from URI we have.
    • Create a point where we have to draw watermark.
    • We can create typeface also if we want to apply
    • We create a canvas from bitmap we have and use a paint object to create text in canvas.
    • Finally we save the bitmap on same location/directory.
    1. package com.example.atiwari.screenshotdemo;   
      package com.example.atiwari.screenshotdemo;  
    2.   
    3. import android.graphics.Bitmap;  
    4. import android.graphics.Canvas;  
    5. import android.graphics.Color;  
    6. import android.graphics.Paint;  
    7. import android.graphics.Point;  
    8. import android.graphics.Typeface;  
    9. import android.net.Uri;  
    10. import android.provider.MediaStore;  
    11. import android.support.v7.app.AppCompatActivity;  
    12. import android.os.Bundle;  
    13. import android.util.Log;  
    14. import java.io.File;  
    15. import java.io.FileOutputStream;  
    16. import java.io.IOException;  
    17.   
    18. public class MainActivity extends AppCompatActivity implements OnScreenshotTakenListener {  
    19.   
    20.     private ScreenshotObserver obs;  
    21.   
    22.     @Override  
    23.     protected void onCreate(Bundle savedInstanceState) {  
    24.         super.onCreate(savedInstanceState);  
    25.   
    26.         obs = new ScreenshotObserver(this);  
    27.         obs.startWatching();  
    28.   
    29.         setContentView(R.layout.activity_main);  
    30.     }  
    31.   
    32.     @Override  
    33.     public void onScreenshotTaken(Uri uri) {  
    34.   
    35.         Log.v("MainActivity"" URI: " + uri);  
    36.         Log.v("MainActivity"" PATH: " + uri.getPath());  
    37.   
    38.         Bitmap bitmap = createBitmapFromUri(uri);  
    39.   
    40.         Point point = new Point(200, 200);  
    41.   
    42.         // Creating type face  
    43.         Typeface plain = Typeface.createFromAsset(getAssets(), "fonts/brotherDeluxe1350.ttf");  
    44.         Typeface bold = Typeface.create(plain, Typeface.BOLD);  
    45.   
    46.   
    47.         Bitmap newBitmap = mark(bitmap, getApplicationName(), point, Color.GRAY, 1, 50, true, bold);  
    48.   
    49.         try {  
    50.             FileOutputStream fos = new FileOutputStream(new File(uri.getPath()));  
    51.             newBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);  
    52.         } catch (IOException e) {  
    53.             e.printStackTrace();  
    54.         }  
    55.     }  
    56.   
    57.   
    58.     // Method to draw watermark  
    59.     public static Bitmap mark(Bitmap src, String watermark, Point location, int color, int alpha, int size, boolean underline, Typeface typeface) {  
    60.   
    61.         int w = src.getWidth();  
    62.         int h = src.getHeight();  
    63.         Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());  
    64.   
    65.         Canvas canvas = new Canvas(result);  
    66.         canvas.drawBitmap(src, 0, 0, null);  
    67.   
    68.   
    69.         Paint paint = new Paint();  
    70.         paint.setColor(color);  
    71.         paint.setAlpha(alpha);  
    72.         paint.setStyle(Paint.Style.FILL);  
    73.         paint.setTextSize(size);  
    74.         paint.setAntiAlias(true);  
    75.         paint.setUnderlineText(underline);  
    76.         paint.setTypeface(typeface);  
    77.         canvas.drawText(watermark, location.x, location.y, paint);  
    78.   
    79.         return result;  
    80.     }  
    81.   
    82.     // Method to create bitmap from uri  
    83.     private Bitmap createBitmapFromUri(Uri uri) {  
    84.         Bitmap bitmap = null;  
    85.         try {  
    86.             bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);  
    87.         } catch (IOException e) {  
    88.             e.printStackTrace();  
    89.         }  
    90.   
    91.         return bitmap;  
    92.     }  
    93.   
    94.     private String getApplicationName() {  
    95.         return getApplicationInfo().loadLabel(getPackageManager()).toString();  
    96.     }  
    97. }  
    Output Screen

    output

Read more articles on Android: