C# 使用EncodeToJPG(在OpenVR中)将Texture2D转换为字节[]

C# 使用EncodeToJPG(在OpenVR中)将Texture2D转换为字节[],c#,unity3d,texture2d,opencvsharp,openvr,C#,Unity3d,Texture2d,Opencvsharp,Openvr,我有一个问题 我只是想,只是…使用EncodeToJPG()函数将Texture2D转换为Byte[] 我的工作区是Unity和C#Script+OpenCvSharp 也许你认为这很容易,但它有一些问题 此脚本使用OpenVR(HTC VIVE) 无论如何,这是我的消息来源 var source = SteamVR_TrackedCamera.Source(undistorted); //Receive texture data from VR. texture = source.textu

我有一个问题

我只是想,只是…使用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()));
而且…有个问题。变量纹理为异常纹理2D类型

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();
}
\u纹理由函数CreateExternalTexture创建,其具有名为nativeTex的Intptr类型参数

我不知道该怎么办

+++编辑!错误显示+++


由于Unity可能无法直接访问外部纹理,我建议先使用Graphics.Blit()将该纹理通过GPU传输到RenderTexture

在RenderTexture中拥有纹理后,您需要再跳过一个环,将RenderTexture.active中的像素读取到另一个基于RAM的标准纹理2D中,然后您应该能够对其进行编码


执行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函数:


这完全奏效了

你说有问题,但你从没提过。EncodeToJPG有什么问题吗?谢谢你告诉我!哈哈,是的,我知道,我做到了。但是它也有同样的问题。你的统一版本是什么?这个问题是在编辑器中发生的吗?同时调用
byte[]img=texture.EncodeToJPG()
然后注释掉
Mat.FromImageData
代码,然后告诉我结果我的统一版本是2018.02 v谢谢!我使用了Graphics.Blit()函数,这很有效@伊普苏克至少要感谢赞巴里的帮助。你甚至没有对他的答案投赞成票,也没有在你自封的答案中承认他
_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);