MyGrayCameraCaptureController.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using Seengene.XDKUnityPluginCloud;
  2. using System;
  3. using System.Collections;
  4. using System.IO;
  5. using System.Text;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. namespace NRKernal.NRExamples {
  9. public class MyGrayCameraCaptureController : MonoBehaviour {
  10. private bool m_IsInitialized = false;
  11. [SerializeField] private CameraCalibrationLoader.EyeType eyeTyep = CameraCalibrationLoader.EyeType.LeftEye;
  12. [SerializeField] private bool m_UndistortImage = false;
  13. [SerializeField] private RawImage m_CameraRawImage;
  14. /// <summary>
  15. /// 本地图片保存路径
  16. /// </summary>
  17. private string m_ImageSavePath;
  18. /// <summary>
  19. /// 重定位间隔时间
  20. /// </summary>
  21. private float m_RelocationInterval = 1f;
  22. private float m_ElapsedTime;
  23. CameraCalibration cameraCalibration = null;
  24. private void Awake() {
  25. cameraCalibration = CameraCalibrationLoader.GetCameraCalibration(eyeTyep);
  26. if (cameraCalibration != null) {
  27. Debug.LogFormat("初始化相机内参成功:{0}, {1}", eyeTyep, cameraCalibration.ToString());
  28. m_IsInitialized = true;
  29. } else {
  30. Debug.LogError("初始化相机内参失败!");
  31. }
  32. }
  33. private void Start() {
  34. // 清空本地图片缓存文件夹
  35. m_ImageSavePath = Path.Combine(Application.persistentDataPath, "seengene");
  36. if (Directory.Exists(m_ImageSavePath))
  37. Directory.Delete(m_ImageSavePath, true);
  38. m_ElapsedTime = 0;
  39. }
  40. void Update() {
  41. if (m_IsInitialized) {
  42. m_ElapsedTime += Time.deltaTime;
  43. if (m_ElapsedTime < m_RelocationInterval) return;
  44. m_ElapsedTime = 0f;
  45. GetFrameCameraImageAndPose();
  46. }
  47. }
  48. private void GetFrameCameraImageAndPose() {
  49. //Debug.Log("SvrManager.Instance.Initialized : " + SvrManager.Instance.Initialized);
  50. if (!SvrManager.Instance.Initialized) return;
  51. int imageWidth = 640;
  52. int imageHeight = 400;
  53. bool outBUdate = true;
  54. uint outCurrFrameIndex = 0;
  55. ulong outFrameExposureNano = 0;
  56. byte[] outFrameData = new byte[imageWidth * imageHeight];
  57. float[] outTRDataArray = new float[7];
  58. Texture2D texture = null;
  59. SvrPluginAndroid.SvrGetLatestQVRCameraFrameData(ref outBUdate, ref outCurrFrameIndex, ref outFrameExposureNano, outFrameData, outTRDataArray);
  60. //Debug.Log("outBUdate: " + outBUdate);
  61. if (outBUdate) {
  62. Vector3 cameraPosition = new Vector3(outTRDataArray[4], outTRDataArray[5], outTRDataArray[6]);
  63. Quaternion cameraRotation = new Quaternion(outTRDataArray[0], outTRDataArray[1], outTRDataArray[2], outTRDataArray[3]);
  64. // compress grayscale image data into JPG format.
  65. TextureFormat textureFormat = TextureFormat.R8;
  66. byte[] jpegBytes;
  67. //float[] cameraIntrinsicData = new float[9] { 266.75388f, 0, 321.64169f, 0, 266.75388f, 196.71561f, 0, 0, 1 };
  68. //float[] disCoeffsData = new float[8] { 0.92348194f, 0, 0, 0, 0, 0, 0, 0 };
  69. //jpegBytes = XDKTools.GetGrayUndistortTextureBytes(outFrameData, textureFormat, imageHeight, imageWidth, cameraIntrinsicData, disCoeffsData);
  70. if (m_UndistortImage) {
  71. jpegBytes = XDKTools.GetGrayUndistortTextureBytes(outFrameData, textureFormat, imageHeight, imageWidth, cameraCalibration);
  72. } else {
  73. jpegBytes = XDKTools.GetGrayTextureBytes(outFrameData, textureFormat, imageHeight, imageWidth);
  74. }
  75. if (texture == null) {
  76. texture = new Texture2D(imageWidth, imageHeight, TextureFormat.R8, false);
  77. }
  78. texture.LoadImage(jpegBytes);
  79. texture.Apply();
  80. m_CameraRawImage.texture = texture;
  81. m_CameraRawImage.SetNativeSize();
  82. //Debug.Log("设置图片显示完成");
  83. //SaveCameraImageToLocal(frame.timeStamp, jpegBytes);
  84. SaveCameraImageToLocal(outCurrFrameIndex, jpegBytes, cameraPosition, cameraRotation, cameraCalibration);
  85. }
  86. }
  87. private void SaveCameraImageToLocal(UInt64 seq, byte[] imageBuffer, Vector3 cameraPosition, Quaternion cameraRotation, CameraCalibration cameraIntrinsic) {
  88. if (!Directory.Exists(m_ImageSavePath)) Directory.CreateDirectory(m_ImageSavePath);
  89. // 计算相机位姿Transform
  90. Vector3 positionIn = cameraPosition;
  91. positionIn.z = -positionIn.z;
  92. Quaternion rotationIn = cameraRotation;
  93. rotationIn.z = -rotationIn.z;
  94. rotationIn.w = -rotationIn.w;
  95. Matrix4x4 matrix = Matrix4x4.TRS(positionIn, rotationIn, Vector3.one);
  96. matrix = matrix.transpose;
  97. string poseData = string.Format(
  98. "{0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14} {15} {16} {17} {18} {19} {20} {21} {22} {23} {24}",
  99. matrix.m00, matrix.m01, matrix.m02, matrix.m03,
  100. matrix.m10, matrix.m11, matrix.m12, matrix.m13,
  101. matrix.m20, matrix.m21, matrix.m22, matrix.m32,
  102. matrix.m30, matrix.m31, matrix.m32, matrix.m33,
  103. cameraIntrinsic.focal_length.x, 0, cameraIntrinsic.principal_point.x,
  104. 0, cameraIntrinsic.focal_length.y, cameraIntrinsic.principal_point.y,
  105. 0, 0, 1);
  106. string poseDataOrigin = string.Format("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14} {15}",
  107. cameraPosition.x, cameraPosition.y, cameraPosition.z, cameraRotation.x, cameraRotation.y, cameraRotation.z, cameraRotation.w,
  108. cameraIntrinsic.focal_length.x, 0, cameraIntrinsic.principal_point.x,
  109. 0, cameraIntrinsic.focal_length.y, cameraIntrinsic.principal_point.y,
  110. 0, 0, 1);
  111. File.WriteAllBytes(Path.Combine(m_ImageSavePath, seq + ".jpg"), imageBuffer);
  112. File.WriteAllText(Path.Combine(m_ImageSavePath, seq + ".txt"), poseData);
  113. File.WriteAllText(Path.Combine(m_ImageSavePath, seq + "-origin" + ".txt"), poseDataOrigin);
  114. Debug.Log("图片本地保存完成:" + Path.Combine(m_ImageSavePath, seq + ".jpg"));
  115. }
  116. private void SaveCameraImageToLocal(UInt64 seq, byte[] imageBuffer) {
  117. if (!Directory.Exists(m_ImageSavePath)) Directory.CreateDirectory(m_ImageSavePath);
  118. File.WriteAllBytes(Path.Combine(m_ImageSavePath, seq + ".jpg"), imageBuffer);
  119. }
  120. }
  121. }