ViewShot.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package fr.greweb.reactnativeviewshot;
  2. import javax.annotation.Nullable;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.graphics.Bitmap;
  6. import android.graphics.Canvas;
  7. import android.net.Uri;
  8. import android.util.Base64;
  9. import android.view.TextureView;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.ScrollView;
  13. import com.facebook.react.bridge.Promise;
  14. import com.facebook.react.bridge.ReactApplicationContext;
  15. import com.facebook.react.uimanager.NativeViewHierarchyManager;
  16. import com.facebook.react.uimanager.UIBlock;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. /**
  25. * Snapshot utility class allow to screenshot a view.
  26. */
  27. public class ViewShot implements UIBlock {
  28. static final String ERROR_UNABLE_TO_SNAPSHOT = "E_UNABLE_TO_SNAPSHOT";
  29. private final int tag;
  30. private final String extension;
  31. private final Bitmap.CompressFormat format;
  32. private final double quality;
  33. private final Integer width;
  34. private final Integer height;
  35. private final File output;
  36. private final String result;
  37. private final Promise promise;
  38. private final Boolean snapshotContentContainer;
  39. private final ReactApplicationContext reactContext;
  40. private final Activity currentActivity;
  41. public ViewShot(
  42. int tag,
  43. String extension,
  44. Bitmap.CompressFormat format,
  45. double quality,
  46. @Nullable Integer width,
  47. @Nullable Integer height,
  48. File output,
  49. String result,
  50. Boolean snapshotContentContainer,
  51. ReactApplicationContext reactContext,
  52. Activity currentActivity,
  53. Promise promise) {
  54. this.tag = tag;
  55. this.extension = extension;
  56. this.format = format;
  57. this.quality = quality;
  58. this.width = width;
  59. this.height = height;
  60. this.output = output;
  61. this.result = result;
  62. this.snapshotContentContainer = snapshotContentContainer;
  63. this.reactContext = reactContext;
  64. this.currentActivity = currentActivity;
  65. this.promise = promise;
  66. }
  67. @Override
  68. public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
  69. final View view;
  70. if (tag == -1) {
  71. view = currentActivity.getWindow().getDecorView().findViewById(android.R.id.content);
  72. } else {
  73. view = nativeViewHierarchyManager.resolveView(tag);
  74. }
  75. if (view == null) {
  76. promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "No view found with reactTag: "+tag);
  77. return;
  78. }
  79. try {
  80. if ("tmpfile".equals(result)) {
  81. captureView(view, new FileOutputStream(output));
  82. final String uri = Uri.fromFile(output).toString();
  83. promise.resolve(uri);
  84. } else if ("base64".equals(result)) {
  85. final ByteArrayOutputStream os = new ByteArrayOutputStream();
  86. captureView(view, os);
  87. final byte[] bytes = os.toByteArray();
  88. final String data = Base64.encodeToString(bytes, Base64.NO_WRAP);
  89. promise.resolve(data);
  90. } else if ("data-uri".equals(result)) {
  91. final ByteArrayOutputStream os = new ByteArrayOutputStream();
  92. captureView(view, os);
  93. final byte[] bytes = os.toByteArray();
  94. String data = Base64.encodeToString(bytes, Base64.NO_WRAP);
  95. // correct the extension if JPG
  96. if ("jpg".equals(extension)) {
  97. extension = "jpeg";
  98. }
  99. data = "data:image/"+extension+";base64," + data;
  100. promise.resolve(data);
  101. }
  102. } catch (Exception e) {
  103. e.printStackTrace();
  104. promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "Failed to capture view snapshot");
  105. }
  106. }
  107. private List<View> getAllChildren(View v) {
  108. if (!(v instanceof ViewGroup)) {
  109. ArrayList<View> viewArrayList = new ArrayList<View>();
  110. viewArrayList.add(v);
  111. return viewArrayList;
  112. }
  113. ArrayList<View> result = new ArrayList<View>();
  114. ViewGroup viewGroup = (ViewGroup) v;
  115. for (int i = 0; i < viewGroup.getChildCount(); i++) {
  116. View child = viewGroup.getChildAt(i);
  117. //Do not add any parents, just add child elements
  118. result.addAll(getAllChildren(child));
  119. }
  120. return result;
  121. }
  122. /**
  123. * Screenshot a view and return the captured bitmap.
  124. * @param view the view to capture
  125. * @return the screenshot or null if it failed.
  126. */
  127. private void captureView(View view, OutputStream os) throws IOException {
  128. try {
  129. captureViewImpl(view, os);
  130. } finally {
  131. os.close();
  132. }
  133. }
  134. private void captureViewImpl(View view, OutputStream os) {
  135. int w = view.getWidth();
  136. int h = view.getHeight();
  137. if (w <= 0 || h <= 0) {
  138. throw new RuntimeException("Impossible to snapshot the view: view is invalid");
  139. }
  140. //evaluate real height
  141. if (snapshotContentContainer) {
  142. h=0;
  143. ScrollView scrollView = (ScrollView)view;
  144. for (int i = 0; i < scrollView.getChildCount(); i++) {
  145. h += scrollView.getChildAt(i).getHeight();
  146. }
  147. }
  148. Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
  149. Bitmap childBitmapBuffer;
  150. Canvas c = new Canvas(bitmap);
  151. view.draw(c);
  152. //after view is drawn, go through children
  153. List<View> childrenList = getAllChildren(view);
  154. for (View child : childrenList) {
  155. if(child instanceof TextureView) {
  156. ((TextureView) child).setOpaque(false);
  157. childBitmapBuffer = ((TextureView) child).getBitmap(child.getWidth(), child.getHeight());
  158. if (childBitmapBuffer != null) {
  159. int left = child.getLeft();
  160. int top = child.getTop();
  161. View parentElem = (View)child.getParent();
  162. while (parentElem != null) {
  163. if (parentElem == view) {
  164. break;
  165. }
  166. left += parentElem.getLeft();
  167. top += parentElem.getTop();
  168. parentElem = (View)parentElem.getParent();
  169. }
  170. c.drawBitmap(childBitmapBuffer, left + child.getPaddingLeft(), top + child.getPaddingTop(), null);
  171. }
  172. }
  173. }
  174. if (width != null && height != null && (width != w || height != h)) {
  175. bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
  176. }
  177. if (bitmap == null) {
  178. throw new RuntimeException("Impossible to snapshot the view");
  179. }
  180. bitmap.compress(format, (int)(100.0 * quality), os);
  181. }
  182. }