Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ private void markImageByText(
String fontName,
Integer fontSize,
ShadowLayerStyle shadowLayerStyle,
TextBackgroundStyle textBackgroundStyle,
Integer X,
Integer Y,
int quality,
Expand Down Expand Up @@ -332,25 +333,26 @@ private void markImageByText(
textWidth = (int) Math.ceil(Math.max(textWidth, textLayout.getLineWidth(a) + textLayout.getLineLeft(a)));
}

float x = 20;
float y = 20;
int margin = 20;
float x = margin;
float y = margin;

if (position != null) {
if("topCenter".equals(position)) {
x = (width - textWidth)/2;
} else if("topRight".equals(position)) {
x = (width - textWidth);
x = width - textWidth - margin;
} else if("center".equals(position)) {
x = (width - textWidth) / 2;
y = (height - textHeight) / 2;
} else if("bottomLeft".equals(position)) {
y = (height - textHeight);
y = height - textHeight - margin;
} else if("bottomCenter".equals(position)) {
x = (width - textWidth) / 2;
y = (height - textHeight);
} else if("bottomRight".equals(position)) {
x = (width - textWidth);
y = (height - textHeight);
x = width - textWidth - margin;
y = height - textHeight - margin;
}
} else {
if (null != X) {
Expand All @@ -361,6 +363,20 @@ private void markImageByText(
}
}

// Draw text background
if(null != textBackgroundStyle) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
paint.setStyle(Paint.Style.FILL);
paint.setColor(textBackgroundStyle.color);
if("stretchX".equals(textBackgroundStyle.type)) {
canvas.drawRect(0, y - textBackgroundStyle.paddingY, width, y + textHeight + textBackgroundStyle.paddingY, paint);
} else if("stretchY".equals(textBackgroundStyle.type)) {
canvas.drawRect(x - textBackgroundStyle.paddingX, 0, x + textWidth + textBackgroundStyle.paddingX, height, paint);
} else {
canvas.drawRect(x - textBackgroundStyle.paddingX, y - textBackgroundStyle.paddingY,
x + textWidth + textBackgroundStyle.paddingX, y + textHeight + textBackgroundStyle.paddingY, paint);
}
}
canvas.save();
canvas.translate(x, y);
textLayout.draw(canvas);
Expand Down Expand Up @@ -413,6 +429,7 @@ public void addText(
final String fontName,
final Integer fontSize,
ReadableMap shadowStyle,
ReadableMap textBackgroundStyle,
final float scale,
final int quality,
String filename,
Expand All @@ -429,6 +446,7 @@ public void addText(
final String dest = generateCacheFilePathForMarker(uri, filename);

final ShadowLayerStyle myShadowStyle = null != shadowStyle? new ShadowLayerStyle(shadowStyle) : null;
final TextBackgroundStyle myTextBackgroundStyle = null != textBackgroundStyle ? new TextBackgroundStyle(textBackgroundStyle) : null;

Log.d(IMAGE_MARKER_TAG, uri);
Log.d(IMAGE_MARKER_TAG, src.toString());
Expand All @@ -442,7 +460,7 @@ public void addText(
public void onNewResultImpl(@Nullable Bitmap bitmap) {
if (bitmap != null) {
Bitmap bg = Utils.scaleBitmap(bitmap, scale);
markImageByText(bg, mark, null, color, fontName, fontSize, myShadowStyle, X, Y, quality, dest, promise);
markImageByText(bg, mark, null, color, fontName, fontSize, myShadowStyle, myTextBackgroundStyle, X, Y, quality, dest, promise);
} else {
promise.reject( "marker error","Can't retrieve the file from the src: " + uri);
}
Expand Down Expand Up @@ -473,7 +491,7 @@ public void onFailureImpl(DataSource dataSource) {
bitmap.recycle();
System.gc();
}
markImageByText(bg, mark, null, color, fontName, fontSize, myShadowStyle, X, Y, quality, dest, promise);
markImageByText(bg, mark, null, color, fontName, fontSize, myShadowStyle, myTextBackgroundStyle, X, Y, quality, dest, promise);
}
}
} catch (Exception e) {
Expand Down Expand Up @@ -502,6 +520,7 @@ public void addTextByPostion(
final String fontName,
final Integer fontSize,
ReadableMap shadowStyle,
ReadableMap textBackgroundStyle,
final float scale,
final Integer quality,
String filename,
Expand All @@ -517,6 +536,7 @@ public void addTextByPostion(
final String dest = generateCacheFilePathForMarker(uri, filename);

final ShadowLayerStyle myShadowStyle = null != shadowStyle? new ShadowLayerStyle(shadowStyle) : null;
final TextBackgroundStyle myTextBackgroundStyle = null != textBackgroundStyle ? new TextBackgroundStyle(textBackgroundStyle) : null;


Log.d(IMAGE_MARKER_TAG, uri);
Expand All @@ -531,7 +551,7 @@ public void addTextByPostion(
public void onNewResultImpl(@Nullable Bitmap bitmap) {
if (bitmap != null) {
Bitmap bg = Utils.scaleBitmap(bitmap, scale);
markImageByText(bg, mark, position, color, fontName, fontSize, myShadowStyle,null, null, quality, dest, promise);
markImageByText(bg, mark, position, color, fontName, fontSize, myShadowStyle, myTextBackgroundStyle, null, null, quality, dest, promise);
} else {
promise.reject( "marker error","Can't retrieve the file from the src: " + uri);
}
Expand Down Expand Up @@ -562,7 +582,7 @@ public void onFailureImpl(DataSource dataSource) {
bitmap.recycle();
System.gc();
}
markImageByText(bg, mark, position, color, fontName, fontSize, myShadowStyle, null, null, quality, dest, promise);
markImageByText(bg, mark, position, color, fontName, fontSize, myShadowStyle, myTextBackgroundStyle, null, null, quality, dest, promise);
}
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.jimmydaddy.imagemarker;

import android.graphics.Color;
import android.util.Log;
import com.facebook.react.bridge.ReadableMap;

public class TextBackgroundStyle {
public String type = "";
public float paddingX = 0F;
public float paddingY = 0F;
public int color = Color.TRANSPARENT;

public TextBackgroundStyle(String type, float paddingX, float paddingY, int color) {
this.type = type;
this.paddingX = paddingX;
this.paddingY = paddingY;
this.color = color;
}

public TextBackgroundStyle(ReadableMap readableMap) {
if (null != readableMap) {
try {
this.setType(readableMap.getString("type"));
this.setPaddingX((float) readableMap.getDouble("paddingX"));
this.setPaddingY((float) readableMap.getDouble("paddingY"));
this.setColor(readableMap.getString("color"));
} catch (Exception e) {
Log.d(Utils.TAG, "Unknown text background options ", e);
}

}
}

public String getType() {
return type;
}

public float getPaddingX() {
return paddingX;
}

public float getPaddingY() {
return paddingY;
}

public int getColor() {
return color;
}

public void setType(String type) {
this.type = type;
}

public void setPaddingX(float paddingX) {
this.paddingX = paddingX;
}

public void setPaddingY(float paddingY) {
this.paddingY = paddingY;
}

public void setColor(int color) {
this.color = color;
}

public void setColor(String color) {
try {
Integer parsedColor = Color.parseColor(color);
if (null != parsedColor) {
this.setColor(parsedColor);
}
} catch (Exception e) {
Log.d(Utils.TAG, "Unknown color string ", e);
}

}
}
14 changes: 13 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ export type ShadowLayerStyle = {
'color': string
}

export type TextBackgroundStyle = {
'paddingX': float,
'paddingY': float,
'type': string,
'color': string
}

type TextMarkOption = {
// image src, local image
src: string,
Expand All @@ -45,7 +52,8 @@ type TextMarkOption = {
quality: number,
position?: Position,
filename?: string,
shadowStyle: ShadowLayerStyle
shadowStyle: ShadowLayerStyle,
textBackgroundStyle: TextBackgroundStyle
}

type ImageMarkOption = {
Expand Down Expand Up @@ -74,6 +82,7 @@ export default class Marker {
fontName,
fontSize,
shadowStyle,
textBackgroundStyle,
scale,
quality,
position,
Expand All @@ -93,6 +102,7 @@ export default class Marker {
}

let mShadowStyle = shadowStyle || {}
let mTextBackgroundStyle = textBackgroundStyle || {}

if (!position) {
return ImageMarker.addText(
Expand All @@ -104,6 +114,7 @@ export default class Marker {
fontName,
fontSize,
mShadowStyle,
mTextBackgroundStyle,
scale,
quality,
filename
Expand All @@ -117,6 +128,7 @@ export default class Marker {
fontName,
fontSize,
mShadowStyle,
mTextBackgroundStyle,
scale,
quality,
filename
Expand Down
7 changes: 7 additions & 0 deletions ios/RCTImageMarker/RCTImageMarker.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@
@interface ImageMarker : NSObject <RCTBridgeModule>

@end

@interface TextBackground : NSObject
@property (copy) UIColor * colorBg;
@property (copy) NSString * typeBg;
@property (assign) float paddingX;
@property (assign) float paddingY;
@end
Loading