Преглед изворни кода

Revert "Convert image to data in a separate thread"

This reverts commit 45061cc62a.
Gaëtan Renaudeau пре 8 година
родитељ
комит
41c357dfa8
1 измењених фајлова са 40 додато и 60 уклоњено
  1. 40
    60
      android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java

+ 40
- 60
android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java Прегледај датотеку

@@ -1,7 +1,6 @@
1 1
 package fr.greweb.reactnativeviewshot;
2 2
 
3 3
 import javax.annotation.Nullable;
4
-
5 4
 import android.graphics.Bitmap;
6 5
 import android.graphics.Canvas;
7 6
 import android.net.Uri;
@@ -17,8 +16,6 @@ import java.io.File;
17 16
 import java.io.FileOutputStream;
18 17
 import java.io.IOException;
19 18
 import java.io.OutputStream;
20
-import java.util.concurrent.ExecutorService;
21
-import java.util.concurrent.Executors;
22 19
 
23 20
 /**
24 21
  * Snapshot utility class allow to screenshot a view.
@@ -27,8 +24,6 @@ public class ViewShot implements UIBlock {
27 24
 
28 25
     static final String ERROR_UNABLE_TO_SNAPSHOT = "E_UNABLE_TO_SNAPSHOT";
29 26
 
30
-    private final static ExecutorService sExecutor = Executors.newFixedThreadPool(5);
31
-
32 27
     private int tag;
33 28
     private String extension;
34 29
     private Bitmap.CompressFormat format;
@@ -62,63 +57,59 @@ public class ViewShot implements UIBlock {
62 57
 
63 58
     @Override
64 59
     public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
60
+        OutputStream os = null;
65 61
         View view = nativeViewHierarchyManager.resolveView(tag);
66 62
         if (view == null) {
67
-            promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "No view found with reactTag: " + tag);
63
+            promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "No view found with reactTag: "+tag);
68 64
             return;
69 65
         }
70
-        final Bitmap bitmap = captureView(view);
71
-        sExecutor.execute(new Runnable() {
72
-            @Override
73
-            public void run() {
74
-                OutputStream os = null;
66
+        try {
67
+            if ("file".equals(result)) {
68
+                os = new FileOutputStream(output);
69
+                captureView(view, os);
70
+                String uri = Uri.fromFile(output).toString();
71
+                promise.resolve(uri);
72
+            }
73
+            else if ("base64".equals(result)) {
74
+                os = new ByteArrayOutputStream();
75
+                captureView(view, os);
76
+                byte[] bytes = ((ByteArrayOutputStream) os).toByteArray();
77
+                String data = Base64.encodeToString(bytes, Base64.NO_WRAP);
78
+                promise.resolve(data);
79
+            }
80
+            else if ("data-uri".equals(result)) {
81
+                os = new ByteArrayOutputStream();
82
+                captureView(view, os);
83
+                byte[] bytes = ((ByteArrayOutputStream) os).toByteArray();
84
+                String data = Base64.encodeToString(bytes, Base64.NO_WRAP);
85
+                data = "data:image/"+extension+";base64," + data;
86
+                promise.resolve(data);
87
+            }
88
+            else {
89
+                promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "Unsupported result: "+result+". Try one of: file | base64 | data-uri");
90
+            }
91
+        }
92
+        catch (Exception e) {
93
+            e.printStackTrace();
94
+            promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "Failed to capture view snapshot");
95
+        }
96
+        finally {
97
+            if (os != null) {
75 98
                 try {
76
-                    if ("file".equals(result)) {
77
-                        os = new FileOutputStream(output);
78
-                        String uri = Uri.fromFile(output).toString();
79
-                        promise.resolve(uri);
80
-                    } else if ("base64".equals(result)) {
81
-                        os = new ByteArrayOutputStream();
82
-                        convertImageToData(bitmap, os);
83
-                        byte[] bytes = ((ByteArrayOutputStream) os).toByteArray();
84
-                        String data = Base64.encodeToString(bytes, Base64.NO_WRAP);
85
-                        promise.resolve(data);
86
-                    } else if ("data-uri".equals(result)) {
87
-                        os = new ByteArrayOutputStream();
88
-                        convertImageToData(bitmap, os);
89
-                        byte[] bytes = ((ByteArrayOutputStream) os).toByteArray();
90
-                        String data = Base64.encodeToString(bytes, Base64.NO_WRAP);
91
-                        data = "data:image/" + extension + ";base64," + data;
92
-                        promise.resolve(data);
93
-                    } else {
94
-                        promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "Unsupported result: " + result +
95
-                                ". Try one of: file | base64 | data-uri");
96
-                    }
97
-                } catch (Exception e) {
99
+                    os.close();
100
+                } catch (IOException e) {
98 101
                     e.printStackTrace();
99
-                    promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "Failed to capture view snapshot");
100
-                } finally {
101
-                    if (os != null) {
102
-                        try {
103
-                            os.close();
104
-                        } catch (IOException e) {
105
-                            e.printStackTrace();
106
-                        }
107
-                    }
108 102
                 }
109 103
             }
110
-        });
111
-
112
-
104
+        }
113 105
     }
114 106
 
115 107
     /**
116 108
      * Screenshot a view and return the captured bitmap.
117
-     *
118 109
      * @param view the view to capture
119
-     * @return bitmap drawn by view
110
+     * @return the screenshot or null if it failed.
120 111
      */
121
-    private Bitmap captureView(View view) {
112
+    private void captureView (View view, OutputStream os) {
122 113
         int w = view.getWidth();
123 114
         int h = view.getHeight();
124 115
         if (w <= 0 || h <= 0) {
@@ -134,17 +125,6 @@ public class ViewShot implements UIBlock {
134 125
         if (bitmap == null) {
135 126
             throw new RuntimeException("Impossible to snapshot the view");
136 127
         }
137
-        return bitmap;
138
-    }
139
-
140
-    /**
141
-     * As Bitmap.compress() may take a long time, it's better to
142
-     * call it in a separate thread
143
-     *
144
-     * @param bitmap image to convert
145
-     * @param os     output stream
146
-     */
147
-    private void convertImageToData(Bitmap bitmap, OutputStream os) {
148
-        bitmap.compress(format, (int) (100.0 * quality), os);
128
+        bitmap.compress(format, (int)(100.0 * quality), os);
149 129
     }
150 130
 }