2013-08-18 49 views
0

我有一个来自opencv C++的bgr图像uchar格式。这个函数就像int * texture(int * data,int width,int height);这个函数就像int *该函数在C++ end中处理图像并返回指向数据的指针。如何将Unity中的这些数据转换为纹理。基本上可以将这些数据作为纹理使用。我不想将它写入文件。请帮忙。如何将int *(bgr图像)从C++转换为unity3d纹理图像?

代码段(我使用的DLL):::

public static WebCamTexture webCamTexture; 
private Color32[] data; 
private int[] imageData; 
private int[] imdat; 

void Start() { 
.... 

data = new Color32[webCamTexture.width * webCamTexture.height]; 
     imageData = new int[data.Length * 3]; 
} 


void Update() 
    { 

     webCamTexture.GetPixels32(data); 

     // Convert the Color32[] in int* and emit it in bgr format 
     for (int i = 0; i < data.Length; ++i) 
     { 
      imageData[i * 3] = (int)data[i].b; 
      imageData[i * 3 + 1] = (int)data[i].g; 
      imageData[i * 3 + 2] = (int)data[i].r; 
     } 

     //this is the function called from dll 
     imdat = texture(imageData, int width, int height); 
} 

和DLL底好像::

char *tmp; 

int* texture(int* imageData ,int width ,int height) 
{ 
int n = w * h * 3; 
    tmp = new char[n]; 

    //ImageData inverted here and then passed onto tmp 3 channels image 
    for (int i = 0; i < (w*3); ++i) 
      for (int j = 0; j < h; ++j) 
       tmp[i + j * (w*3)] = (char)imageData[i + (h - j - 1) * (w*3)]; 

return (int)tmp; 

} 

回答

1

我不知道你是什么格式的质感,但如果可以将其转换为byte [],则可以使用Texture2D.LoadImage(byte [])将其转换为工作纹理。

1

你应该可以通过BitConverter.GetBytes()Texture2D.LoadImage()来达到想要的效果。确保您在那里的Unity手册页中特别注意图像格式限制。

不知道你的你的C之间的结合++土地和C#代码的土地,但你应该怎么能够做一点这样的:

/* iImportedTexture = [Your c++ function here]; */ 

byte[] bImportedTexture = BitConverter.GetBytes(iImportedTexture); 

Texture2D importedTexture = Texture2D.LoadImage(bImportedTexture);