【发布时间】:2018-07-25 09:02:18
【问题描述】:
我有一个问题!
我只想,只是...使用 EncodeToJPG() 函数将 Texture2D 转换为 Byte[]!
我的工作区是 Unity 和 C# 脚本,+ OpenCvSharp。
也许你认为这很容易,但它有一些问题。
此脚本使用 OpenVR(HTC VIVE)。
无论如何,这是我的来源。
var source = SteamVR_TrackedCamera.Source(undistorted);
//Receive texture data from VR.
texture = source.texture;
//Debug.Log(source.texture.width + "/" + source.texture.height);
//size : 612 /460
if (texture == null)
{
return;
}
//Input texture data into material, and it's in unity (quad GameObject).
//this G.O print display like camera
material.mainTexture = texture;
//here is my src, I want to save Image but texture.EncodeToJPG has some error.
Cv2.ImShow("_Texture ...", Mat.FromImageData(texture.EncodeToJPG()));
Cv2.ImWrite(SavePath+ "Image.jpg", Mat.FromImageData(texture.EncodeToJPG()));
而且...有问题。变量texture是Texture2D类型异常。
if (_texture == null)
{
_texture = Texture2D.CreateExternalTexture((int)header.nWidth, (int)header.nHeight, TextureFormat.RGBA32, false, false, nativeTex);
//_texture = new Texture2D(612, 460, TextureFormat.RGBA32, false);
uint width = 0, height = 0;
var frameBounds = new VRTextureBounds_t();
if (trackedCamera.GetVideoStreamTextureSize(deviceIndex, frameType, ref frameBounds, ref width, ref height) == EVRTrackedCameraError.None)
{
// Account for textures being upside-down in Unity.
frameBounds.vMin = 1.0f - frameBounds.vMin;
frameBounds.vMax = 1.0f - frameBounds.vMax;
this.frameBounds = frameBounds;
}
}
else
{
_texture.UpdateExternalTexture(nativeTex);
//_texture.Apply();
}
_texture是由CreateExternalTexture函数创建的,它有一个名为nativeTex的Intptr类型参数。
我不知道该怎么办?
+++编辑!错误显示+++
【问题讨论】:
-
您说有问题,但您从未提及。 EncodeToJPG 有什么问题吗?
-
谢谢你告诉我!哈哈哈
-
是的,我知道并且我做到了。但它也有同样的问题。
-
您的 Unity 版本是什么?这个问题是否发生在编辑器中?也调用
byte []img = texture.EncodeToJPG()然后注释掉Mat.FromImageData代码然后告诉我结果 -
我的Unity版本是2018.02 v
标签: c# unity3d texture2d opencvsharp openvr