No Description

ViewShot.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package fr.greweb.reactnativeviewshot;
  2. import javax.annotation.Nullable;
  3. import android.graphics.Bitmap;
  4. import android.net.Uri;
  5. import android.util.Base64;
  6. import android.view.View;
  7. import com.facebook.react.bridge.Promise;
  8. import com.facebook.react.uimanager.NativeViewHierarchyManager;
  9. import com.facebook.react.uimanager.UIBlock;
  10. import java.io.ByteArrayOutputStream;
  11. import java.io.File;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import java.io.OutputStream;
  15. /**
  16. * Snapshot utility class allow to screenshot a view.
  17. */
  18. public class ViewShot implements UIBlock {
  19. static final String ERROR_UNABLE_TO_SNAPSHOT = "E_UNABLE_TO_SNAPSHOT";
  20. private int tag;
  21. private String extension;
  22. private Bitmap.CompressFormat format;
  23. private double quality;
  24. private Integer width;
  25. private Integer height;
  26. private File output;
  27. private String result;
  28. private Promise promise;
  29. public ViewShot(
  30. int tag,
  31. String extension,
  32. Bitmap.CompressFormat format,
  33. double quality,
  34. @Nullable Integer width,
  35. @Nullable Integer height,
  36. File output,
  37. String result,
  38. Promise promise) {
  39. this.tag = tag;
  40. this.extension = extension;
  41. this.format = format;
  42. this.quality = quality;
  43. this.width = width;
  44. this.height = height;
  45. this.output = output;
  46. this.result = result;
  47. this.promise = promise;
  48. }
  49. @Override
  50. public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
  51. OutputStream os = null;
  52. View view = nativeViewHierarchyManager.resolveView(tag);
  53. if (view == null) {
  54. promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "No view found with reactTag: "+tag);
  55. return;
  56. }
  57. try {
  58. if ("file".equals(result)) {
  59. os = new FileOutputStream(output);
  60. captureView(view, os);
  61. String uri = Uri.fromFile(output).toString();
  62. promise.resolve(uri);
  63. }
  64. else if ("base64".equals(result)) {
  65. os = new ByteArrayOutputStream();
  66. captureView(view, os);
  67. byte[] bytes = ((ByteArrayOutputStream) os).toByteArray();
  68. String data = Base64.encodeToString(bytes, Base64.NO_WRAP);
  69. promise.resolve(data);
  70. }
  71. else if ("data-uri".equals(result)) {
  72. os = new ByteArrayOutputStream();
  73. captureView(view, os);
  74. byte[] bytes = ((ByteArrayOutputStream) os).toByteArray();
  75. String data = Base64.encodeToString(bytes, Base64.NO_WRAP);
  76. data = "data:image/"+extension+";base64," + data;
  77. promise.resolve(data);
  78. }
  79. else {
  80. promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "Unsupported result: "+result+". Try one of: file | base64 | data-uri");
  81. }
  82. }
  83. catch (Exception e) {
  84. promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "Failed to capture view snapshot");
  85. }
  86. finally {
  87. if (os != null) {
  88. try {
  89. os.close();
  90. } catch (IOException e) {
  91. e.printStackTrace();
  92. }
  93. }
  94. }
  95. }
  96. /**
  97. * Screenshot a view and return the captured bitmap.
  98. * @param view the view to capture
  99. * @return the screenshot or null if it failed.
  100. */
  101. private void captureView (View view, OutputStream os) {
  102. int w = view.getWidth();
  103. int h = view.getHeight();
  104. if (w <= 0 || h <= 0) {
  105. throw new RuntimeException("Impossible to snapshot the view: view is invalid");
  106. }
  107. if (!view.isDrawingCacheEnabled())
  108. view.setDrawingCacheEnabled(true);
  109. Bitmap bitmap = view.getDrawingCache();
  110. if (width != null && height != null && (width != w || height != h)) {
  111. bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
  112. }
  113. if (bitmap == null) {
  114. throw new RuntimeException("Impossible to snapshot the view");
  115. }
  116. bitmap.compress(format, (int)(100.0 * quality), os);
  117. view.setDrawingCacheEnabled(false);
  118. }
  119. }