Created
September 11, 2019 02:37
-
-
Save mjm918/3c56a5753742a814d4127d7f0fe559af to your computer and use it in GitHub Desktop.
React Native Android watermark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CreateImage extends ReactContextBaseJavaModule { | |
private String finalPath = ""; | |
private ReactApplicationContext reactApplicationContext = null; | |
public CreateImage(ReactApplicationContext reactContext) { | |
super(reactContext); | |
this.reactApplicationContext = reactContext; | |
} | |
@Override | |
public String getName() { | |
return "CreateImage"; | |
} | |
@ReactMethod | |
public void imagePath(String imagePath, String watermark, Callback newPath) { | |
String tmpPath = String.valueOf(imagePath).replace("file://",""); | |
Bitmap backImage = null, originalImg = null; | |
BitmapFactory.Options bfOptions=new BitmapFactory.Options(); | |
bfOptions.inDither=false; | |
bfOptions.inPurgeable=true; | |
bfOptions.inInputShareable=true; | |
bfOptions.inTempStorage=new byte[32 * 1024]; | |
File file=new File(tmpPath); | |
FileInputStream fs=null; | |
try { | |
fs = new FileInputStream(file); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
try { | |
if(fs!=null) originalImg = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally{ | |
if(fs!=null) { | |
try { | |
fs.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
int width = bfOptions.outWidth; | |
int height = bfOptions.outHeight; | |
int reserved = 100; | |
float fontSize = 17.f; | |
if(width == 0){ | |
return; | |
} | |
int minimalRate = 2; | |
if(width / 1000 > 2){ | |
minimalRate = width / 1000; | |
fontSize += minimalRate; | |
} | |
int todoWidth = width / minimalRate; | |
int todoHeight = height / minimalRate; | |
if(height < 1000 || width < 1000){ | |
todoWidth = width; | |
todoHeight = height; | |
fontSize = 14.f; | |
} | |
int canvasheight = todoHeight + reserved; | |
backImage = Bitmap.createBitmap(todoWidth, canvasheight, Bitmap.Config.ARGB_8888); | |
Canvas canvas = new Canvas(backImage); | |
Paint paint = new Paint(); | |
paint.setColor(Color.BLACK); | |
paint.setStyle(Paint.Style.FILL); | |
canvas.drawPaint(paint); | |
TextPaint mTextPaint=new TextPaint(); | |
mTextPaint.setColor(Color.WHITE); | |
mTextPaint.setAntiAlias(true); | |
mTextPaint.setTextAlign(Paint.Align.LEFT); | |
mTextPaint.setTextSize(fontSize); | |
StaticLayout mTextLayout = new StaticLayout(watermark, mTextPaint, canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); | |
canvas.save(); | |
canvas.translate(10, todoHeight+10); | |
mTextLayout.draw(canvas); | |
canvas.restore(); | |
Bitmap mutableBitmap = backImage.copy(Bitmap.Config.ARGB_8888, true); | |
backImage = null; | |
if(originalImg != null){ | |
Canvas _canvas = new Canvas(mutableBitmap); | |
paint = new Paint(); | |
paint.setColor(Color.WHITE); | |
_canvas.drawBitmap(Bitmap.createScaledBitmap(originalImg,todoWidth,todoHeight,false), 0, 0, paint); | |
this.finalPath = saveInternalStorage(mutableBitmap); | |
newPath.invoke("file://"+this.finalPath,todoWidth,todoHeight); | |
}else{ | |
newPath.invoke(imagePath,width,height); | |
} | |
} | |
private String saveInternalStorage(Bitmap bitmap) { | |
String stored = null; | |
File sdcard = this.reactApplicationContext.getFilesDir(); | |
File folder = new File(sdcard.getAbsoluteFile(), "/tmp/"); | |
folder.mkdir(); | |
File file = new File(folder.getAbsoluteFile(), "temp.jpg") ; | |
if (file.exists()){ | |
file.delete(); | |
} | |
try { | |
FileOutputStream out = new FileOutputStream(file); | |
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); | |
out.flush(); | |
out.close(); | |
stored = file.getAbsolutePath(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return stored; | |
} | |
@Nullable | |
@Override | |
public Map<String, Object> getConstants() { | |
final Map<String, Object> constants = new HashMap<>(); | |
constants.put("path", finalPath); | |
return constants; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RNCreateImage implements ReactPackage { | |
@Nonnull | |
@Override | |
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) { | |
List<NativeModule> modules = new ArrayList<>(); | |
modules.add(new CreateImage(reactContext)); | |
return modules; | |
} | |
@Nonnull | |
@Override | |
public List<ViewManager> createViewManagers(@Nonnull ReactApplicationContext reactContext) { | |
return Collections.emptyList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment