Skip to content

Commit

Permalink
Added ability to let the rubber stamp be a bitmap (#9)
Browse files Browse the repository at this point in the history
This PR addresses issue #8 . Also refactored code a bit in order to make it slightly cleaner, although I plan to make it more modular.
  • Loading branch information
vinaygaba authored Apr 27, 2017
1 parent f118d14 commit 5e9b280
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 82 deletions.
124 changes: 83 additions & 41 deletions library/src/main/java/com/vinaygaba/rubberstamp/RubberStamp.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.graphics.Typeface;
import android.support.annotation.DrawableRes;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Pair;

Expand Down Expand Up @@ -57,57 +58,38 @@ public RubberStamp(Context context){
}

public Bitmap addStamp(RubberStampConfig config) {
Bitmap baseBitmap = config.getBaseBitmap();
@DrawableRes int drawable = config.getBaseDrawable();

if (baseBitmap == null) {
baseBitmap = BitmapFactory.decodeResource(mContext.getResources(), drawable);
if (baseBitmap == null) return null;
}
Bitmap baseBitmap = getBaseBitmap(config);
if (baseBitmap == null) return null;

int baseBitmapWidth = baseBitmap.getWidth();
int baseBitmapHeight = baseBitmap.getHeight();

Rect bounds = new Rect();

Bitmap result = Bitmap.createBitmap(baseBitmapWidth, baseBitmapHeight, baseBitmap.getConfig());

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

Paint paint = new Paint();
paint.setTextSize(config.getSize());
paint.setColor(config.getColor());
paint.setAntiAlias(true);
paint.setUnderlineText(false);

String typeFacePath = config.getTypeFacePath();
if(!TextUtils.isEmpty(typeFacePath)) {
Typeface typeface = Typeface.createFromAsset(mContext.getAssets(), typeFacePath);
paint.setTypeface(typeface);

if (!TextUtils.isEmpty(config.getRubberStampString())) {
addTextToBitmap(config, canvas, baseBitmapWidth, baseBitmapHeight);
}

int alpha = config.getAplha();
if (alpha >= 0 && alpha <= 255) {
paint.setAlpha(alpha);
if (config.getRubberStampBitmap() != null) {
addBitmapToBitmap(config.getRubberStampBitmap(), config, canvas,
baseBitmapWidth, baseBitmapHeight);
}

Shader shader = config.getShader();
if (shader != null) paint.setShader(shader);

String rubberStampString = config.getRubberStampString();
paint.getTextBounds(rubberStampString,0,rubberStampString.length(),bounds);
int rubberStampWidth = bounds.width();
int rubberStampHeight = bounds.height();

Pair<Integer, Integer> pair = PositionCalculator
.getCoordinates(config.getRubberStampPosition(),
baseBitmapWidth, baseBitmapHeight,
rubberStampWidth, rubberStampHeight);

canvas.drawText(rubberStampString, pair.first , pair.second, paint);

return result;
}

@Nullable
private Bitmap getBaseBitmap(RubberStampConfig config) {
Bitmap baseBitmap = config.getBaseBitmap();
@DrawableRes int drawable = config.getBaseDrawable();

if (baseBitmap == null) {
baseBitmap = BitmapFactory.decodeResource(mContext.getResources(), drawable);
if (baseBitmap == null) return null;
}
return baseBitmap;
}

public Bitmap addStamp(int src, int rubberstamp, int wMarkWidth, int wMarkHeight,
@RubberStampPosition int pos) {
Expand Down Expand Up @@ -135,7 +117,7 @@ public Bitmap addStamp(int src, int rubberstamp, int wMarkWidth, int wMarkHeight
return result;
}

public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
private static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

int width = bm.getWidth();
int height = bm.getHeight();
Expand All @@ -150,4 +132,64 @@ public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

return resizedBitmap;
}

private void addTextToBitmap(RubberStampConfig config, Canvas canvas, int baseBitmapWidth,
int baseBitmapHeight) {
Rect bounds = new Rect();

Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setUnderlineText(false);

paint.setTextSize(config.getSize());
paint.setColor(config.getColor());

String typeFacePath = config.getTypeFacePath();
if(!TextUtils.isEmpty(typeFacePath)) {
Typeface typeface = Typeface.createFromAsset(mContext.getAssets(), typeFacePath);
paint.setTypeface(typeface);
}

int alpha = config.getAplha();
if (alpha >= 0 && alpha <= 255) {
paint.setAlpha(alpha);
}

Shader shader = config.getShader();
if (shader != null) paint.setShader(shader);

String rubberStampString = config.getRubberStampString();
paint.getTextBounds(rubberStampString,0,rubberStampString.length(),bounds);

int rubberStampWidth = bounds.width();
int rubberStampHeight = bounds.height();

Pair<Integer, Integer> pair = PositionCalculator
.getCoordinates(config.getRubberStampPosition(),
baseBitmapWidth, baseBitmapHeight,
rubberStampWidth, rubberStampHeight);

canvas.drawText(rubberStampString, pair.first , pair.second, paint);
}

private void addBitmapToBitmap(Bitmap rubberStampBitmap, RubberStampConfig config, Canvas canvas,
int baseBitmapWidth, int baseBitmapHeight) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setUnderlineText(false);

int alpha = config.getAplha();
if (alpha >= 0 && alpha <= 255) {
paint.setAlpha(alpha);
}

Shader shader = config.getShader();
if (shader != null) paint.setShader(shader);

Pair<Integer, Integer> pair =
PositionCalculator.getCoordinates(config.getRubberStampPosition(), baseBitmapWidth,
baseBitmapWidth, rubberStampBitmap.getWidth(), rubberStampBitmap.getHeight());
canvas.drawBitmap(rubberStampBitmap,pair.first,
pair.second - rubberStampBitmap.getHeight(), paint);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,37 @@
public class RubberStampConfig {

private Bitmap mBaseBitmap;
@DrawableRes
private int mBaseDrawable;
@DrawableRes private int mBaseDrawable;
private int mSize;
@ColorInt private int mColor;
private String mTypeFacePath;
private @RubberStamp.RubberStampPosition
int mRubberStampPosition;
private @RubberStamp.RubberStampPosition int mRubberStampPosition;
private String mRubberStampString;
private Bitmap mRubberStampBitmap;
private int mAplha;
private Shader mShader;

private RubberStampConfig(Bitmap baseBitmap, int size, int color, String typeFacePath,
int rubberStampPosition, String rubberStampString, int alpha,
Shader shader) {
this.mBaseBitmap = baseBitmap;
this.mSize = size;
this.mColor = color;
this.mTypeFacePath = typeFacePath;
this.mRubberStampPosition = rubberStampPosition;
this.mRubberStampString = rubberStampString;
this.mAplha = alpha;
this.mShader = shader;
private RubberStampConfig(RubberStampConfigBuilder builder) {
this.mBaseBitmap = builder.mBaseBitmap;
this.mBaseDrawable = builder.mBaseDrawable;
this.mSize = builder.mSize;
this.mColor = builder.mColor;
this.mTypeFacePath = builder.mTypeFacePath;
this.mRubberStampPosition = builder.mRubberStampPosition;
this.mRubberStampString = builder.mRubberStampString;
this.mRubberStampBitmap = builder.mRubberStampBitmap;
this.mAplha = builder.mAlpha;
this.mShader = builder.mShader;
}

private RubberStampConfig(@DrawableRes int baseDrawable, int size, int mColor,
String typeFacePath, int rubberStampPosition, String rubberStampString,
int alpha, Shader shader) {
this.mBaseDrawable = baseDrawable;
this.mSize = size;
this.mColor = mColor;
this.mTypeFacePath = typeFacePath;
this.mRubberStampPosition = rubberStampPosition;
this.mRubberStampString = rubberStampString;
this.mAplha = alpha;
this.mShader = shader;
}


public Bitmap getBaseBitmap() {
return mBaseBitmap;
}

protected int getBaseDrawable() {
return mBaseDrawable;
}

public int getSize() {
return mSize;
}
Expand All @@ -72,8 +62,8 @@ public String getRubberStampString() {
return mRubberStampString;
}

public int getBaseDrawable() {
return mBaseDrawable;
protected Bitmap getRubberStampBitmap() {
return mRubberStampBitmap;
}

public int getAplha() {
Expand All @@ -94,15 +84,16 @@ public static class RubberStampConfigBuilder {
private @RubberStamp.RubberStampPosition
int mRubberStampPosition = CENTER;
private String mRubberStampString;
private Bitmap mRubberStampBitmap;
private int mAlpha = 255;
private Shader mShader;

public RubberStampConfigBuilder bitmap(final Bitmap bitmap) {
public RubberStampConfigBuilder base(final Bitmap bitmap) {
this.mBaseBitmap = bitmap;
return this;
}

public RubberStampConfigBuilder drawable(@DrawableRes int drawable) {
public RubberStampConfigBuilder base(@DrawableRes int drawable) {
this.mBaseDrawable = drawable;
return this;
}
Expand All @@ -128,11 +119,16 @@ public RubberStampConfigBuilder rubberStampPosition(final @RubberStamp.RubberSta
return this;
}

public RubberStampConfigBuilder rubberStampString(final String rubberStampString) {
public RubberStampConfigBuilder rubberStamp(final String rubberStampString) {
this.mRubberStampString = rubberStampString;
return this;
}

public RubberStampConfigBuilder rubberStamp(final Bitmap rubberStampBitmap) {
this.mRubberStampBitmap = rubberStampBitmap;
return this;
}

public RubberStampConfigBuilder alpha(final int alpha) {
this.mAlpha = alpha;
return this;
Expand All @@ -144,11 +140,7 @@ public RubberStampConfigBuilder alpha(final Shader shader) {
}

public RubberStampConfig build() {
return mBaseBitmap != null ?
new RubberStampConfig(mBaseBitmap, mSize, mColor, mTypeFacePath,
mRubberStampPosition, mRubberStampString, mAlpha, mShader) :
new RubberStampConfig(mBaseDrawable, mSize, mColor, mTypeFacePath,
mRubberStampPosition, mRubberStampString, mAlpha, mShader);
return new RubberStampConfig(this);
}
}
}
4 changes: 2 additions & 2 deletions sample/src/main/java/com/vinaygaba/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ protected void onCreate(Bundle savedInstanceState) {
R.drawable.card);
RubberStamp rubberStamp = new RubberStamp(this);
RubberStampConfig config = new RubberStampConfigBuilder()
.rubberStampString("Test")
.bitmap(icon)
.base(icon)
.rubberStamp("Test")
.size(50)
.color(Color.RED)
.rubberStampPosition(RubberStamp.BOTTOMRIGHT)
Expand Down

0 comments on commit 5e9b280

Please sign in to comment.