Ver código fonte

Merge pull request #2 from gre/master

Merge from gre/react-native-view-shot
Ardavan Kalhori 6 anos atrás
pai
commit
044d6bff89
Nenhuma conta vinculada ao e-mail do autor do commit

+ 44
- 48
android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java Ver arquivo

@@ -33,18 +33,18 @@ public class ViewShot implements UIBlock {
33 33
 
34 34
     static final String ERROR_UNABLE_TO_SNAPSHOT = "E_UNABLE_TO_SNAPSHOT";
35 35
 
36
-    private int tag;
37
-    private String extension;
38
-    private Bitmap.CompressFormat format;
39
-    private double quality;
40
-    private Integer width;
41
-    private Integer height;
42
-    private File output;
43
-    private String result;
44
-    private Promise promise;
45
-    private Boolean snapshotContentContainer;
46
-    private  ReactApplicationContext reactContext;
47
-    private Activity currentActivity;
36
+    private final int tag;
37
+    private final String extension;
38
+    private final Bitmap.CompressFormat format;
39
+    private final double quality;
40
+    private final Integer width;
41
+    private final Integer height;
42
+    private final File output;
43
+    private final String result;
44
+    private final Promise promise;
45
+    private final Boolean snapshotContentContainer;
46
+    private final ReactApplicationContext reactContext;
47
+    private final Activity currentActivity;
48 48
 
49 49
     public ViewShot(
50 50
             int tag,
@@ -75,8 +75,7 @@ public class ViewShot implements UIBlock {
75 75
 
76 76
     @Override
77 77
     public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
78
-        OutputStream os = null;
79
-        View view = null;
78
+        final View view;
80 79
 
81 80
         if (tag == -1) {
82 81
             view = currentActivity.getWindow().getDecorView().findViewById(android.R.id.content);
@@ -90,22 +89,19 @@ public class ViewShot implements UIBlock {
90 89
         }
91 90
         try {
92 91
             if ("tmpfile".equals(result)) {
93
-                os = new FileOutputStream(output);
94
-                captureView(view, os);
95
-                String uri = Uri.fromFile(output).toString();
92
+                captureView(view, new FileOutputStream(output));
93
+                final String uri = Uri.fromFile(output).toString();
96 94
                 promise.resolve(uri);
97
-            }
98
-            else if ("base64".equals(result)) {
99
-                os = new ByteArrayOutputStream();
95
+            } else if ("base64".equals(result)) {
96
+                final ByteArrayOutputStream os = new ByteArrayOutputStream();
100 97
                 captureView(view, os);
101
-                byte[] bytes = ((ByteArrayOutputStream) os).toByteArray();
102
-                String data = Base64.encodeToString(bytes, Base64.NO_WRAP);
98
+                final byte[] bytes = os.toByteArray();
99
+                final String data = Base64.encodeToString(bytes, Base64.NO_WRAP);
103 100
                 promise.resolve(data);
104
-            }
105
-            else if ("data-uri".equals(result)) {
106
-                os = new ByteArrayOutputStream();
101
+            } else if ("data-uri".equals(result)) {
102
+                final ByteArrayOutputStream os = new ByteArrayOutputStream();
107 103
                 captureView(view, os);
108
-                byte[] bytes = ((ByteArrayOutputStream) os).toByteArray();
104
+                final byte[] bytes = os.toByteArray();
109 105
                 String data = Base64.encodeToString(bytes, Base64.NO_WRAP);
110 106
                 // correct the extension if JPG
111 107
                 if ("jpg".equals(extension)) {
@@ -114,20 +110,10 @@ public class ViewShot implements UIBlock {
114 110
                 data = "data:image/"+extension+";base64," + data;
115 111
                 promise.resolve(data);
116 112
             }
117
-        }
118
-        catch (Exception e) {
113
+        } catch (Exception e) {
119 114
             e.printStackTrace();
120 115
             promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "Failed to capture view snapshot");
121 116
         }
122
-        finally {
123
-            if (os != null) {
124
-                try {
125
-                    os.close();
126
-                } catch (IOException e) {
127
-                    e.printStackTrace();
128
-                }
129
-            }
130
-        }
131 117
     }
132 118
 
133 119
     private List<View> getAllChildren(View v) {
@@ -156,7 +142,15 @@ public class ViewShot implements UIBlock {
156 142
      * @param view the view to capture
157 143
      * @return the screenshot or null if it failed.
158 144
      */
159
-    private void captureView (View view, OutputStream os) {
145
+    private void captureView(View view, OutputStream os) throws IOException {
146
+        try {
147
+            captureViewImpl(view, os);
148
+        } finally {
149
+            os.close();
150
+        }
151
+    }
152
+
153
+    private void captureViewImpl(View view, OutputStream os) {
160 154
         int w = view.getWidth();
161 155
         int h = view.getHeight();
162 156
 
@@ -184,18 +178,20 @@ public class ViewShot implements UIBlock {
184 178
             if(child instanceof TextureView) {
185 179
                 ((TextureView) child).setOpaque(false);
186 180
                 childBitmapBuffer = ((TextureView) child).getBitmap(child.getWidth(), child.getHeight());
187
-                int left = child.getLeft();
188
-                int top = child.getTop();
189
-                View parentElem = (View)child.getParent();
190
-                while (parentElem != null) {
191
-                    if (parentElem == view) {
192
-                        break;
181
+                if (childBitmapBuffer != null) {
182
+                    int left = child.getLeft();
183
+                    int top = child.getTop();
184
+                    View parentElem = (View)child.getParent();
185
+                    while (parentElem != null) {
186
+                        if (parentElem == view) {
187
+                            break;
188
+                        }
189
+                        left += parentElem.getLeft();
190
+                        top += parentElem.getTop();
191
+                        parentElem = (View)parentElem.getParent();
193 192
                     }
194
-                    left += parentElem.getLeft();
195
-                    top += parentElem.getTop();
196
-                    parentElem = (View)parentElem.getParent();
193
+                    c.drawBitmap(childBitmapBuffer, left + child.getPaddingLeft(), top + child.getPaddingTop(), null);
197 194
                 }
198
-                c.drawBitmap(childBitmapBuffer, left + child.getPaddingLeft(),  top + child.getPaddingTop(), null);
199 195
             }
200 196
         }
201 197
 

+ 5
- 0
src/index.d.ts Ver arquivo

@@ -10,6 +10,7 @@
10 10
 
11 11
 declare module 'react-native-view-shot' {
12 12
     import { Component, ReactInstance } from 'react'
13
+    import { StyleObj } from 'react-native/Libraries/StyleSheet/StyleSheetTypes'
13 14
 
14 15
     export interface CaptureOptions {
15 16
         /**
@@ -66,6 +67,10 @@ declare module 'react-native-view-shot' {
66 67
          * @param {Error} error
67 68
          */
68 69
         onCaptureFailure?(error: Error): void;
70
+        /**
71
+         * style prop as StyleObj
72
+         */
73
+        style?: StyleObj;
69 74
     }
70 75
 
71 76
     export default class ViewShot extends Component<ViewShotProperties> {