ImwriteScreenCaptureExample.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.SceneManagement;
  5. using OpenCVForUnity.CoreModule;
  6. using OpenCVForUnity.ImgprocModule;
  7. using OpenCVForUnity.ImgcodecsModule;
  8. using OpenCVForUnity.UnityUtils;
  9. namespace OpenCVForUnityExample
  10. {
  11. [RequireComponent (typeof(Camera))]
  12. public class ImwriteScreenCaptureExample : MonoBehaviour
  13. {
  14. /// <summary>
  15. /// The cube.
  16. /// </summary>
  17. public GameObject cube;
  18. /// <summary>
  19. /// The save path input field.
  20. /// </summary>
  21. public InputField savePathInputField;
  22. /// <summary>
  23. /// The capture flag.
  24. /// </summary>
  25. bool captureFlag = false;
  26. /// <summary>
  27. /// The save path.
  28. /// </summary>
  29. string savePath;
  30. // Use this for initialization
  31. void Start ()
  32. {
  33. savePath = Application.persistentDataPath + "/ImwriteScreenCaptureExample_output.jpg";
  34. //if true, The error log of the Native side OpenCV will be displayed on the Unity Editor Console.
  35. Utils.setDebugMode (true);
  36. Texture2D imgTexture = Resources.Load ("lena") as Texture2D;
  37. Mat imgMat = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC4);
  38. Utils.texture2DToMat (imgTexture, imgMat);
  39. Debug.Log ("imgMat.ToString() " + imgMat.ToString ());
  40. Texture2D texture = new Texture2D (imgMat.cols (), imgMat.rows (), TextureFormat.RGBA32, false);
  41. Utils.matToTexture2D (imgMat, texture);
  42. cube.GetComponent<Renderer> ().material.mainTexture = texture;
  43. Utils.setDebugMode (false);
  44. }
  45. /// <summary>
  46. /// Raises the render image event.
  47. /// </summary>
  48. /// <param name="source">Source.</param>
  49. /// <param name="destination">Destination.</param>
  50. void OnRenderImage (RenderTexture source, RenderTexture destination)
  51. {
  52. if (captureFlag) {
  53. // Debug.Log ("source.width " + source.width + "source.height " + source.height);
  54. Mat cameraMat = new Mat (source.height, source.width, CvType.CV_8UC4);
  55. Texture2D texture = new Texture2D (cameraMat.width (), cameraMat.height (), TextureFormat.ARGB32, false);
  56. Utils.textureToTexture2D (source, texture);
  57. Utils.texture2DToMat (texture, cameraMat);
  58. Imgproc.cvtColor (cameraMat, cameraMat, Imgproc.COLOR_RGBA2BGRA);
  59. Imgproc.rectangle (cameraMat, new Point (0, 0), new Point (cameraMat.width (), cameraMat.height ()), new Scalar (0, 0, 255, 255), 3);
  60. Imgproc.putText (cameraMat, "SavePath:", new Point (5, cameraMat.rows () - 30), Imgproc.FONT_HERSHEY_SIMPLEX, 0.8, new Scalar (0, 0, 255), 2, Imgproc.LINE_AA, false);
  61. Imgproc.putText (cameraMat, savePath, new Point (5, cameraMat.rows () - 8), Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar (255, 255, 255), 0, Imgproc.LINE_AA, false);
  62. Imgcodecs.imwrite (savePath, cameraMat);
  63. savePathInputField.text = savePath;
  64. Debug.Log ("savePath: " + savePath);
  65. captureFlag = false;
  66. }
  67. Graphics.Blit (source, destination);
  68. }
  69. /// <summary>
  70. /// Raises the back button click event.
  71. /// </summary>
  72. public void OnBackButtonClick ()
  73. {
  74. SceneManager.LoadScene ("OpenCVForUnityExample");
  75. }
  76. /// <summary>
  77. /// Raises the capture screen button click event.
  78. /// </summary>
  79. public void OnCaptureScreenButtonClick ()
  80. {
  81. captureFlag = true;
  82. }
  83. /// <summary>
  84. /// Raises the load screen button click event.
  85. /// </summary>
  86. public void OnLoadScreenButtonClick ()
  87. {
  88. Mat loadMat = Imgcodecs.imread (savePath);
  89. Debug.Log ("loadMat.ToString() " + loadMat.ToString ());
  90. if (loadMat.width () != 0 && loadMat.height () != 0) {
  91. Texture2D texture = new Texture2D (loadMat.width (), loadMat.height (), TextureFormat.RGBA32, false);
  92. Imgproc.cvtColor (loadMat, loadMat, Imgproc.COLOR_BGR2RGB);
  93. Utils.matToTexture2D (loadMat, texture);
  94. cube.GetComponent<Renderer> ().material.mainTexture = texture;
  95. }
  96. }
  97. }
  98. }