Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. extern "C" {
  2. uint8_t *resultBuffer;
  3. int bufferWidth;
  4. int bufferHeight;
  5.  
  6. int InitCV_Internal(int width, int height) {
  7. size_t size = width * height * 4;
  8. resultBuffer = new uint8_t[size];
  9.  
  10. bufferWidth = width;
  11. bufferHeight = height;
  12.  
  13. return 0;
  14. }
  15.  
  16. // Convert the image in grayscale.
  17. uint8_t *SubmitFrame_Internal(int width, int height, uint8_t *buffer) {
  18. Mat inFrame = Mat(height, width, CV_8UC4, buffer);
  19. Mat outFrame(inFrame.rows, inFrame.cols, CV_8UC4, Scalar(0,0,0));
  20.  
  21. cvtColor(inFrame, outFrame, COLOR_RGBA2GRAY);
  22.  
  23. size_t size = bufferWidth * bufferHeight * 4;
  24. memcpy(resultBuffer, outFrame.data, size);
  25.  
  26. inFrame.release();
  27. outFrame.release();
  28.  
  29. return resultBuffer;
  30. }
  31.  
  32. using System.Runtime.InteropServices;
  33. using System;
  34. using UnityEngine;
  35.  
  36. public class NativeLibAdapter {
  37.  
  38. [DllImport("native-lib")]
  39. private static extern int InitCV_Internal(int width, int height);
  40.  
  41. [DllImport("native-lib")]
  42. private static extern IntPtr SubmitFrame_Internal(int width, int height, IntPtr buffer);
  43.  
  44. public static int InitCVAdapter(int width, int height)
  45. {
  46. int result = InitCV_Internal(width, height);
  47.  
  48. return result;
  49. }
  50.  
  51. public static IntPtr SubmitFrameAdapter(int width, int height, IntPtr buffer)
  52. {
  53. IntPtr bufferResult = SubmitFrame_Internal(width, height, buffer);
  54.  
  55. return bufferResult;
  56. }
  57. }
  58.  
  59. using System;
  60. using System.Runtime.InteropServices;
  61. using UnityEngine;
  62. using UnityEngine.UI;
  63.  
  64. public class Test : MonoBehaviour
  65. {
  66. private Texture2D texIn;
  67. private Color32[] pixel32;
  68. private GCHandle pixelHandle;
  69. private IntPtr pixelPtr;
  70.  
  71. public Text text;
  72.  
  73. void Start()
  74. {
  75. texIn = (Texture2D)GetComponent<Renderer>().material.mainTexture;
  76. InitTexture();
  77. }
  78.  
  79. void InitTexture()
  80. {
  81. pixel32 = texIn.GetPixels32();
  82. // Pin pixel32 array
  83. pixelHandle = GCHandle.Alloc(pixel32, GCHandleType.Pinned);
  84. // Get the pinned address
  85. pixelPtr = pixelHandle.AddrOfPinnedObject();
  86.  
  87. NativeLibAdapter.InitCVAdapter(texIn.width, texIn.height);
  88.  
  89. // MatToTexture2D
  90. IntPtr result = NativeLibAdapter.SubmitFrameAdapter(texIn.width, texIn.height, pixelPtr);
  91. int bufferSize = texIn.width * texIn.height * 4;
  92. byte[] rawData = new byte[bufferSize];
  93.  
  94. if (result != IntPtr.Zero)
  95. {
  96. Marshal.Copy(result, rawData, 0, bufferSize);
  97. texIn.LoadRawTextureData(rawData);
  98. texIn.Apply();
  99. // text.text = result.ToString();
  100. }
  101. }
  102.  
  103. void OnApplicationQuit()
  104. {
  105. // Free handle
  106. pixelHandle.Free();
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement