Browse Source

Adds base64 support.

Steve Kellock 8 years ago
parent
commit
7e24f3465c

+ 1
- 0
README.md View File

37
  - **`width`** / **`height`** *(number)*: the width and height of the image to capture.
37
  - **`width`** / **`height`** *(number)*: the width and height of the image to capture.
38
  - **`format`** *(string)*: either `png` or `jpg`/`jpeg` or `webm` (Android). Defaults to `png`.
38
  - **`format`** *(string)*: either `png` or `jpg`/`jpeg` or `webm` (Android). Defaults to `png`.
39
  - **`quality`** *(number)*: the quality. 0.0 - 1.0 (default). (only available on lossy formats like jpeg)
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
 ## Getting started
43
 ## Getting started

+ 3
- 2
android/src/main/java/fr/greweb/reactnativeviewshot/RNViewShotModule.java View File

61
         DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
61
         DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
62
         Integer width = options.hasKey("width") ? (int)(displayMetrics.density * options.getDouble("width")) : null;
62
         Integer width = options.hasKey("width") ? (int)(displayMetrics.density * options.getDouble("width")) : null;
63
         Integer height = options.hasKey("height") ? (int)(displayMetrics.density * options.getDouble("height")) : null;
63
         Integer height = options.hasKey("height") ? (int)(displayMetrics.density * options.getDouble("height")) : null;
64
+        boolean base64 = options.hasKey("base64") ? options.getBoolean("base64") : false;
64
         try {
65
         try {
65
             File tmpFile = createTempFile(getReactApplicationContext(), format);
66
             File tmpFile = createTempFile(getReactApplicationContext(), format);
66
             UIManagerModule uiManager = this.reactContext.getNativeModule(UIManagerModule.class);
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
         catch (Exception e) {
70
         catch (Exception e) {
70
             promise.reject(ViewShot.ERROR_UNABLE_TO_SNAPSHOT, "Failed to snapshot view tag "+tag);
71
             promise.reject(ViewShot.ERROR_UNABLE_TO_SNAPSHOT, "Failed to snapshot view tag "+tag);
135
         return File.createTempFile(TEMP_FILE_PREFIX, ext, cacheDir);
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 View File

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

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

+ 13
- 6
ios/RNViewShot.m View File

70
                 return;
70
                 return;
71
             }
71
             }
72
 
72
 
73
-            // Save to a temp file
74
             NSError *error = nil;
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