Nessuna descrizione

ViewShot.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 int tag;
  30. private String extension;
  31. private Bitmap.CompressFormat format;
  32. private double quality;
  33. private Integer width;
  34. private Integer height;
  35. private File output;
  36. private String result;
  37. private Promise promise;
  38. private Boolean snapshotContentContainer;
  39. private ReactApplicationContext reactContext;
  40. private 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. data = "data:image/"+extension+";base64," + data;
  96. promise.resolve(data);
  97. }
  98. } catch (Exception e) {
  99. e.printStackTrace();
  100. promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "Failed to capture view snapshot");
  101. }
  102. }
  103. private List<View> getAllChildren(View v) {
  104. if (!(v instanceof ViewGroup)) {
  105. ArrayList<View> viewArrayList = new ArrayList<View>();
  106. viewArrayList.add(v);
  107. return viewArrayList;
  108. }
  109. ArrayList<View> result = new ArrayList<View>();
  110. ViewGroup viewGroup = (ViewGroup) v;
  111. for (int i = 0; i < viewGroup.getChildCount(); i++) {
  112. View child = viewGroup.getChildAt(i);
  113. //Do not add any parents, just add child elements
  114. result.addAll(getAllChildren(child));
  115. }
  116. return result;
  117. }
  118. /**
  119. * Screenshot a view and return the captured bitmap.
  120. * @param view the view to capture
  121. * @return the screenshot or null if it failed.
  122. */
  123. private void captureView(View view, OutputStream os) throws IOException {
  124. try {
  125. captureViewImpl(view, os);
  126. } finally {
  127. os.close();
  128. }
  129. }
  130. private void captureViewImpl(View view, OutputStream os) {
  131. int w = view.getWidth();
  132. int h = view.getHeight();
  133. if (w <= 0 || h <= 0) {
  134. throw new RuntimeException("Impossible to snapshot the view: view is invalid");
  135. }
  136. //evaluate real height
  137. if (snapshotContentContainer) {
  138. h=0;
  139. ScrollView scrollView = (ScrollView)view;
  140. for (int i = 0; i < scrollView.getChildCount(); i++) {
  141. h += scrollView.getChildAt(i).getHeight();
  142. }
  143. }
  144. Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
  145. Bitmap childBitmapBuffer;
  146. Canvas c = new Canvas(bitmap);
  147. view.draw(c);
  148. //after view is drawn, go through children
  149. List<View> childrenList = getAllChildren(view);
  150. for (View child : childrenList) {
  151. if(child instanceof TextureView) {
  152. ((TextureView) child).setOpaque(false);
  153. childBitmapBuffer = ((TextureView) child).getBitmap(child.getWidth(), child.getHeight());
  154. int left = child.getLeft();
  155. int top = child.getTop();
  156. View parentElem = (View)child.getParent();
  157. while (parentElem != null) {
  158. if (parentElem == view) {
  159. break;
  160. }
  161. left += parentElem.getLeft();
  162. top += parentElem.getTop();
  163. parentElem = (View)parentElem.getParent();
  164. }
  165. c.drawBitmap(childBitmapBuffer, left + child.getPaddingLeft(), top + child.getPaddingTop(), null);
  166. }
  167. }
  168. if (width != null && height != null && (width != w || height != h)) {
  169. bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
  170. }
  171. if (bitmap == null) {
  172. throw new RuntimeException("Impossible to snapshot the view");
  173. }
  174. bitmap.compress(format, (int)(100.0 * quality), os);
  175. }
  176. }