Quellcode durchsuchen

Adds base64 support.

Steve Kellock vor 8 Jahren
Ursprung
Commit
7e24f3465c

+ 1
- 0
README.md Datei anzeigen

@@ -37,6 +37,7 @@ Returns a Promise of the image URI.
37 37
  - **`width`** / **`height`** *(number)*: the width and height of the image to capture.
38 38
  - **`format`** *(string)*: either `png` or `jpg`/`jpeg` or `webm` (Android). Defaults to `png`.
39 39
  - **`quality`** *(number)*: the quality. 0.0 - 1.0 (default). (only available on lossy formats like jpeg)
40
+ - **`base64`** *(bool)*: if true, the promise returns the base64 encoded data instead of the uri. Defaults to `false`.
40 41
 
41 42
 
42 43
 ## Getting started

+ 3
- 2
android/src/main/java/fr/greweb/reactnativeviewshot/RNViewShotModule.java Datei anzeigen

@@ -61,10 +61,11 @@ public class RNViewShotModule extends ReactContextBaseJavaModule {
61 61
         DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
62 62
         Integer width = options.hasKey("width") ? (int)(displayMetrics.density * options.getDouble("width")) : null;
63 63
         Integer height = options.hasKey("height") ? (int)(displayMetrics.density * options.getDouble("height")) : null;
64
+        boolean base64 = options.hasKey("base64") ? options.getBoolean("base64") : false;
64 65
         try {
65 66
             File tmpFile = createTempFile(getReactApplicationContext(), format);
66 67
             UIManagerModule uiManager = this.reactContext.getNativeModule(UIManagerModule.class);
67
-            uiManager.addUIBlock(new ViewShot(tag, compressFormat, quality, width, height, tmpFile, promise));
68
+            uiManager.addUIBlock(new ViewShot(tag, compressFormat, quality, width, height, tmpFile, base64, promise));
68 69
         }
69 70
         catch (Exception e) {
70 71
             promise.reject(ViewShot.ERROR_UNABLE_TO_SNAPSHOT, "Failed to snapshot view tag "+tag);
@@ -135,4 +136,4 @@ public class RNViewShotModule extends ReactContextBaseJavaModule {
135 136
         return File.createTempFile(TEMP_FILE_PREFIX, ext, cacheDir);
136 137
     }
137 138
 
138
-}
139
+}

+ 24
- 10
android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java Datei anzeigen

@@ -3,15 +3,18 @@ package fr.greweb.reactnativeviewshot;
3 3
 import javax.annotation.Nullable;
4 4
 import android.graphics.Bitmap;
5 5
 import android.net.Uri;
6
+import android.util.Base64;
6 7
 import android.view.View;
7 8
 
8 9
 import com.facebook.react.bridge.Promise;
9 10
 import com.facebook.react.uimanager.NativeViewHierarchyManager;
10 11
 import com.facebook.react.uimanager.UIBlock;
11 12
 
13
+import java.io.ByteArrayOutputStream;
12 14
 import java.io.File;
13 15
 import java.io.FileOutputStream;
14 16
 import java.io.IOException;
17
+import java.io.OutputStream;
15 18
 
16 19
 /**
17 20
  * Snapshot utility class allow to screenshot a view.
@@ -26,6 +29,7 @@ public class ViewShot implements UIBlock {
26 29
     private Integer width;
27 30
     private Integer height;
28 31
     private File output;
32
+    private boolean base64;
29 33
     private Promise promise;
30 34
 
31 35
     public ViewShot(
@@ -35,6 +39,7 @@ public class ViewShot implements UIBlock {
35 39
             @Nullable Integer width,
36 40
             @Nullable Integer height,
37 41
             File output,
42
+            boolean base64,
38 43
             Promise promise) {
39 44
         this.tag = tag;
40 45
         this.format = format;
@@ -42,26 +47,35 @@ public class ViewShot implements UIBlock {
42 47
         this.width = width;
43 48
         this.height = height;
44 49
         this.output = output;
50
+        this.base64 = base64;
45 51
         this.promise = promise;
46 52
     }
47 53
 
48 54
     @Override
49 55
     public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
50
-        FileOutputStream fos = null;
56
+        OutputStream os = null;
51 57
         View view = nativeViewHierarchyManager.resolveView(tag);
52 58
         try {
53
-            fos = new FileOutputStream(output);
54
-            captureView(view, fos);
55
-            String uri = Uri.fromFile(output).toString();
56
-            promise.resolve(uri);
59
+            if (base64) {
60
+                os = new ByteArrayOutputStream();
61
+                captureView(view, os);
62
+                byte[] bytes = ((ByteArrayOutputStream) os).toByteArray();
63
+                String data = Base64.encodeToString(bytes, Base64.NO_WRAP);
64
+                promise.resolve(data);
65
+            } else {
66
+                os = new FileOutputStream(output);
67
+                captureView(view, os);
68
+                String uri = Uri.fromFile(output).toString();
69
+                promise.resolve(uri);
70
+            }
57 71
         }
58 72
         catch (Exception e) {
59 73
             promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "Failed to snapshot view tag "+tag);
60 74
         }
61 75
         finally {
62
-            if (fos != null) {
76
+            if (os != null) {
63 77
                 try {
64
-                    fos.close();
78
+                    os.close();
65 79
                 } catch (IOException e) {
66 80
                     e.printStackTrace();
67 81
                 }
@@ -74,7 +88,7 @@ public class ViewShot implements UIBlock {
74 88
      * @param view the view to capture
75 89
      * @return the screenshot or null if it failed.
76 90
      */
77
-    private void captureView (View view, FileOutputStream fos) {
91
+    private void captureView (View view, OutputStream os) {
78 92
         int w = view.getWidth();
79 93
         int h = view.getHeight();
80 94
         if (w <= 0 || h <= 0) {
@@ -90,6 +104,6 @@ public class ViewShot implements UIBlock {
90 104
         if (bitmap == null) {
91 105
             throw new RuntimeException("Impossible to snapshot the view");
92 106
         }
93
-        bitmap.compress(format, (int)(100.0 * quality), fos);
107
+        bitmap.compress(format, (int)(100.0 * quality), os);
94 108
     }
95
-}
109
+}

+ 1
- 0
index.js Datei anzeigen

@@ -11,6 +11,7 @@ export function takeSnapshot(
11 11
     height ?: number;
12 12
     format ?: "png" | "jpg" | "jpeg" | "webm";
13 13
     quality ?: number;
14
+    base64 ?: bool;
14 15
   }
15 16
 ): Promise<string> {
16 17
   if (typeof view !== "number") {

+ 13
- 6
ios/RNViewShot.m Datei anzeigen

@@ -70,13 +70,20 @@ RCT_EXPORT_METHOD(takeSnapshot:(nonnull NSNumber *)target
70 70
                 return;
71 71
             }
72 72
 
73
-            // Save to a temp file
74 73
             NSError *error = nil;
75
-            NSString *tempFilePath = RCTTempFilePath(format, &error);
76
-            if (tempFilePath) {
77
-                if ([data writeToFile:tempFilePath options:(NSDataWritingOptions)0 error:&error]) {
78
-                    resolve(tempFilePath);
79
-                    return;
74
+            if ([[options objectForKey:@"base64"] boolValue]) {
75
+                // Return as a base64'd string
76
+                NSString *dataString = [data base64EncodedStringWithOptions:0];
77
+                resolve(dataString);
78
+                return;
79
+            } else {
80
+                // Save to a temp file
81
+                NSString *tempFilePath = RCTTempFilePath(format, &error);
82
+                if (tempFilePath) {
83
+                    if ([data writeToFile:tempFilePath options:(NSDataWritingOptions)0 error:&error]) {
84
+                        resolve(tempFilePath);
85
+                        return;
86
+                    }
80 87
                 }
81 88
             }
82 89