0

I am new for android and i want to add watermark effect on captured images and i am trying find the solution for it since 2 days but still not able to add watermark effect on captured images can some one help me what i did mistack in my below code..

I want add water mark effect on bottom middle of image

Code

private void onCaptureImageResult(Intent data) {
            Bitmap photo;Bitmap watermark_bitmap=null;
            String selectedFilePath = null;
            if (data.getExtras().get("data") != null) {
                photo = (Bitmap) data.getExtras().get("data");
                watermark_bitmap=Utilities.mark(this,photo,"Hello World",true);
                Uri contentUri = Utilities.getImageUri1(
                        this, watermark_bitmap);
                selectedFilePath = ImageFilePath.getPath(
                        this, contentUri);
                newMeter_image.setImageBitmap(watermark_bitmap); 
           }



 public static Bitmap mark(Context context,Bitmap src, String watermark, 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();
        int color = ContextCompat.getColor(context, R.color.black);
        paint.setColor(color);
        paint.setAlpha(1);
        paint.setTextSize(5);
        paint.setAntiAlias(true);
        paint.setUnderlineText(underline);
        canvas.drawText(watermark, 50,50, paint);
        return result;
    }
Krish
  • 4,166
  • 11
  • 58
  • 110
  • 2
    Does this answer your question? [How to draw text On image?](https://stackoverflow.com/questions/7320392/how-to-draw-text-on-image) – kelvin Feb 15 '21 at 09:53

1 Answers1

1

I would have also used the same approach.

  1. Load the image in a canvas.

    Canvas canvas = new Canvas(result); canvas.drawBitmap(src, 0, 0, null);

  2. Set the paint and draw the text/drawable/bitmap of your watermark at the location you desire.

  3. Instead of using hardcoded values (50,50) try using calculated values.

    canvas.drawText("Watermark",canvas.width/2, canvas.height/2, paint)

This will make the watermark as a part of your image. If you want to keep it separate but create an illusion of watermark, then you can display your watermark as src in an imageView and overlay it over your actual image display view.

Sorry if I misunderstood. Do share a better solution though!