使用EncodeToJPG(在OpenVR中)将Texture2D转换为Byte []
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用EncodeToJPG(在OpenVR中)将Texture2D转换为Byte []相关的知识,希望对你有一定的参考价值。
我有一个问题!
我只想...,使用EncodeToJPG()函数将Texture2D转换为Byte []!
我的工作区是Unity和C#Script,+ 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()));
并且......有问题。变量纹理是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类型的参数。
我不知道怎么办?
+++编辑!错误显示+++
由于Unity可能无法直接访问外部纹理,我建议先使用Graphics.Blit()通过GPU将该纹理传输到RenderTexture。
一旦你在RenderTexture中获得纹理,你需要再跳过一个环,并从RenderTexture.active读取像素到另一个标准的基于RAM的Texture2D,然后你应该能够编码它。
记得在执行ReadPixels()后应用()纹理
宣布这个功能:
private Texture2D ReadExternalTexture(Texture externalTexture)
{
Texture2D myTexture2D = new Texture2D(texture.width, texture.height);
//myTexture2D => empty Texture2D type variable
if (myTexture2D == null)
{
Debug.Log("var: myTexture2D is null state.");
myTexture2D = new Texture2D(texture.width, texture.height);
}
//Make RenderTexture type variable
RenderTexture tmp = RenderTexture.GetTemporary(
externalTexture.width,
externalTexture.height,
0,
RenderTextureFormat.ARGB32,
RenderTextureReadWrite.sRGB);
Graphics.Blit(externalTexture, tmp);
RenderTexture previous = RenderTexture.active;
RenderTexture.active = tmp;
myTexture2D.ReadPixels(new UnityEngine.Rect(0, 0, tmp.width, tmp.height), 0, 0);
myTexture2D.Apply();
RenderTexture.active = previous;
RenderTexture.ReleaseTemporary(tmp);
return myTexture2D;
}
并在void Update()中使用ReadExternalTexture函数:
_texture = ReadExternalTexture(material.mainTexture);
Mat mat = Mat.FromImageData(_texture.EncodeToPNG());
Cv2.Flip(mat, mat, 0);
//Flip(src, dest, 0->updown flip / 1->leftright flip)
Cv2.Resize(mat, mat, new Size(224, 224));
Cv2.ImShow("Image", mat);
//Cv2.ImWrite(SavePath + "Image.jpg", mat);
这完全奏效了!
以上是关于使用EncodeToJPG(在OpenVR中)将Texture2D转换为Byte []的主要内容,如果未能解决你的问题,请参考以下文章
unity3d encodetopng和encodetojpg的区别
OpenVR、SteamVR 和 Unity3D 如何协同工作?
Unity3D OpenVR SteamVR 点击菜单切换场景