Нет описания

ViewShot.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 ("file".equals(result)) {
  74. os = new FileOutputStream(output);
  75. captureView(view, os);
  76. String uri = Uri.fromFile(output).toString();
  77. reactContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(uri)));
  78. promise.resolve(uri);
  79. }
  80. else if ("base64".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. promise.resolve(data);
  86. }
  87. else if ("data-uri".equals(result)) {
  88. os = new ByteArrayOutputStream();
  89. captureView(view, os);
  90. byte[] bytes = ((ByteArrayOutputStream) os).toByteArray();
  91. String data = Base64.encodeToString(bytes, Base64.NO_WRAP);
  92. data = "data:image/"+extension+";base64," + data;
  93. promise.resolve(data);
  94. }
  95. else {
  96. promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "Unsupported result: "+result+". Try one of: file | base64 | data-uri");
  97. }
  98. }
  99. catch (Exception e) {
  100. e.printStackTrace();
  101. promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "Failed to capture view snapshot");
  102. }
  103. finally {
  104. if (os != null) {
  105. try {
  106. os.close();
  107. } catch (IOException e) {
  108. e.printStackTrace();
  109. }
  110. }
  111. }
  112. }
  113. private List<View> getAllChildren(View v) {
  114. if (!(v instanceof ViewGroup)) {
  115. ArrayList<View> viewArrayList = new ArrayList<View>();
  116. viewArrayList.add(v);
  117. return viewArrayList;
  118. }
  119. ArrayList<View> result = new ArrayList<View>();
  120. ViewGroup viewGroup = (ViewGroup) v;
  121. for (int i = 0; i < viewGroup.getChildCount(); i++) {
  122. View child = viewGroup.getChildAt(i);
  123. //Do not add any parents, just add child elements
  124. result.addAll(getAllChildren(child));
  125. }
  126. return result;
  127. }
  128. /**
  129. * Screenshot a view and return the captured bitmap.
  130. * @param view the view to capture
  131. * @return the screenshot or null if it failed.
  132. */
  133. private void captureView (View view, OutputStream os) {
  134. int w = view.getWidth();
  135. int h = view.getHeight();
  136. if (w <= 0 || h <= 0) {
  137. throw new RuntimeException("Impossible to snapshot the view: view is invalid");
  138. }
  139. //evaluate real height
  140. if (snapshotContentContainer) {
  141. h=0;
  142. ScrollView scrollView = (ScrollView)view;
  143. for (int i = 0; i < scrollView.getChildCount(); i++) {
  144. h += scrollView.getChildAt(i).getHeight();
  145. }
  146. }
  147. Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
  148. Bitmap childBitmapBuffer;
  149. Canvas c = new Canvas(bitmap);
  150. view.draw(c);
  151. //after view is drawn, go through children
  152. List<View> childrenList = getAllChildren(view);
  153. for (View child : childrenList) {
  154. if(child instanceof TextureView) {
  155. ((TextureView) child).setOpaque(false);
  156. childBitmapBuffer = ((TextureView) child).getBitmap(child.getWidth(), child.getHeight());
  157. c.drawBitmap(childBitmapBuffer, child.getLeft() + ((ViewGroup)child.getParent()).getLeft() + child.getPaddingLeft(), child.getTop() + ((ViewGroup)child.getParent()).getTop() + child.getPaddingTop(), null);
  158. }
  159. }
  160. if (width != null && height != null && (width != w || height != h)) {
  161. bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
  162. }
  163. if (bitmap == null) {
  164. throw new RuntimeException("Impossible to snapshot the view");
  165. }
  166. bitmap.compress(format, (int)(100.0 * quality), os);
  167. }
  168. }