Browse Source

Merge pull request #2 from gre/master

Merge from gre/react-native-view-shot
Ardavan Kalhori 6 years ago
parent
commit
044d6bff89
No account linked to committer's email address
2 changed files with 49 additions and 48 deletions
  1. 44
    48
      android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java
  2. 5
    0
      src/index.d.ts

+ 44
- 48
android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java View File

33
 
33
 
34
     static final String ERROR_UNABLE_TO_SNAPSHOT = "E_UNABLE_TO_SNAPSHOT";
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
     public ViewShot(
49
     public ViewShot(
50
             int tag,
50
             int tag,
75
 
75
 
76
     @Override
76
     @Override
77
     public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
77
     public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
78
-        OutputStream os = null;
79
-        View view = null;
78
+        final View view;
80
 
79
 
81
         if (tag == -1) {
80
         if (tag == -1) {
82
             view = currentActivity.getWindow().getDecorView().findViewById(android.R.id.content);
81
             view = currentActivity.getWindow().getDecorView().findViewById(android.R.id.content);
90
         }
89
         }
91
         try {
90
         try {
92
             if ("tmpfile".equals(result)) {
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
                 promise.resolve(uri);
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
                 captureView(view, os);
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
                 promise.resolve(data);
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
                 captureView(view, os);
103
                 captureView(view, os);
108
-                byte[] bytes = ((ByteArrayOutputStream) os).toByteArray();
104
+                final byte[] bytes = os.toByteArray();
109
                 String data = Base64.encodeToString(bytes, Base64.NO_WRAP);
105
                 String data = Base64.encodeToString(bytes, Base64.NO_WRAP);
110
                 // correct the extension if JPG
106
                 // correct the extension if JPG
111
                 if ("jpg".equals(extension)) {
107
                 if ("jpg".equals(extension)) {
114
                 data = "data:image/"+extension+";base64," + data;
110
                 data = "data:image/"+extension+";base64," + data;
115
                 promise.resolve(data);
111
                 promise.resolve(data);
116
             }
112
             }
117
-        }
118
-        catch (Exception e) {
113
+        } catch (Exception e) {
119
             e.printStackTrace();
114
             e.printStackTrace();
120
             promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "Failed to capture view snapshot");
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
     private List<View> getAllChildren(View v) {
119
     private List<View> getAllChildren(View v) {
156
      * @param view the view to capture
142
      * @param view the view to capture
157
      * @return the screenshot or null if it failed.
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
         int w = view.getWidth();
154
         int w = view.getWidth();
161
         int h = view.getHeight();
155
         int h = view.getHeight();
162
 
156
 
184
             if(child instanceof TextureView) {
178
             if(child instanceof TextureView) {
185
                 ((TextureView) child).setOpaque(false);
179
                 ((TextureView) child).setOpaque(false);
186
                 childBitmapBuffer = ((TextureView) child).getBitmap(child.getWidth(), child.getHeight());
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 View File

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