Creating 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, the application has some bug in it or it may be anything that we want to capture quickly in its actual looking format and after taking a screenshot it is saved in a directory. (\Pictures\Screenshots).

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

What do we need to know?

  • Creating a listener to get notified when the screenshot captured.
  • Drawing a text on the captured image.

Creating a listener to get notified when screenshot captured

As we know there is no 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 the specified directory (.\Pictures\Screenshots).

Drawing a text on the captured image

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

Steps to create a small application to take a 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. }  
  3. Now we come to our activity and implement OnScreenShotTakenListener that we have created in the first step, and override onScreenshotTaken() method.

    • We create a bitmap from URI we have.
    • Create a point where we have to draw a 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 the 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.     // Method to draw watermark  
    57.     public static Bitmap mark(Bitmap src, String watermark, Point location, int color, int alpha, int size, boolean underline, Typeface typeface) {  
    58.   
    59.         int w = src.getWidth();  
    60.         int h = src.getHeight();  
    61.         Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());  
    62.   
    63.         Canvas canvas = new Canvas(result);  
    64.         canvas.drawBitmap(src, 0, 0, null);  
    65.         Paint paint = new Paint();  
    66.         paint.setColor(color);  
    67.         paint.setAlpha(alpha);  
    68.         paint.setStyle(Paint.Style.FILL);  
    69.         paint.setTextSize(size);  
    70.         paint.setAntiAlias(true);  
    71.         paint.setUnderlineText(underline);  
    72.         paint.setTypeface(typeface);  
    73.         canvas.drawText(watermark, location.x, location.y, paint);  
    74.   
    75.         return result;  
    76.     }  
    77.   
    78.     // Method to create bitmap from uri  
    79.     private Bitmap createBitmapFromUri(Uri uri) {  
    80.         Bitmap bitmap = null;  
    81.         try {  
    82.             bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);  
    83.         } catch (IOException e) {  
    84.             e.printStackTrace();  
    85.         }  
    86.   
    87.         return bitmap;  
    88.     }  
    89.   
    90.     private String getApplicationName() {  
    91.         return getApplicationInfo().loadLabel(getPackageManager()).toString();  
    92.     }  
    93. }  
    Output Screen

    output

Read more articles on Android


Similar Articles