Home > Tutorials > Image Processing – Watermarking On The Fly

Image Processing – Watermarking On The Fly


Having a little free time, I’ve just been digging on this technique. A little trick with canvas and drawing, I’ve got an image with watermark, so cool!!!

This is how it looks:

Image w/ Watermark

Image w/ Watermark

Here the implementation:

	public static Bitmap mark(Bitmap src, String watermark, Point location, Color 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(watermark, location.x, location.y, paint);

		return result;
	}

Hope you like it!

Cheers,
Pete Houston

  1. June 16, 2017 at 1:27 pm

    public static Bitmap mark(Bitmap src, String watermark, Point location, Color color, int alpha, int size, boolean underline)

    how to call about method as it does not show any result,i need help

    • June 16, 2017 at 1:28 pm

      how to call above method as it does not show any result,i need help please

  2. April 25, 2017 at 9:05 am

    Hi Pete I am looking for a way to set a water mark image… then as I take pictures have it put this watermark image on the picture as I take them. no post editing, just shoot photos and they will all be watermarked, with a water mark image I select ahead of time.

    Or can you point me in that direction?

    Thanks
    Dean-O

  3. June 7, 2016 at 4:41 pm

    cant save image, I have added permission external storage one, still its showing permission denied for folders present inside, and if I write newFolder, it says no such Directory find

    • June 16, 2017 at 1:30 pm

      you phone is not allowing image,there is a permission option in your phone you have to enable that

  4. Farhaan
    July 31, 2014 at 7:45 pm

    I have added watermark on image using above code. Is there any way to remove that watermark?

  5. July 18, 2014 at 2:19 pm

    hello i had saw your tutorial but dont understand what is Point location in your function ?

  6. Ankur Sharma
    July 16, 2014 at 8:54 pm

    sorry it isnt letting me to post an xml text .. u can make a edittext to enter the desired text and a button to clear the edited image .. and if u want a save button can also be added . thanks

    • Ankur Sharma
      July 16, 2014 at 8:55 pm

      also an imageview of course to ad an image frm the drawable or gallery.

  7. July 7, 2014 at 9:52 pm

    Can u give me your code? I don’t understand how to use your tutorial.

  8. November 7, 2012 at 2:20 pm

    can u show me , what Point location? and how I call this function?? thanks 🙂

    • Ankur Sharma
      July 16, 2014 at 8:50 pm

      Touch to watermark with entered text :–

      Make this class with class name TextAd.

      import android.app.Activity;
      import android.graphics.Bitmap;
      import android.graphics.BitmapFactory;
      import android.graphics.Canvas;
      import android.graphics.Color;
      import android.graphics.Paint;
      import android.os.Bundle;
      import android.util.DisplayMetrics;
      import android.view.MotionEvent;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.view.View.OnTouchListener;
      import android.view.Window;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.ImageView;

      import com.example.photogallery.MainActivity;

      public class TextAd extends Activity {

      Bitmap originalBitmap,image;
      ImageView iv_ttx;
      EditText et_sample;

      Paint paint;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.adtext);
      //image view
      iv_ttx = (ImageView) findViewById(R.id.iv_ttx);

      DisplayMetrics displaymetrics = new DisplayMetrics();

      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

      //dimentions x,y of device to create a scaled bitmap having similar dimentions to screen size

      int height1 = displaymetrics.heightPixels;
      int width1 = displaymetrics.widthPixels;

      //paint object to define paint properties

      paint = new Paint();
      paint.setStyle(Paint.Style.FILL);
      paint.setColor(Color.BLUE);
      paint.setTextSize(25);

      //loading bitmap from drawable

      originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_search);

      //scaling of bitmap

      originalBitmap =Bitmap.createScaledBitmap(originalBitmap, width1, height1, false);

      //creating anoter copy of bitmap to be used for editing

      image = originalBitmap.copy(Bitmap.Config.RGB_565, true);

      et_sample =(EditText) findViewById(R.id.et_txt);

      // Button btn_save_img = (Button) findViewById(R.id.btn_save_image);

      Button btn_clr_all = (Button) findViewById(R.id.btn_clr_all);

      btn_clr_all.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
      // TODO Auto-generated method stub
      //loading original bitmap again (undoing all editing)
      image = originalBitmap.copy(Bitmap.Config.RGB_565, true);
      iv_ttx.setImageBitmap(image);
      }
      });

      /*btn_save_img.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View arg0) {
      // TODO Auto-generated method stub
      //funtion save image is called with bitmap image as parameter
      // saveImage(image);

      }
      });
      */

      iv_ttx.setOnTouchListener(new OnTouchListener() {

      @Override
      public boolean onTouch(View arg0, MotionEvent arg1) {
      // TODO Auto-generated method stub
      String user_text=et_sample.getText().toString();
      //gettin x,y cordinates on screen touch
      float scr_x=arg1.getRawX();
      float scr_y=arg1.getRawY();
      //funtion called to perform drawing
      createImage(scr_x,scr_y,user_text);
      return true;
      }
      });

      }

      /*void saveImage(Bitmap img) {
      String RootDir = Environment.getExternalStorageDirectory()
      + File.separator + “txt_imgs”;
      File myDir=new File(RootDir);
      myDir.mkdirs();
      Random generator = new Random();
      int n = 10000;
      n = generator.nextInt(n);
      String fname = “Image-“+ n +”.jpg”;
      File file = new File (myDir, fname);
      if (file.exists ()) file.delete ();
      try {
      FileOutputStream out = new FileOutputStream(file);

      img.compress(Bitmap.CompressFormat.JPEG, 90, out);
      out.flush();
      out.close();
      } catch (Exception e) {
      e.printStackTrace();
      }
      }

      */

      public Bitmap createImage(float scr_x,float scr_y,String user_text){
      //canvas object with bitmap image as constructor
      Canvas canvas = new Canvas(image);
      int viewTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
      //removing title bar hight
      scr_y=scr_y- viewTop;
      //fuction to draw text on image. you can try more drawing funtions like oval,point,rect,etc…
      canvas.drawText(“”+user_text, scr_x, scr_y, paint);
      iv_ttx.setImageBitmap(image);
      return image;
      }

      }

      Make a layout with name adtext : —

      Touch on the screen and your text with be embeded on the image . set the image in your drawable folder and give its path in the xml. or bring it frm the gallery. enjoy coding .

      • Ankur Sharma
        July 16, 2014 at 8:52 pm

        Your layout will look like this : —

  1. No trackbacks yet.

Leave a comment