Ad Code

Add watermark to Image Android Programmatically

Hello Developers, in this Article I will provide you the documentation of how to Add watermark to image android programmatically. Adding a watermark to an image is a common requirement in many Android applications, such as photo editing apps, social media apps, or document scanning apps. Watermarking an image adds a visible overlay that typically includes text or a logo, which identifies the source or ownership of the image. In this article, we will explore how to add a watermark to an image programmatically in Android.



Add watermark to image android Programmatically.

There are various approaches to add a watermark to an image in Android, but one common method is to use the Android graphics framework to draw the watermark on a bitmap.

ImageUtils.java
// add your package  here

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class ImageUtils {

    //Here we have used paint for adding watermark in the Image Bitmap
    private static Bitmap addWatermark(Context context, Bitmap source, String watermarkText, float textSize, int textColor, float x, float y) {
        // Create a mutable bitmap from the source image
        Bitmap bitmap = source.copy(Bitmap.Config.ARGB_8888, true);

        // Create a canvas from the bitmap to draw the watermark
        Canvas canvas = new Canvas(bitmap);

        // Create a paint object to set the watermark properties
        Paint paint = new Paint();
        paint.setColor(textColor);
        paint.setTextSize(textSize);
        paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
        paint.setAntiAlias(true);
        paint.setAlpha(128);

        // Measure the size of the watermark text
        Rect bounds = new Rect();
        paint.getTextBounds(watermarkText, 0, watermarkText.length(), bounds);
        float textWidth = paint.measureText(watermarkText);
        float textHeight = bounds.height();

        // Calculate the coordinates to place the watermark
        float xPos = x * bitmap.getWidth() - textWidth / 2;
        float yPos = y * bitmap.getHeight() + textHeight / 2;

        // Draw the watermark text on the canvas
        canvas.drawText(watermarkText, xPos, yPos, paint);

        return bitmap;
    }
    
    

    public static Drawable addWatermark(Context context, Drawable source, String watermarkText, float textSize, int textColor, float x, float y) {
        // Convert the drawable to a bitmap
        BitmapDrawable bitmapDrawable = (BitmapDrawable) source;
        Bitmap sourceBitmap = bitmapDrawable.getBitmap();

        // Add watermark to the bitmap
        Bitmap watermarkBitmap = addWatermark(context, sourceBitmap, watermarkText, textSize, textColor, x, y);

        // Convert the bitmap back to a drawable
        return new BitmapDrawable(context.getResources(), watermarkBitmap);
    }



}

You can use this utility class ImageUtils to add a text watermark to a Bitmap or a Drawable in Android. The addWatermark() method takes the source image (either a Bitmap or a Drawable), the watermark text, text size, text color, and the coordinates to place the watermark on the image (x and y as percentages of the image size). It returns a new Bitmap with the watermark added, which you can then use in your application as needed.

Here is the usages of this ImageUtis.java class.

Java
        Context context = MainActivity.this;
        ImageView imageView = findViewById(R.id.imageView);
        
        //x and y refers to the position of watermark
        //Don't forget to check if drawable not equals to null
        // add text size and text colors
        
        Drawable drawable = addWatermark(context, imageView.getDrawable(), "ReckLet Developer", 15f, Color.BLACK, 10, 10);
        
        // Now set the drawable to ImageView
        
        imageView.setImageDrawable(drawable);

Adding a watermark to your images can help you protect your content, add branding, or provide attribution to your images. With the utility class ImageUtils and the example usage provided in this article, you can easily implement image watermarking in your Android application.

It's important to note that adding a visible watermark does not guarantee complete protection against unauthorized use or copying of images. It's always recommended to use additional measures, such as copyright notices and legal disclaimers, to protect your images and ensure compliance with relevant laws and regulations.

Furthermore, it's essential to strike a balance between the visibility and intrusiveness of the watermark. A watermark that is too large, opaque, or obtrusive may negatively impact the aesthetics and usability of the image, while a watermark that is too small or transparent may not effectively serve its purpose. Therefore, it's crucial to carefully design and test your watermark to find the right balance.

In addition to text watermarks, you can also explore other watermarking techniques, such as image logos, patterns, or QR codes, depending on your use case and requirements. Remember to always comply with applicable laws and obtain necessary permissions or licenses for using any copyrighted materials or trademarks in your watermarks.

In conclusion, adding a watermark to an image programmatically in Android is a practical way to protect your images and add branding or attribution. By utilizing the ImageUtils utility class and customizing it to your needs, you can easily implement image watermarking in your Android application. Remember to consider the visibility and intrusiveness of the watermark, comply with relevant laws and regulations, and choose the right type of watermark for your specific use case.

Thank you for visiting our website.

Post a Comment

0 Comments

Ad Code