Skip to content

Commit 87143b8

Browse files
committed
fix: fix #49 #42 #29
1 parent bcbfeaf commit 87143b8

File tree

25 files changed

+1033
-1218
lines changed

25 files changed

+1033
-1218
lines changed

README.MD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Add text or icon watermark to your images
1818

1919
## Installation
2020

21+
**RN version < 0.60.0 please use v0.5.2 or older**
22+
2123
* npm install react-native-image-marker --save
2224
* react-native link
2325

@@ -54,6 +56,7 @@ pod 'RNImageMarker', :path => '../node_modules/react-native-image-marker'
5456
|`filename` | set filename for the result |
5557
|`saveFormat`| `png` `jpg` `base64` |
5658
|`textBackgroundStyle` | text background style |
59+
|`maxSize`| default value is 2048, **need RN version >= 0.60.0**, fresco `MaxBitmapSize` [`ImagePipelineConfig.Builder.experiment().setMaxBitmapSize()`](https://github.com/facebook/fresco/blob/08ca5f40cc0b60b4db16d15e45552cafeae39ccb/imagepipeline/src/main/java/com/facebook/imagepipeline/core/ImagePipelineExperiments.java#L282), see [#49](https://github.com/JimmyDaddy/react-native-image-marker/issues/49#issuecomment-535303838)|
5760

5861
```typescript
5962

@@ -83,6 +86,7 @@ export type TextMarkOption = {
8386
shadowStyle: ShadowLayerStyle,
8487
textBackgroundStyle: TextBackgroundStyle,
8588
saveFormat?: ImageFormat,
89+
maxSize?: number, // android only see #49 #42
8690
}
8791
```
8892
@@ -100,6 +104,7 @@ export type TextMarkOption = {
100104
|`quality`| image qulaity |
101105
|`filename` | set filename for the result |
102106
|`saveFormat`| `png` `jpg` `base64`, default is `jpg` |
107+
|`maxSize`| default value is 2048, **need RN version >= 0.60.0**, fresco `MaxBitmapSize` [`ImagePipelineConfig.Builder.experiment().setMaxBitmapSize()`](https://github.com/facebook/fresco/blob/08ca5f40cc0b60b4db16d15e45552cafeae39ccb/imagepipeline/src/main/java/com/facebook/imagepipeline/core/ImagePipelineExperiments.java#L282), see [#49](https://github.com/JimmyDaddy/react-native-image-marker/issues/49#issuecomment-535303838)|
103108
104109
```typescript
105110
export type ImageMarkOption = {
@@ -116,6 +121,7 @@ export type ImageMarkOption = {
116121
position?: Position,
117122
filename?: string,
118123
saveFormat?: ImageFormat,
124+
maxSize?: number, // android only see #49 #42
119125
}
120126
```
121127
@@ -325,6 +331,15 @@ AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcI` },
325331

326332
```
327333

334+
## Extra about Android decoding image
335+
336+
This library use [Fresco](https://github.com/facebook/fresco) to decode image on Android. You can set your configuration through [Configure Fresco in React Native](https://medium.com/in-the-hudl/configure-fresco-in-react-native-28c2bc7dcc4d)
337+
338+
* RN version < 0.60.0 use fresco v1.10.0
339+
* RN version >= 0.60.0 use fresco v2.0.0 +
340+
341+
[see](https://github.com/facebook/react-native/blob/8cf9505bd27c5dade33b17cc177fa5ef1613dbcd/ReactAndroid/gradle.properties#L15)
342+
328343
## Save image to file
329344

330345
* If you want to save the new image result to the phone camera roll, just use the [CameraRoll-module from react-native](https://facebook.github.io/react-native/docs/cameraroll.html#savetocameraroll).

android/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
4+
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
5+
<classpathentry kind="output" path="bin/default"/>
6+
</classpath>

android/build.gradle

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ android {
2323

2424
dependencies {
2525
compile fileTree(dir: 'libs', include: ['*.jar'])
26-
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
27-
exclude group: 'com.android.support', module: 'support-annotations'
28-
})
2926
testCompile 'junit:junit:4.12'
30-
compile 'com.facebook.react:react-native:+'
31-
27+
api 'com.facebook.react:react-native:+'
3228
}

android/src/androidTest/java/com/example/imagemarker/ExampleInstrumentedTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.example.imagemarker;
22

33
import android.content.Context;
4-
import android.support.test.InstrumentationRegistry;
5-
import android.support.test.runner.AndroidJUnit4;
4+
import androidx.test.InstrumentationRegistry;
5+
import androidx.test.runner.AndroidJUnit4;
66

77
import org.junit.Test;
88
import org.junit.runner.RunWith;

android/src/main/java/com/jimmydaddy/imagemarker/ImageMarkerManager.java

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import android.graphics.Color;
88
import android.graphics.Paint;
99
import android.graphics.Typeface;
10-
import android.support.annotation.Nullable;
1110
import android.text.Layout;
1211
import android.text.StaticLayout;
1312
import android.text.TextPaint;
@@ -22,22 +21,24 @@
2221
import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber;
2322
import com.facebook.imagepipeline.image.CloseableImage;
2423
import com.facebook.imagepipeline.request.ImageRequest;
25-
import com.facebook.imagepipeline.request.ImageRequestBuilder;
2624
import com.facebook.react.bridge.Promise;
2725
import com.facebook.react.bridge.ReactApplicationContext;
2826
import com.facebook.react.bridge.ReactContextBaseJavaModule;
2927
import com.facebook.react.bridge.ReactMethod;
3028
import com.facebook.react.bridge.ReadableMap;
3129
import com.facebook.react.views.text.ReactFontManager;
30+
import com.facebook.react.modules.systeminfo.ReactNativeVersion;
3231

3332
import java.io.BufferedOutputStream;
3433
import java.io.ByteArrayOutputStream;
3534
import java.io.FileOutputStream;
3635
import java.io.IOException;
37-
import java.net.URI;
3836
import java.util.UUID;
3937
import java.util.concurrent.Executor;
4038
import java.util.concurrent.Executors;
39+
40+
import static com.facebook.drawee.backends.pipeline.Fresco.getImagePipeline;
41+
import static com.jimmydaddy.imagemarker.Utils.getStringSafe;
4142
import static com.jimmydaddy.imagemarker.Utils.transRGBColor;
4243

4344

@@ -93,6 +94,16 @@ private Bitmap.CompressFormat getSaveFormat(String saveFormat) {
9394
return saveFormat != null && (saveFormat.equals("png") || saveFormat.equals("PNG"))? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG;
9495
}
9596

97+
private void setMaxBitmapSize(int maxSize) {
98+
String major = getStringSafe("major", ReactNativeVersion.VERSION);
99+
String minor = getStringSafe("minor", ReactNativeVersion.VERSION);
100+
String patch = getStringSafe("patch", ReactNativeVersion.VERSION);
101+
if (Integer.valueOf(major) >= 0 && Integer.valueOf(minor) >= 60 && Integer.valueOf(patch) >= 0 ) {
102+
ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this.context).experiment().setMaxBitmapSize(maxSize).build();
103+
Fresco.initialize(this.context, config);
104+
}
105+
}
106+
96107
private void markImage(
97108
final Bitmap bg,
98109
ReadableMap source,
@@ -110,17 +121,16 @@ private void markImage(
110121
// String resultFile = generateCacheFilePathForMarker(imgSavePath, filename);
111122

112123
final String uri = source.getString(PROP_ICON_URI);
113-
114124
Log.d(IMAGE_MARKER_TAG, uri);
115125
Log.d(IMAGE_MARKER_TAG, source.toString());
116126

117127
if (isFrescoImg(uri)) {
118128
ImageRequest imageRequest = ImageRequest.fromUri(uri);
119-
DataSource<CloseableReference<CloseableImage>> dataSource = Fresco.getImagePipeline().fetchDecodedImage(imageRequest, null);
129+
DataSource<CloseableReference<CloseableImage>> dataSource = Fresco.getImagePipeline().fetchDecodedImage(imageRequest, context);
120130
Executor executor = Executors.newSingleThreadExecutor();
121131
dataSource.subscribe(new BaseBitmapDataSubscriber() {
122132
@Override
123-
public void onNewResultImpl(@Nullable Bitmap bitmap) {
133+
public void onNewResultImpl(Bitmap bitmap) {
124134
if (bitmap != null) {
125135
Bitmap mark = Utils.scaleBitmap(bitmap, markerScale);
126136

@@ -473,6 +483,7 @@ public void addText(
473483
final int quality,
474484
String filename,
475485
final String saveFormat,
486+
Integer maxSize,
476487
final Promise promise
477488
) {
478489
if (TextUtils.isEmpty(mark)){
@@ -493,11 +504,14 @@ public void addText(
493504

494505
if (isFrescoImg(uri)) {
495506
ImageRequest imageRequest = ImageRequest.fromUri(uri);
496-
DataSource<CloseableReference<CloseableImage>> dataSource = Fresco.getImagePipeline().fetchDecodedImage(imageRequest, null);
507+
if (null != maxSize && maxSize > 0) {
508+
setMaxBitmapSize(maxSize);
509+
}
510+
DataSource<CloseableReference<CloseableImage>> dataSource = getImagePipeline().fetchDecodedImage(imageRequest, null);
497511
Executor executor = Executors.newSingleThreadExecutor();
498512
dataSource.subscribe(new BaseBitmapDataSubscriber() {
499513
@Override
500-
public void onNewResultImpl(@Nullable Bitmap bitmap) {
514+
public void onNewResultImpl(Bitmap bitmap) {
501515
if (bitmap != null) {
502516
Bitmap bg = Utils.scaleBitmap(bitmap, scale);
503517
markImageByText(bg, mark, null, color, fontName, fontSize, myShadowStyle, myTextBackgroundStyle, X, Y, quality, dest, saveFormat, promise);
@@ -565,6 +579,7 @@ public void addTextByPostion(
565579
final Integer quality,
566580
String filename,
567581
final String saveFormat,
582+
Integer maxSize,
568583
final Promise promise
569584
) {
570585
if (TextUtils.isEmpty(mark)){
@@ -585,11 +600,14 @@ public void addTextByPostion(
585600

586601
if (isFrescoImg(uri)) {
587602
ImageRequest imageRequest = ImageRequest.fromUri(uri);
588-
DataSource<CloseableReference<CloseableImage>> dataSource = Fresco.getImagePipeline().fetchDecodedImage(imageRequest, null);
603+
if (null != maxSize && maxSize > 0) {
604+
setMaxBitmapSize(maxSize);
605+
}
606+
DataSource<CloseableReference<CloseableImage>> dataSource = getImagePipeline().fetchDecodedImage(imageRequest, null);
589607
Executor executor = Executors.newSingleThreadExecutor();
590608
dataSource.subscribe(new BaseBitmapDataSubscriber() {
591609
@Override
592-
public void onNewResultImpl(@Nullable Bitmap bitmap) {
610+
public void onNewResultImpl(Bitmap bitmap) {
593611
if (bitmap != null) {
594612
Bitmap bg = Utils.scaleBitmap(bitmap, scale);
595613
markImageByText(bg, mark, position, color, fontName, fontSize, myShadowStyle, myTextBackgroundStyle, null, null, quality, dest, saveFormat, promise);
@@ -634,7 +652,18 @@ public void onFailureImpl(DataSource dataSource) {
634652
}
635653

636654
@ReactMethod
637-
public void markWithImage(ReadableMap src, final ReadableMap marker, final Integer X, final Integer Y, final Float scale, final Float markerScale, final int quality, String filename, final String saveFormat, final Promise promise ) {
655+
public void markWithImage(
656+
ReadableMap src,
657+
final ReadableMap marker,
658+
final Integer X,
659+
final Integer Y,
660+
final Float scale,
661+
final Float markerScale,
662+
final int quality,
663+
String filename,
664+
final String saveFormat,
665+
Integer maxSize,
666+
final Promise promise ) {
638667

639668

640669
try {
@@ -651,11 +680,14 @@ public void markWithImage(ReadableMap src, final ReadableMap marker, final Integ
651680

652681
if (isFrescoImg(uri)) {
653682
ImageRequest imageRequest = ImageRequest.fromUri(uri);
654-
DataSource<CloseableReference<CloseableImage>> dataSource = Fresco.getImagePipeline().fetchDecodedImage(imageRequest, null);
683+
if (null != maxSize && maxSize > 0) {
684+
setMaxBitmapSize(maxSize);
685+
}
686+
DataSource<CloseableReference<CloseableImage>> dataSource = getImagePipeline().fetchDecodedImage(imageRequest, null);
655687
Executor executor = Executors.newSingleThreadExecutor();
656688
dataSource.subscribe(new BaseBitmapDataSubscriber() {
657689
@Override
658-
public void onNewResultImpl(@Nullable Bitmap bitmap) {
690+
public void onNewResultImpl(Bitmap bitmap) {
659691
if (bitmap != null) {
660692
Bitmap bg = Utils.scaleBitmap(bitmap, scale);
661693
markImage(bg, marker, null, X, Y, markerScale, quality, dest, saveFormat, promise);
@@ -701,7 +733,17 @@ public void onFailureImpl(DataSource dataSource) {
701733
}
702734

703735
@ReactMethod
704-
public void markWithImageByPosition(ReadableMap src, final ReadableMap marker, final String position, final Float scale, final Float markerScale, final int quality, final String filename, final String saveFormat, final Promise promise ) {
736+
public void markWithImageByPosition(
737+
ReadableMap src,
738+
final ReadableMap marker,
739+
final String position,
740+
final Float scale,
741+
final Float markerScale,
742+
final int quality,
743+
final String filename,
744+
final String saveFormat,
745+
Integer maxSize,
746+
final Promise promise ) {
705747

706748
try {
707749

@@ -714,11 +756,14 @@ public void markWithImageByPosition(ReadableMap src, final ReadableMap marker, f
714756

715757
if (isFrescoImg(uri)) {
716758
ImageRequest imageRequest = ImageRequest.fromUri(uri);
717-
DataSource<CloseableReference<CloseableImage>> dataSource = Fresco.getImagePipeline().fetchDecodedImage(imageRequest, null);
759+
if (null != maxSize && maxSize > 0) {
760+
setMaxBitmapSize(maxSize);
761+
}
762+
DataSource<CloseableReference<CloseableImage>> dataSource = getImagePipeline().fetchDecodedImage(imageRequest, null);
718763
Executor executor = Executors.newSingleThreadExecutor();
719764
dataSource.subscribe(new BaseBitmapDataSubscriber() {
720765
@Override
721-
public void onNewResultImpl(@Nullable Bitmap bitmap) {
766+
public void onNewResultImpl(Bitmap bitmap) {
722767
if (bitmap != null) {
723768
Bitmap bg = Utils.scaleBitmap(bitmap, scale);
724769
markImage(bg, marker, position, 0, 0, markerScale, quality, dest, saveFormat, promise);

android/src/main/java/com/jimmydaddy/imagemarker/Utils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.InputStream;
1111
import java.net.HttpURLConnection;
1212
import java.net.URL;
13+
import java.util.Map;
1314

1415
/**
1516
* Created by jimmydaddy on 2018/4/8.
@@ -186,6 +187,11 @@ public static String transRGBColor(String color) {
186187
}
187188
}
188189

190+
public static String getStringSafe(String key, Map<String, Object> map) {
191+
Object obj = map.get(key);
192+
return (obj != null) ? obj.toString() : null;
193+
}
194+
189195
//
190196
// public static BitmapFactory.Options getOptions () {
191197
//

example/markerExample/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,5 @@ buck-out/
5454

5555
# Bundle artifact
5656
*.jsbundle
57+
58+
/ios/Pods/

0 commit comments

Comments
 (0)