RNViewShotModule.cs 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Newtonsoft.Json.Linq;
  2. using ReactNative.Bridge;
  3. using System;
  4. using System.Collections.Generic;
  5. //using Windows.ApplicationModel.Core;
  6. //using Windows.UI.Core;
  7. namespace RNViewShot
  8. {
  9. /// <summary>
  10. /// A module that allows JS to share data.
  11. /// </summary>
  12. class RNViewShotModule : ReactContextNativeModuleBase
  13. {
  14. private ReactContext reactContext;
  15. /// <summary>
  16. /// Instantiates the <see cref="RNViewShotModule"/>.
  17. /// </summary>
  18. public RNViewShotModule(ReactContext reactContext) : base(reactContext)
  19. {
  20. this.reactContext = reactContext;
  21. }
  22. /// <summary>
  23. /// The name of the native module.
  24. /// </summary>
  25. public override string Name
  26. {
  27. get
  28. {
  29. return "RNViewShot";
  30. }
  31. }
  32. [ReactMethod]
  33. public void takeSnapshot(int tag, JObject options, IPromise promise) {
  34. // Android Equivalent Code
  35. //ReactApplicationContext context = getReactApplicationContext();
  36. //String format = options.hasKey("format") ? options.getString("format") : "png";
  37. //Bitmap.CompressFormat compressFormat =
  38. // format.equals("png")
  39. // ? Bitmap.CompressFormat.PNG
  40. // : format.equals("jpg") || format.equals("jpeg")
  41. // ? Bitmap.CompressFormat.JPEG
  42. // : format.equals("webm")
  43. // ? Bitmap.CompressFormat.WEBP
  44. // : null;
  45. //if (compressFormat == null)
  46. //{
  47. // promise.reject(ViewShot.ERROR_UNABLE_TO_SNAPSHOT, "Unsupported image format: " + format + ". Try one of: png | jpg | jpeg");
  48. // return;
  49. //}
  50. //double quality = options.hasKey("quality") ? options.getDouble("quality") : 1.0;
  51. //DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
  52. //Integer width = options.hasKey("width") ? (int)(displayMetrics.density * options.getDouble("width")) : null;
  53. //Integer height = options.hasKey("height") ? (int)(displayMetrics.density * options.getDouble("height")) : null;
  54. //String result = options.hasKey("result") ? options.getString("result") : "file";
  55. //Boolean snapshotContentContainer = options.hasKey("snapshotContentContainer") ? options.getBoolean("snapshotContentContainer") : false;
  56. //try
  57. //{
  58. // String name = options.hasKey("filename") ? options.getString("filename") : null;
  59. // File tmpFile = "file".equals(result) ? createTempFile(getReactApplicationContext(), format, name) : null;
  60. // UIManagerModule uiManager = this.reactContext.getNativeModule(UIManagerModule.class);
  61. // uiManager.addUIBlock(new ViewShot(tag, format, compressFormat, quality, width, height, tmpFile, result, snapshotContentContainer, promise));
  62. //}
  63. //catch (Exception e) {
  64. // promise.reject(ViewShot.ERROR_UNABLE_TO_SNAPSHOT, "Failed to snapshot view tag "+tag);
  65. //}
  66. //Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
  67. //Graphics graphics = Graphics.FromImage(bitmap as Image);
  68. //graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
  69. //bitmap.Save("c:\\screenshot.jpeg", ImageFormat.Jpeg);
  70. promise.Resolve("This is working!");
  71. }
  72. }
  73. }