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
- Create an interface with a method declaration that will be implemented by the Activity that will be captured.
- package com.example.atiwari.screenshotdemo;
-
- import android.net.Uri;
-
-
-
-
- public interface OnScreenshotTakenListener {
- void onScreenshotTaken(Uri uri);
- }
- 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).
- package com.example.atiwari.screenshotdemo;
- import android.net.Uri;
- import android.os.Environment;
- import android.os.FileObserver;
- import android.util.Log;
-
- import java.io.File;
-
-
-
-
- public class ScreenshotObserver extends FileObserver {
-
- private static final String TAG = "ScreenshotObserver";
- private static final String PATH = Environment.getExternalStorageDirectory().toString() + "/Pictures/Screenshots/";
-
- private OnScreenshotTakenListener mListener;
- private String mLastTakenPath;
-
- public ScreenshotObserver(OnScreenshotTakenListener listener) {
- super(PATH, FileObserver.CLOSE_WRITE);
- mListener = listener;
- }
-
- @Override
- public void onEvent(int event, String path) {
-
- Log.i(TAG, "Event:" + event + "\t" + path);
-
- mLastTakenPath = path;
- File file = new File(PATH + path);
- mListener.onScreenshotTaken(Uri.fromFile(file));
-
- }
-
- @Override
- public void stopWatching() {
- super.stopWatching();
- }
-
- @Override
- public void startWatching() {
- super.startWatching();
- }
-
-
- }
- 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.
- package com.example.atiwari.screenshotdemo;
package com.example.atiwari.screenshotdemo; -
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Point;
- import android.graphics.Typeface;
- import android.net.Uri;
- import android.provider.MediaStore;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
-
- public class MainActivity extends AppCompatActivity implements OnScreenshotTakenListener {
-
- private ScreenshotObserver obs;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- obs = new ScreenshotObserver(this);
- obs.startWatching();
-
- setContentView(R.layout.activity_main);
- }
-
- @Override
- public void onScreenshotTaken(Uri uri) {
-
- Log.v("MainActivity", " URI: " + uri);
- Log.v("MainActivity", " PATH: " + uri.getPath());
-
- Bitmap bitmap = createBitmapFromUri(uri);
-
- Point point = new Point(200, 200);
-
-
- Typeface plain = Typeface.createFromAsset(getAssets(), "fonts/brotherDeluxe1350.ttf");
- Typeface bold = Typeface.create(plain, Typeface.BOLD);
-
-
- Bitmap newBitmap = mark(bitmap, getApplicationName(), point, Color.GRAY, 1, 50, true, bold);
-
- try {
- FileOutputStream fos = new FileOutputStream(new File(uri.getPath()));
- newBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
-
-
- public static Bitmap mark(Bitmap src, String watermark, Point location, int color, int alpha, int size, boolean underline, Typeface typeface) {
-
- 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.setStyle(Paint.Style.FILL);
- paint.setTextSize(size);
- paint.setAntiAlias(true);
- paint.setUnderlineText(underline);
- paint.setTypeface(typeface);
- canvas.drawText(watermark, location.x, location.y, paint);
-
- return result;
- }
-
-
- private Bitmap createBitmapFromUri(Uri uri) {
- Bitmap bitmap = null;
- try {
- bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- return bitmap;
- }
-
- private String getApplicationName() {
- return getApplicationInfo().loadLabel(getPackageManager()).toString();
- }
- }
Output Screen
Read more articles on Android: