Skip to content

Commit

Permalink
add watermark blend mode setting
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Apr 12, 2024
1 parent 113fdfd commit 62e839b
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 30 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ androidCompileSdk = "34"
jvmTarget = "17"
compose-compiler = "1.5.11"

libVersion = "1.4.1"
libVersion = "1.4.2"

composeColorfulSliders = "1.2.2"
paletteKtx = "1.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.BlendMode;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Shader;
import android.os.Build;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.watermark.androidwm.bean.AsyncTaskParams;
import com.watermark.androidwm.bean.WatermarkImage;
Expand All @@ -55,7 +58,10 @@ public class Watermark {
private final Bitmap backgroundImg;
private final Context context;

private final PorterDuff.Mode porterDuffMode;
private PorterDuff.Mode porterDuffMode = PorterDuff.Mode.SRC_OVER;

@RequiresApi(29)
private BlendMode blendMode = BlendMode.SRC_OVER;
private final boolean isTileMode;
private final boolean isInvisible;
private final boolean isLSB;
Expand Down Expand Up @@ -98,6 +104,38 @@ public class Watermark {
createWatermarkTexts(wmTextList);
}

@RequiresApi(29)
Watermark(@NonNull Context context,
@NonNull Bitmap backgroundImg,
@Nullable WatermarkImage watermarkImg,
@Nullable List<WatermarkImage> wmBitmapList,
@Nullable WatermarkText inputText,
@Nullable List<WatermarkText> wmTextList,
boolean isTileMode,
boolean isInvisible,
boolean isLSB,
BlendMode blendMode,
@Nullable BuildFinishListener<Bitmap> buildFinishListener) {

this.context = context;
this.isTileMode = isTileMode;
this.watermarkImg = watermarkImg;
this.backgroundImg = backgroundImg;
this.watermarkText = inputText;
this.isInvisible = isInvisible;
this.blendMode = blendMode;
this.buildFinishListener = buildFinishListener;
this.isLSB = isLSB;

canvasBitmap = backgroundImg;
outputImage = backgroundImg;

createWatermarkImage(watermarkImg);
createWatermarkImages(wmBitmapList);
createWatermarkText(watermarkText);
createWatermarkTexts(wmTextList);
}


/**
* interface for getting the watermark bitmap.
Expand Down Expand Up @@ -136,7 +174,11 @@ private void createWatermarkImage(WatermarkImage watermarkImg) {
}
} else {
Paint watermarkPaint = new Paint();
watermarkPaint.setXfermode(new PorterDuffXfermode(porterDuffMode));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
watermarkPaint.setBlendMode(blendMode);
} else {
watermarkPaint.setXfermode(new PorterDuffXfermode(porterDuffMode));
}
watermarkPaint.setAlpha(watermarkImg.getAlpha());
Bitmap newBitmap = Bitmap.createBitmap(backgroundImg.getWidth(),
backgroundImg.getHeight(), backgroundImg.getConfig());
Expand Down Expand Up @@ -199,7 +241,11 @@ private void createWatermarkText(WatermarkText watermarkText) {
} else {
Paint watermarkPaint = new Paint();
watermarkPaint.setAlpha(watermarkText.getTextAlpha());
watermarkPaint.setXfermode(new PorterDuffXfermode(porterDuffMode));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
watermarkPaint.setBlendMode(blendMode);
} else {
watermarkPaint.setXfermode(new PorterDuffXfermode(porterDuffMode));
}
Bitmap newBitmap = Bitmap.createBitmap(backgroundImg.getWidth(),
backgroundImg.getHeight(), backgroundImg.getConfig());
Canvas watermarkCanvas = new Canvas(newBitmap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlendMode;
import android.graphics.PorterDuff;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.watermark.androidwm.bean.WatermarkImage;
import com.watermark.androidwm.bean.WatermarkPosition;
Expand Down Expand Up @@ -57,6 +60,9 @@ public final class WatermarkBuilder {

private PorterDuff.Mode porterDuffMode = PorterDuff.Mode.SRC_OVER;

@RequiresApi(29)
private BlendMode blendMode = BlendMode.SRC_OVER;

/**
* Constructors for WatermarkBuilder
*/
Expand Down Expand Up @@ -289,6 +295,12 @@ public WatermarkBuilder setPorterDuffMode(PorterDuff.Mode mode) {
return this;
}

@RequiresApi(29)
public WatermarkBuilder setBlendMode(BlendMode mode) {
this.blendMode = mode;
return this;
}

/**
* set a listener for building progress.
*/
Expand All @@ -298,19 +310,35 @@ public void setInvisibleWMListener(
) {
this.buildFinishListener = listener;
this.isLSB = isLSB;
new Watermark(
context,
backgroundImg,
watermarkImage,
watermarkBitmaps,
watermarkText,
watermarkTexts,
isTileMode,
true,
isLSB,
porterDuffMode,
buildFinishListener
);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
new Watermark(
context,
backgroundImg,
watermarkImage,
watermarkBitmaps,
watermarkText,
watermarkTexts,
isTileMode,
true,
isLSB,
blendMode,
buildFinishListener
);
} else {
new Watermark(
context,
backgroundImg,
watermarkImage,
watermarkBitmaps,
watermarkText,
watermarkTexts,
isTileMode,
true,
isLSB,
porterDuffMode,
buildFinishListener
);
}
}


Expand All @@ -337,18 +365,34 @@ private void backgroundFromImageView(ImageView imageView) {
* @return a new {@link Watermark} object
*/
public Watermark getWatermark() {
return new Watermark(
context,
backgroundImg,
watermarkImage,
watermarkBitmaps,
watermarkText,
watermarkTexts,
isTileMode,
false,
isLSB,
porterDuffMode,
buildFinishListener
);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
return new Watermark(
context,
backgroundImg,
watermarkImage,
watermarkBitmaps,
watermarkText,
watermarkTexts,
isTileMode,
false,
isLSB,
blendMode,
buildFinishListener
);
} else {
return new Watermark(
context,
backgroundImg,
watermarkImage,
watermarkBitmaps,
watermarkText,
watermarkTexts,
isTileMode,
false,
isLSB,
porterDuffMode,
buildFinishListener
);
}
}
}

0 comments on commit 62e839b

Please sign in to comment.