webcamtexture的图像问题

时间:2017-10-17 11:05:15

标签: c# unity3d

我的Unity工作中有一点问题,我正在尝试从Webcam纹理中获取图像,我会将其保存在byte []中。当我显示图像时出现问题,这是看不到的,它看起来像某种类型的网格网格。我认为问题在于我用来传递像素。我希望你能帮助我。enter image description here

    void Update()
{

    //texto.text = texto.text +  width +" / "+ height + " / " + ini.ToString()+ " mal: "+ testo().ToString()+ " SizeIMG: "+ imgData.Length + " NUEVO: "+ nuevo +"\n";

    imgData = null;
    imgData = new byte[width * height * 3];
    resultado = null;
    resultado = new byte[width * height * 3];

    color = webcam.GetPixels32();
    for (int i = 0; i < color.Length; i += 3)
    {
        imgData[(i * 3)] = color[i].r;
        imgData[(i * 3) + 1] = color[i].g;
        imgData[(i * 3) + 2] = color[i].b;

    }
    color = null;

    //video(imgData, resultado);

    //ProcessFrame(imgData, resultado, 0, 0, 0,0, nuevo);
    nuevo = false;

    textura2 = new Texture2D(width, height, TextureFormat.RGB24, false);
    textura2.LoadRawTextureData(imgData);
    textura2.Apply();

    //Left IMAGE
    renderer.material.mainTexture = textura2;
    textura2 = null;

    RightImage.GetComponent<Renderer>().material.mainTexture = webcam;

    if (kont == 30)
    {
        texto.text = "";
        kont = 0;
    }
    kont++;

    Resources.UnloadUnusedAssets();
}

1 个答案:

答案 0 :(得分:1)

我认为问题在于如何将索引编入imgData数组。除了乘以3之外,你还要增加3。

int bytesPerPixel = 3;
const int indexR = 0;
const int indexG = 1;
const int indexB = 2;
for(var i = 0; i < color.Length; i++)
{
    imgData[(i * bytesPerPixel) + indexR] = color[i].r;
    imgData[(i * bytesPerPixel) + indexG] = color[i].g;
    imgData[(i * bytesPerPixel) + indexB] = color[i].b;
}