ViewShot.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. public ViewShot(
  41. int tag,
  42. String extension,
  43. Bitmap.CompressFormat format,
  44. double quality,
  45. @Nullable Integer width,
  46. @Nullable Integer height,
  47. File output,
  48. String result,
  49. Boolean snapshotContentContainer,
  50. ReactApplicationContext reactContext,
  51. Promise promise) {
  52. this.tag = tag;
  53. this.extension = extension;
  54. this.format = format;
  55. this.quality = quality;
  56. this.width = width;
  57. this.height = height;
  58. this.output = output;
  59. this.result = result;
  60. this.snapshotContentContainer = snapshotContentContainer;
  61. this.reactContext = reactContext;
  62. this.promise = promise;
  63. }
  64. @Override
  65. public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
  66. OutputStream os = null;
  67. View view = nativeViewHierarchyManager.resolveView(tag);
  68. if (view == null) {
  69. promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "No view found with reactTag: "+tag);
  70. return;
  71. }
  72. try {
  73. if ("tmpfile".equals(result)) {
  74. os = new FileOutputStream(output);
  75. captureView(view, os);
  76. String uri = Uri.fromFile(output).toString();
  77. promise.resolve(uri);
  78. }
  79. else if ("base64".equals(result)) {
  80. os = new ByteArrayOutputStream();
  81. captureView(view, os);
  82. byte[] bytes = ((ByteArrayOutputStream) os).toByteArray();
  83. String data = Base64.encodeToString(bytes, Base64.NO_WRAP);
  84. promise.resolve(data);
  85. }
  86. else if ("data-uri".equals(result)) {
  87. os = new ByteArrayOutputStream();
  88. captureView(view, 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. }
  94. }
  95. catch (Exception e) {
  96. e.printStackTrace();
  97. promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "Failed to capture view snapshot");
  98. }
  99. finally {
  100. if (os != null) {
  101. try {
  102. os.close();
  103. } catch (IOException e) {
  104. e.printStackTrace();
  105. }
  106. }
  107. }
  108. }
  109. private List<View> getAllChildren(View v) {
  110. if (!(v instanceof ViewGroup)) {
  111. ArrayList<View> viewArrayList = new ArrayList<View>();
  112. viewArrayList.add(v);
  113. return viewArrayList;
  114. }
  115. ArrayList<View> result = new ArrayList<View>();
  116. ViewGroup viewGroup = (ViewGroup) v;
  117. for (int i = 0; i < viewGroup.getChildCount(); i++) {
  118. View child = viewGroup.getChildAt(i);
  119. //Do not add any parents, just add child elements
  120. result.addAll(getAllChildren(child));
  121. }
  122. return result;
  123. }
  124. /**
  125. * Screenshot a view and return the captured bitmap.
  126. * @param view the view to capture
  127. * @return the screenshot or null if it failed.
  128. */
  129. private void captureView (View view, OutputStream os) {
  130. int w = view.getWidth();
  131. int h = view.getHeight();
  132. if (w <= 0 || h <= 0) {
  133. throw new RuntimeException("Impossible to snapshot the view: view is invalid");
  134. }
  135. //evaluate real height
  136. if (snapshotContentContainer) {
  137. h=0;
  138. ScrollView scrollView = (ScrollView)view;
  139. for (int i = 0; i < scrollView.getChildCount(); i++) {
  140. h += scrollView.getChildAt(i).getHeight();
  141. }
  142. }
  143. Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
  144. Bitmap childBitmapBuffer;
  145. Canvas c = new Canvas(bitmap);
  146. view.draw(c);
  147. //after view is drawn, go through children
  148. List<View> childrenList = getAllChildren(view);
  149. for (View child : childrenList) {
  150. if(child instanceof TextureView) {
  151. ((TextureView) child).setOpaque(false);
  152. childBitmapBuffer = ((TextureView) child).getBitmap(child.getWidth(), child.getHeight());
  153. c.drawBitmap(childBitmapBuffer, child.getLeft() + ((ViewGroup)child.getParent()).getLeft() + child.getPaddingLeft(), child.getTop() + ((ViewGroup)child.getParent()).getTop() + child.getPaddingTop(), null);
  154. }
  155. }
  156. if (width != null && height != null && (width != w || height != h)) {
  157. bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
  158. }
  159. if (bitmap == null) {
  160. throw new RuntimeException("Impossible to snapshot the view");
  161. }
  162. bitmap.compress(format, (int)(100.0 * quality), os);
  163. }
  164. }